简体   繁体   English

如何在 C++ SDK 中使用 ListObjects() 访问 AWS“子文件夹”?

[英]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.

相关问题 GCP 中不存在路径的 ListObjects 的预期行为是什么? (C++) - What is the expected behavior for ListObjects in GCP with a non existent path? (C++) 如何将数据从检索到的“GetObject()”文件流式传输到磁盘上的本地文件 - C++ Aws SDK - How do I stream data from retrieved "GetObject()" file to localfile on disk - C++ Aws SDK 如何使用 AWS SDK 部署特定堆栈? - How to deploy a specific stack using AWS SDK? C++ 代码未使用 g++ -o object test.cpp 为 AWS SDK 编译 - C++ code is not compiling with g++ -o object test.cpp for AWS SDK 如何使用适用于 Delphi 的 Appercept AWS SDK 获取存储桶中的对象列表? - How to get list of objects in bucket, using Appercept AWS SDK for Delphi? 如何使用 AWS Go SDK 仅更新 DynamoDB 中的单个字段 - How to only update a single field in DynamoDB using AWS Go SDK 如何在 nodejs 中使用 `aws-sdk` 读取 cloudwatch 日志 - How can I read cloudwatch logs using `aws-sdk`in nodejs 如何在golang中集成aws sdk ses? - How to integrate aws sdk ses in golang? 如何使用 Lambda 访问 AWS API Gateway 请求的 HTTP 标头? - How to access HTTP headers for request to AWS API Gateway using Lambda? 如何使用适用于 .NET 的 AWS 开发工具包将多个日志发送到单个 CloudWatch 日志流? - How can I send multiple logs to a single CloudWatch log stream using the AWS SDK for .NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM