Xcode中使用AWS SDK CPP连接S3时MakeShared构造函数报错
问题:Xcode环境下使用AWS SDK CPP创建S3Client时出现构造函数匹配错误
在Xcode中尝试用AWS SDK CPP连接并认证S3时,遇到construct_at无匹配函数的错误,AWS官方文档未明确原因。
报错信息
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/allocator_traits.h:298:9: No matching function for call to 'construct_at' /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/shared_ptr.h:292:37: In instantiation of function template specialization 'std::allocator_traits<std::allocator<Aws::S3::S3Client>>::construct<Aws::S3::S3Client, std::shared_ptr<SimplifiedAWSCredentialsProvider> &, Aws::Client::ClientConfiguration &, void, void>' requested here
相关代码
PTUploader::PTUploader(const std::string& access_key, const std::string& secret_key, const std::string& region) : access_key_(access_key), secret_key_(secret_key), region_(region) { // Initialize the SDK options_.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug; Aws::Utils::Logging::InitializeAWSLogging( Aws::MakeShared<Aws::Utils::Logging::DefaultLogSystem>( "RunUnitTests", Aws::Utils::Logging::LogLevel::Debug, "aws_sdk_")); Aws::InitAPI(options_); Aws::Client::ClientConfiguration config; config.region = region_; // Create credentials auto credentialsProvider = std::make_shared<SimplifiedAWSCredentialsProvider>(access_key_, secret_key_); // Create S3 client with credentials and config s3_client_ = std::make_shared<Aws::S3::S3Client>(credentialsProvider, config); }
解决方案
这个错误核心是S3Client构造函数参数类型不匹配,具体修复方式如下:
确保自定义凭证提供者正确继承父类
S3Client构造函数要求凭证提供者是std::shared_ptr<Aws::Auth::AWSCredentialsProvider>类型,你的SimplifiedAWSCredentialsProvider必须继承自Aws::Auth::AWSCredentialsProvider,并实现必要的虚函数:class SimplifiedAWSCredentialsProvider : public Aws::Auth::AWSCredentialsProvider { public: Aws::Auth::AWSCredentials GetAWSCredentials() const override { return Aws::Auth::AWSCredentials(access_key_, secret_key_); } // 其他必要实现 };显式转换凭证提供者类型
若已正确继承仍报错,可显式转换类型以匹配构造函数要求:auto credentialsProvider = std::static_pointer_cast<Aws::Auth::AWSCredentialsProvider>( std::make_shared<SimplifiedAWSCredentialsProvider>(access_key_, secret_key_) ); s3_client_ = std::make_shared<Aws::S3::S3Client>(credentialsProvider, config);改用AWS SDK推荐的
Aws::MakeShared
AWS SDK提供的Aws::MakeShared会处理内部内存分配与类型兼容问题,替换代码中的std::make_shared:s3_client_ = Aws::MakeShared<Aws::S3::S3Client>("PTUploader", credentialsProvider, config);检查SDK版本与编译配置
确认使用的AWS SDK CPP版本与代码兼容,同时在Xcode项目设置中,将C++ Language Dialect调整为与SDK编译时一致的标准(如GNU++17或更高)。
内容的提问来源于stack exchange,提问作者Samuel Justice
相关产品推荐
相关产品推荐

