S3Client.ListObjects return only 1000 of objects. How to retrieve list of all existing objects using Amazon C# library?
To open the overview pane for an objectSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object. In the Objects list, choose the name of the object for which you want an overview.
All objects are stored in S3 buckets and can be organized with shared names called prefixes. You can also append up to 10 key-value pairs called S3 object tags to each object, which can be created, updated, and deleted throughout an object's lifecycle.
Navigate to the Amazon S3 bucket or folder that contains the objects that you want to delete. Select the check box to the left of the names of the objects that you want to delete. Choose Actions and choose Delete from the list of options that appears. Alternatively, choose Delete from the options in the upper right.
As stated already, Amazon S3 indeed requires Listing Keys Using the AWS SDK for .NET:
As buckets can contain a virtually unlimited number of keys, the complete results of a list query can be extremely large. To manage large result sets, Amazon S3 uses pagination to split them into multiple responses. Each list keys response returns a page of up to 1,000 keys with an indicator indicating if the response is truncated. You send a series of list keys requests until you have received all the keys.
The mentioned indicator is the NextMarker property from the ObjectsResponse Class - its usage is illustrated in the complete example Listing Keys Using the AWS SDK for .NET, with the relevant fragment being:
static AmazonS3 client; client = Amazon.AWSClientFactory.CreateAmazonS3Client( accessKeyID, secretAccessKeyID); ListObjectsRequest request = new ListObjectsRequest(); request.BucketName = bucketName; do { ListObjectsResponse response = client.ListObjects(request); // Process response. // ... // If response is truncated, set the marker to get the next // set of keys. if (response.IsTruncated) { request.Marker = response.NextMarker; } else { request = null; } } while (request != null);
Be aware that the answer above is not using the recommended API to List Objects: http://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
The following snippet shows how it looks with the new API:
using (var s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { ListObjectsV2Request request = new ListObjectsV2Request { BucketName = bucketName, MaxKeys = 10 }; ListObjectsV2Response response; do { response = await s3Client.ListObjectsV2Async(request); // Process response. // ... request.ContinuationToken = response.NextContinuationToken; } while (response.IsTruncated); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With