2012-03-29 90 views

回答

53

如前所述,Amazon S3确实需要Listing Keys Using the AWS SDK for .NET

如水桶可以包含按键几乎无限数量,清单查询的 完整的结果可能会非常大。要管理大型结果集,Amazon S3会使用分页将它们拆分为多个响应,即 。每个列表键响应都会返回一个高达 1,000个键的页面,并带有一个指示符,指示响应是否被截断。 您发送一系列列表密钥请求,直到您收到所有密钥 。

所提到的指标是从ObjectsResponse ClassNextMarker属性 - 它的使用在完整的示例Listing Keys Using the AWS SDK for .NET所示,与相关的片段的存在:

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); 
+0

2年以上后,目前仍是完美的解决方案!谢谢:) – hardba11 2014-04-24 18:55:12

+0

完美答案... – 2014-06-09 18:54:12

+2

您的第二个链接现在已被破解(迭代通过多页结果),并可在此处找到:http://docs.aws.amazon.com/AmazonS3/latest/dev/ ListingObjectKeysUsingNetSDK.html – adamdport 2014-09-03 15:15:21

0

根据文档的客户端使用分页的你描述的情况。根据文件,您应该使用IsTruncated的结果...如果它是true再次调用ListObjects并正确设置Marker以获得下一页等 - 停止呼叫时IsTruncated返回false

3

注意,上面的答案是不使用推荐的API列表对象:http://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html

下面的片段展示了它的外观与新的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 == true);   
}