我使用以下代码列出了我的 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);
}