ListObjectsV2Async() public method

Initiates the asynchronous execution of the ListObjectsV2 operation.
public ListObjectsV2Async ( ListObjectsV2Request request, System cancellationToken = default(CancellationToken) ) : Task
request ListObjectsV2Request Container for the necessary parameters to execute the ListObjectsV2 operation.
cancellationToken System /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. ///
return Task
        /// <summary>
        /// https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingNetSDK.html
        /// </summary>
        /// <returns></returns>
        async Task ListObjectsAsync()
        {
            //List Objects in the Bucket
            ListObjectsV2Request listRequest = new ListObjectsV2Request
            {
                BucketName = awsBucket,
                MaxKeys    = 3
            };
            /// Can also limit Searches with Prefix = searchKeyPrefix
            ///MaxKeys=3 for testing

            ListObjectsV2Response listResponse;

            do
            {
                LOGINFO("OnTask, ListObjectsV2Async: " + listRequest.ToString());
                listResponse = await s3Client.ListObjectsV2Async(listRequest);

                //Process the response
                foreach (S3Object entry in listResponse.S3Objects)
                {
                    LOGINFO("Processing Object Key=" + entry.Key + ", Size=" + entry.Size);
                    var fileName = archivePath + "\\" + entry.Key;
                    ReadObjectDataAsync(entry.Key, fileName).Wait();

                    //Send the local Filename to the Business Service
                    BusinessHost.ProcessInput(fileName);

                    //Now delete the Entry in S3
                    DeleteObjectNonVersionedBucketAsync(entry.Key).Wait();
                }
            } while (listResponse.IsTruncated);
        }
Ejemplo n.º 2
0
        private async Task <List <Amazon.S3.Model.S3Object> > ListObjectsAsync(string bucket, string prefix, CancellationToken ct = default)
        {
            var request = new Amazon.S3.Model.ListObjectsV2Request
            {
                BucketName = bucket,
                Prefix     = prefix,
            };
            var objects = new List <Amazon.S3.Model.S3Object>();

            Amazon.S3.Model.ListObjectsV2Response response;

            do
            {
                response = await _client.ListObjectsV2Async(request, ct);

                objects.AddRange(response.S3Objects);
                request.ContinuationToken = response.NextContinuationToken;
            } while (response.IsTruncated);
            return(objects);
        }
Ejemplo n.º 3
0
        async Task EmptyBucket(Amazon.S3.AmazonS3Client client, string BucketName)
        {
            int itemCount = 0;
            var logger    = LogManager.GetCurrentClassLogger();

            do
            {
                var listObjectsResult = await client.ListObjectsV2Async(new ListObjectsV2Request { BucketName = BucketName });

                itemCount = listObjectsResult.KeyCount;

                if (itemCount > 0)
                {
                    var deleteObjectsResult = await client.DeleteObjectsAsync(new DeleteObjectsRequest { BucketName = BucketName, Objects = listObjectsResult.S3Objects.Select(a => new KeyVersion {
                            Key = a.Key, VersionId = null
                        }).ToList() });

                    if (deleteObjectsResult.HttpStatusCode == System.Net.HttpStatusCode.OK)
                    {
                        logger.Debug($"Successfully deleted {deleteObjectsResult.DeletedObjects.Count} objects from bucket {BucketName}.");
                    }
                }
            } while (itemCount > 0);
        }