18 #include <aws/core/Aws.h> 19 #include <aws/core/utils/logging/LogMacros.h> 20 #include <aws/s3/model/PutObjectRequest.h> 21 #include <aws/s3/S3Client.h> 33 :
S3Facade(enable_encryption, std::make_unique<S3Client>())
37 S3Facade::S3Facade(
const bool enable_encryption,
const Aws::Client::ClientConfiguration & config)
38 :
S3Facade(enable_encryption, std::make_unique<S3Client>(config))
48 const std::string & file_path,
49 const std::string & bucket,
50 const std::string & key)
52 AWS_LOGSTREAM_INFO(__func__,
"Upload: "<<file_path<<
" to s3://"<<bucket<<
"/"<<key);
53 const std::shared_ptr<Aws::IOStream> file_data =
54 std::make_shared<Aws::FStream>(file_path.c_str(),
55 std::ios_base::in | std::ios_base::binary);
56 if (!file_data->good()) {
57 AWS_LOGSTREAM_ERROR(__func__,
"Upload aborted, file " << file_path <<
" couldn't be opened for reading");
58 Aws::StringStream result;
59 result <<
"File " << file_path <<
" couldn't be opened for reading";
60 return Model::PutObjectOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::INVALID_PARAMETER_VALUE,
61 "INVALID_PARAMETER_VALUE", Aws::String(result.str()),
false));
63 Aws::S3::Model::PutObjectRequest put_object_request;
64 put_object_request.SetBucket(bucket.c_str());
65 put_object_request.SetKey(key.c_str());
66 put_object_request.SetBody(file_data);
68 put_object_request.SetServerSideEncryption(Aws::S3::Model::ServerSideEncryption::AES256);
70 put_object_request.SetServerSideEncryption(Aws::S3::Model::ServerSideEncryption::NOT_SET);
73 auto outcome =
s3_client_->PutObject(put_object_request);
74 if (outcome.IsSuccess()) {
75 AWS_LOGSTREAM_INFO(__func__,
"Successfully uploaded " << file_path <<
" to s3://" << bucket <<
"/" << key);
77 const auto & error = outcome.GetError();
78 AWS_LOGSTREAM_ERROR(__func__,
"Failed to upload " << file_path <<
" to s3://" << bucket <<
"/" << key
79 <<
": " << error.GetMessage());
virtual Model::PutObjectOutcome PutObject(const std::string &file_path, const std::string &bucket, const std::string &key)
Call s3 PutObject api to upload file to s3.
std::unique_ptr< Aws::S3::S3Client > s3_client_
S3Facade(const bool enable_encryption)
const bool enable_encryption_