[英]How to access AWS "subfolders" using ListObjects() in C++ SDK?
I am trying to list the objects in the "noaa-goes16/GLM-L2-LCFA/2021/140/05" bucket.我正在尝试列出“noaa-goes16/GLM-L2-LCFA/2021/140/05”存储桶中的对象。 The AWS C++ SDK says that in order to list the objects, you need to use the following function:
AWS C++ SDK 表示,为了列出对象,您需要使用以下函数:
bool ListObjects(const Aws::String& bucketName,
const Aws::Client::ClientConfiguration& clientConfig) {
Aws::S3::S3Client s3_client(clientConfig);
Aws::S3::Model::ListObjectsRequest request;
request.WithBucket(bucketName);
auto outcome = s3_client.ListObjects(request);
if (!outcome.IsSuccess()) {
std::cerr << "Error: ListObjects: " <<
outcome.GetError().GetMessage() << std::endl;
}
else {
Aws::Vector<Aws::S3::Model::Object> objects =
outcome.GetResult().GetContents();
for (Aws::S3::Model::Object& object : objects) {
std::cout << object.GetKey() << std::endl;
}
}
return outcome.IsSuccess();
}
After passing the string to the function, I get the following error:将字符串传递给函数后,出现以下错误:
Aws::SDKOptions options;
Aws::InitAPI(options);
Aws::Client::ClientConfiguration clientConfig;
std::string object = "noaa-goes16/GLM-L2-LCFA/2021/140/05";
ListObject(object, clientConfig);
Output:输出:
Error: ListObjects: The specified key does not exist.
noaa-goes16/GLM-L2-LCFA/2021/140/05
I have tried to add a "/" to the bucket name, and it states again that it does not exist.我试图在存储桶名称中添加一个“/”,它再次声明它不存在。 If I just try "noaa-goes16" as the bucket name, it lists 1000 files that are not relevant to my application.
如果我只是尝试将“noaa-goes16”作为存储桶名称,它会列出 1000 个与我的应用程序无关的文件。 How do I list the files in the "/05" subfolder using the AWS C++ SDK?
如何使用 AWS C++ 开发工具包列出“/05”子文件夹中的文件?
I tried to list the files in a subfolder of the noaa-goes16 bucket using the ListObjects() function in the AWS C++ SDK.我尝试使用 AWS C++ SDK 中的 ListObjects() 函数列出 noaa-goes16 存储桶子文件夹中的文件。 I recieved an error that the subfolder I asked for does not exist.
我收到一条错误消息,指出我要求的子文件夹不存在。 However, I know that the subfolder I am asking for does exist.
但是,我知道我要求的子文件夹确实存在。 I have tried to add a "/" to the end of the bucket, thinking this would resolve the error.
我试图在桶的末尾添加一个“/”,认为这样可以解决错误。 This did not help.
这没有帮助。 I have found that if I only put the main bucket object as the name ("noaa-goes16"), it works.
我发现如果我只将主存储桶对象作为名称(“noaa-goes16”),它就可以工作。 However, I cannot list objects in a specific subfolder and would like to know how.
但是,我无法列出特定子文件夹中的对象,我想知道如何操作。
All, I have just figured it out.一切,我刚刚想通了。 Hopefully, this thread helps others.
希望这个线程可以帮助其他人。
In order to get the subfolder, you need to use a method on the request object.为了获得子文件夹,您需要在请求对象上使用一个方法。
For example, in order to list all of the objects in the "GLM-L2-LCFA/2021/140/05" subfolder, add the Aws::String &prefix:例如,为了列出“GLM-L2-LCFA/2021/140/05”子文件夹中的所有对象,添加 Aws::String &prefix:
bool ListObjects(const Aws::String& bucketName, const Aws::String &prefix,
const Aws::Client::ClientConfiguration& clientConfig) {
Aws::S3::S3Client s3_client(clientConfig);
Aws::S3::Model::ListObjectsRequest request;
request.WithBucket(bucketName).WithPrefix(prefix);
auto outcome = s3_client.ListObjects(request);
if (!outcome.IsSuccess()) {
std::cerr << "Error: ListObjects: " <<
outcome.GetError().GetMessage() << std::endl;
}
else {
Aws::Vector<Aws::S3::Model::Object> objects =
outcome.GetResult().GetContents();
for (Aws::S3::Model::Object& object : objects) {
std::cout << object.GetKey() << std::endl;
}
}
return outcome.IsSuccess();
Now, pass the following:现在,传递以下内容:
Aws::Client::ClientConfiguration clientConfig;
std::string bucket = "noaa-goes16";
std::string prefix = "GLM-L2-LCFA/2021/140/05";
ListObjects(bucket, prefix, clientConfig);
Viola!中提琴!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.