首页 > 解决方案 > 无法通过我的 DLL 项目(用 C++ 编写)连接到 AWS S3 存储桶

问题描述

我正在尝试将 AWS S3 存储桶集成到我的项目中(一个用 C++ 编写的 DLL)。我成功地基于为 C++ 提供的 AWS SDK 创建了一个独立的控制台应用程序,该应用程序连接到存储桶并根据要求上传特定文件。当我使用相同的代码并添加所有依赖项(dll 库和头文件)时,DLL 项目编译没有错误。但是,在运行时同样无法连接到 s3 实例。没有明显的错误,但我可以弄清楚代码无法检索任何存储桶列表。我确定我错过了一些东西,但不确定它是什么。仅供参考,为简单起见,我尝试使用以下命令连接到 AWS 存储桶一次:InitAPI(options) { ……….. ……….. } Aws::ShutdownAPI(options)

我的代码的简单概述:

bool FindTheBucket(const Aws::S3::S3Client& s3Client,
    const Aws::String& bucketName) 
{

    Aws::S3::Model::ListBucketsOutcome outcome = s3Client.ListBuckets();
    
    if (outcome.IsSuccess()) 
    {

        
        Aws::Vector<Aws::S3::Model::Bucket> bucket_list =
            outcome.GetResult().GetBuckets();
        for (Aws::S3::Model::Bucket const& bucket : bucket_list)
        {
            if (bucket.GetName() == bucketName)
            return true;
            
        }
        return false;
    }
    else 
        return false;
}

以及调用 SDK 的主要函数中的代码段......

Aws::SDKOptions options;
    options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Info;
    Aws::InitAPI(options);
    {
        
        Aws::String bucket_name = "testbucket"; // a bucket of the same name exists
        Aws::String region = "us-west-2";
        Aws::Client::ClientConfiguration config;
        config.region = region;
        Aws::S3::S3Client s3_client(config);
    
        if (FindTheBucket(s3_client, bucket_name))
        {
            //do the needful
            std::ofstream myFile(str.c_str());
            myFile.close();
            PutObjectBuffer(bucket_name, str.c_str(),
                layerstr, region);
        }
        else
        {
            //some code…
        }
    }
    Aws::ShutdownAPI(options);

标签: amazon-s3dllaws-sdk-cpp

解决方案


推荐阅读