AWS S3 C++:结果被截断时列出所有对象
AWS S3 C++ : List all the object when result is truncated
我使用以下代码列出我的 S3 存储桶中的所有密钥。
我的存储桶中大约有 15,000 个对象。然而,这段代码只是无限循环前 1000 个对象。似乎它不尊重 SetMarker() 方法。
关于如何解决这个问题的任何提示?
#include <aws/s3/S3Client.h>
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/s3/model/Object.h>
int main(int argc, const char* argv[])
{
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::Client::ClientConfiguration config;
config.region="ap-northeast-1";
Aws::S3::S3Client s3_client(config);
Aws::S3::Model::ListObjectsRequest objects_request;
objects_request.WithBucket("MYBUCKETNAME").WithPrefix("some-prefox");
bool isDone = false;
bool isFailed= false;
Aws::S3::Model::ListObjectsOutcome outcome;
int c=0;
while(!isDone) {
outcome=s3_client.ListObjects(objects_request);
if(!outcome.IsSuccess()) break;
//process
Aws::Vector<Aws::S3::Model::Object> object_list = outcome.GetResult().GetContents();
for (auto const &s3_object : object_list)
{
std::cout << "* " << s3_object.GetKey() << std::endl;
c++;
}
std::cout<<"--------- Break"<<c<<"\n";
isDone=!outcome.GetResult().GetIsTruncated();
if(!isDone) {
objects_request.SetMarker(outcome.GetResult().GetNextMarker());
}
}
std::cout << "Count "<<c<<"\n";
//check isFailed
if(!outcome.IsSuccess()) {
std::cout << "ListObjects error: " <<
outcome.GetError().GetExceptionName() << " " <<
outcome.GetError().GetMessage() << std::endl;
}
}
Aws::ShutdownAPI(options);
}
根据 NextMarker 的 documentation:
Note
This element is returned only if you specify a delimiter request parameter. If the response does not include the NextMarker and it is truncated, you can use the value of the last Key in the response as the marker in the subsequent request to get the next set of object keys.
因此您的代码应该是:
if(!isDone) {
objects_request.SetMarker(outcome.GetResult().GetContents().back().GetKey());
}
另请注意,V1 ListObjects 方法已弃用,您应该使用 ListObjectsV2,它使用更易于使用的显式延续标记。
我使用以下代码列出我的 S3 存储桶中的所有密钥。
我的存储桶中大约有 15,000 个对象。然而,这段代码只是无限循环前 1000 个对象。似乎它不尊重 SetMarker() 方法。
关于如何解决这个问题的任何提示?
#include <aws/s3/S3Client.h>
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/s3/model/Object.h>
int main(int argc, const char* argv[])
{
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::Client::ClientConfiguration config;
config.region="ap-northeast-1";
Aws::S3::S3Client s3_client(config);
Aws::S3::Model::ListObjectsRequest objects_request;
objects_request.WithBucket("MYBUCKETNAME").WithPrefix("some-prefox");
bool isDone = false;
bool isFailed= false;
Aws::S3::Model::ListObjectsOutcome outcome;
int c=0;
while(!isDone) {
outcome=s3_client.ListObjects(objects_request);
if(!outcome.IsSuccess()) break;
//process
Aws::Vector<Aws::S3::Model::Object> object_list = outcome.GetResult().GetContents();
for (auto const &s3_object : object_list)
{
std::cout << "* " << s3_object.GetKey() << std::endl;
c++;
}
std::cout<<"--------- Break"<<c<<"\n";
isDone=!outcome.GetResult().GetIsTruncated();
if(!isDone) {
objects_request.SetMarker(outcome.GetResult().GetNextMarker());
}
}
std::cout << "Count "<<c<<"\n";
//check isFailed
if(!outcome.IsSuccess()) {
std::cout << "ListObjects error: " <<
outcome.GetError().GetExceptionName() << " " <<
outcome.GetError().GetMessage() << std::endl;
}
}
Aws::ShutdownAPI(options);
}
根据 NextMarker 的 documentation:
Note
This element is returned only if you specify a delimiter request parameter. If the response does not include the NextMarker and it is truncated, you can use the value of the last Key in the response as the marker in the subsequent request to get the next set of object keys.
因此您的代码应该是:
if(!isDone) {
objects_request.SetMarker(outcome.GetResult().GetContents().back().GetKey());
}
另请注意,V1 ListObjects 方法已弃用,您应该使用 ListObjectsV2,它使用更易于使用的显式延续标记。