C# 如何列出 Amazon S3 存储桶中的 _all_ 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9920804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list _all_ objects in Amazon S3 bucket?
提问by user626528
S3Client.ListObjects return only 1000 of objects. How to retrieve list of all existing objects using Amazon C# library?
S3Client.ListObjects 仅返回 1000 个对象。如何使用 Amazon C# 库检索所有现有对象的列表?
采纳答案by Steffen Opel
As stated already, Amazon S3indeed requires Listing Keys Using the AWS SDK for .NET:
如前所述,Amazon S3确实需要使用适用于 .NET 的 AWS 开发工具包列出密钥:
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.
由于存储桶可以包含几乎无限数量的键,因此列表查询的完整结果可能非常大。为了管理大型结果集,Amazon S3 使用分页将它们拆分为多个响应。每个列表键响应返回一个包含多达 1,000 个键的页面,并带有指示响应是否被截断的指示符。您发送一系列列表密钥请求,直到您收到所有密钥。
The mentioned indicator is the NextMarkerproperty 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:
提到的指标是ObjectsResponse 类中的NextMarker属性- 完整示例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);
回答by Yahia
According to the documentation the client uses pagination in the case you describe. As per documentation you should use IsTruncatedon the result... if it is truecall ListObjectsagain with correctly setup Markerto get the next page etc. - stop calling when IsTruncatedreturns false.
根据文档,客户端在您描述的情况下使用分页。根据文档,您应该在结果上使用IsTruncated......如果它通过正确设置再次true调用以获取下一页等 -返回时停止调用。ListObjectsMarkerIsTruncatedfalse
回答by lgrosales
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
请注意,上面的答案没有使用推荐的 API 来列出对象:http: //docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
The following snippet shows how it looks with the new API:
以下代码段显示了新 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);
}

