Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

使用 适用于 .NET 的 AWS 开发工具包 列出键

以下 C# 代码示例列出存储桶中的对象键。如果响应被截断 (响应中的 <IsTruncated> 为 true),则代码循环将继续。每个后续请求均指定请求中的 continuation-token,并将其值设置为上一响应中由 Amazon S3 返回的 <NextContinuationToken>

有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { 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 http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when listing objects", amazonS3Exception.Message); } } } } }