s3_facade.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #include <fstream>
16 #include <string>
17 
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>
22 
24 #include <s3_common/s3_facade.h>
25 #include <s3_common/utils.h>
26 
27 namespace Aws
28 {
29 namespace S3
30 {
31 
32 S3Facade::S3Facade(const bool enable_encryption)
33 : S3Facade(enable_encryption, std::make_unique<S3Client>())
34 {
35 }
36 
37 S3Facade::S3Facade(const bool enable_encryption, const Aws::Client::ClientConfiguration & config)
38 : S3Facade(enable_encryption, std::make_unique<S3Client>(config))
39 {
40 }
41 
42 S3Facade::S3Facade(const bool enable_encryption, std::unique_ptr<S3Client> s3_client)
43 : s3_client_(std::move(s3_client)), enable_encryption_(enable_encryption)
44 {
45 }
46 
47 Model::PutObjectOutcome S3Facade::PutObject(
48  const std::string & file_path,
49  const std::string & bucket,
50  const std::string & key)
51 {
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));
62  }
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);
67  if (enable_encryption_) {
68  put_object_request.SetServerSideEncryption(Aws::S3::Model::ServerSideEncryption::AES256);
69  } else {
70  put_object_request.SetServerSideEncryption(Aws::S3::Model::ServerSideEncryption::NOT_SET);
71  }
72 
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);
76  } else {
77  const auto & error = outcome.GetError();
78  AWS_LOGSTREAM_ERROR(__func__, "Failed to upload " << file_path << " to s3://" << bucket << "/" << key
79  << ": " << error.GetMessage());
80  }
81  return outcome;
82 }
83 
84 } // namespace S3
85 } // namespace Aws
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.
Definition: s3_facade.cpp:47
std::unique_ptr< Aws::S3::S3Client > s3_client_
Definition: s3_facade.h:70
S3Facade(const bool enable_encryption)
Definition: s3_facade.cpp:32
const bool enable_encryption_
Definition: s3_facade.h:71


s3_common
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:29