Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Listing Keys Using the AWS SDK for .NET

The following C# code example lists object keys in a bucket. If the response is truncated (<IsTruncated> is true in the response), the code loop continues. Each subsequent request specifies the continuation-token in the request and sets its value to the <NextContinuationToken> returned by Amazon S3 in the previous response.

For instructions on how to create and test a working sample, see Running the Amazon S3 .NET Code Examples.

Copy to clipboard
using System; using Amazon.S3; using Amazon.S3.Model; namespace { class ListObjects { static string bucketName = "***bucket name***"; static IAmazonS3 client; public static void Main(string[] args) { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { Console.WriteLine("Listing objects stored in a bucket"); ListingObjects(); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static void ListingObjects() { try { ListObjectsV2Request request = new ListObjectsV2Request { BucketName = bucketName, MaxKeys = 10 }; ListObjectsV2Response response; do { response = client.ListObjectsV2(request); // Process response. foreach (S3Object entry in response.S3Objects) { Console.WriteLine("key = {0} size = {1}", entry.Key, entry.Size); } Console.WriteLine("Next Continuation Token: {0}", response.NextContinuationToken); request.ContinuationToken = response.NextContinuationToken; } while (response.IsTruncated == true); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "To sign up for service, go to"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when listing objects", amazonS3Exception.Message); } } } } }