AWS S3Client构造函数调用参数不匹配错误,寻求技术解决方法
main.cpp: 在函数‘void downloadFile(const string&, const string&, std::ostream&)’中:
main.cpp:23:95: 错误:找不到匹配的函数调用‘Aws::S3::S3Client::S3Client(Aws::Auth::AWSCredentials, Aws::Client::ClientConfiguration&)’
Aws::S3::S3Client s3Client(Aws::Auth::AWSCredentials(ACCESS_KEY, SECRET_KEY), clientConfig);来自main.cpp第3行包含的文件:
install/include/aws/s3/S3Client.h:100:9: 候选函数:‘Aws::S3::S3Client::S3Client(const std::shared_ptrAws::Auth::AWSCredentialsProvider&, const Aws::Client::ClientConfiguration&, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy, bool, Aws::S3::US_EAST_1_REGIONAL_ENDPOINT_OPTION)’S3Client(const std::shared_ptrAws::Auth::AWSCredentialsProvider& credentialsProvider,
install/include/aws/s3/S3Client.h:100:9: 说明:候选函数需要5个参数,仅提供了2个
你使用的AWS C++ SDK版本中,S3Client的构造函数已变更,不再直接接受AWSCredentials对象作为第一个参数,而是要求传入**std::shared_ptr<Aws::Auth::AWSCredentialsProvider>**类型的凭据提供者实例,同时需要补充剩余的可选参数(可使用默认值)。
- 引入必需的头文件:取消注释
#include <aws/core/auth/AWSCredentialsProvider.h>,因为要用到凭据提供者相关类。 - 用
BasicAWSCredentialsProvider包装密钥:该类是AWSCredentialsProvider的实现类,可直接传入ACCESS_KEY和SECRET_KEY生成实例,再转为shared_ptr。 - 补充构造函数剩余参数:使用各参数的默认值即可满足基本需求。
#include <aws/lambda-runtime/runtime.h> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/GetObjectRequest.h> #include <aws/core/auth/AWSCredentials.h> #include <aws/core/client/ClientConfiguration.h> #include <aws/core/auth/AWSCredentialsProvider.h> // 取消注释该头文件 using namespace aws::lambda_runtime; Aws::SDKOptions options; static const char* ENDPOINT_URL = "endpoint"; static const char* ACCESS_KEY = "accesskey"; static const char* SECRET_KEY = "secretkey"; void downloadFile(const std::string& bucketName, const std::string& objectKey, std::ostream& outputStream) { Aws::Client::ClientConfiguration clientConfig; clientConfig.endpointOverride = ENDPOINT_URL; clientConfig.scheme = Aws::Http::Scheme::HTTP; clientConfig.connectTimeoutMs = 30000; clientConfig.requestTimeoutMs = 30000; clientConfig.region = "us-west-2"; // 构造凭据提供者的shared_ptr auto credentialsProvider = std::make_shared<Aws::Auth::BasicAWSCredentialsProvider>( Aws::Auth::AWSCredentials(ACCESS_KEY, SECRET_KEY) ); // 使用新的构造函数形式,补充默认参数 Aws::S3::S3Client s3Client( credentialsProvider, clientConfig, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, // 默认值 false, // 默认值:是否禁用chunked编码 Aws::S3::US_EAST_1_REGIONAL_ENDPOINT_OPTION::NOT_SET // 默认值 ); }
内容的提问来源于stack exchange,提问作者user20208419

