Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list _all_ objects in Amazon S3 bucket?

S3Client.ListObjects return only 1000 of objects. How to retrieve list of all existing objects using Amazon C# library?

like image 668
user626528 Avatar asked Mar 29 '12 07:03

user626528


People also ask

How do I view contents of a S3 bucket?

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.

Where are the objects stored in S3?

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.

What is the best way to delete multiple objects from S3?

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.


2 Answers

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); 
like image 168
Steffen Opel Avatar answered Sep 19 '22 19:09

Steffen Opel


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);         } 
like image 33
lgrosales Avatar answered Sep 22 '22 19:09

lgrosales