s3_upload_manager.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 
16 #include <boost/function.hpp>
17 #include <string>
18 
19 #include <aws/core/Aws.h>
20 #include <aws/core/utils/logging/LogMacros.h>
21 
23 #include <s3_common/s3_facade.h>
25 
26 
27 namespace Aws
28 {
29 namespace S3
30 {
31 
32 S3UploadManager::S3UploadManager(const bool enable_encryption):
33  S3UploadManager(std::make_unique<S3Facade>(enable_encryption))
34 {
35 }
36 
37 S3UploadManager::S3UploadManager(const bool enable_encryption, const Aws::Client::ClientConfiguration & config):
38  S3UploadManager(std::make_unique<S3Facade>(enable_encryption, config))
39 {
40 }
41 
42 S3UploadManager::S3UploadManager(std::unique_ptr<S3Facade> s3_facade):
44  s3_facade_(std::move(s3_facade))
45 {
46 }
47 
49 {
50  std::lock_guard<std::recursive_mutex> lock(mutex_);
51  if (!IsAvailable()) {
53  }
54 }
55 
56 Model::PutObjectOutcome S3UploadManager::UploadFiles(
57  const std::vector<UploadDescription> & upload_descriptions,
58  const std::string & bucket,
59  const boost::function<void (const std::vector<UploadDescription>&)>& feedback_callback)
60 {
61  {
62  std::lock_guard<std::recursive_mutex> lock(mutex_);
63  if (!IsAvailable()) {
64  return Model::PutObjectOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::INVALID_ACTION,
65  "INVALID_ACTION", "UploadFiles aborted. UploadFiles request already active.", false));
66  }
68  }
69  std::vector<UploadDescription> completed_uploads;
70  // If no files were provided then upload was successful.
71  Model::PutObjectResult default_result;
72  Model::PutObjectOutcome upload_outcome(default_result);
73  std::vector<UploadDescription> uploaded_files;
74  for (const auto& upload_description: upload_descriptions) {
75  {
76  std::lock_guard<std::recursive_mutex> lock(mutex_);
78  break;
79  }
80  }
81  auto file_path = upload_description.file_path;
82  auto object_key = upload_description.object_key;
83  //bucket comes from config
84  AWS_LOGSTREAM_INFO(__func__,"Uploading file " << file_path << " to " << object_key);
85  upload_outcome = s3_facade_->PutObject(file_path, bucket, object_key);
86  if (!upload_outcome.IsSuccess()) {
87  break;
88  }
89  completed_uploads.push_back(upload_description);
90  feedback_callback(completed_uploads);
91  }
92  {
93  std::lock_guard<std::recursive_mutex> lock(mutex_);
95  }
96  return upload_outcome;
97 }
98 
100 {
101  std::lock_guard<std::recursive_mutex> lock(mutex_);
103 }
104 
105 } // namespace S3
106 } // namespace Aws
virtual bool IsAvailable() const
S3UploadManagerState manager_status_
virtual Model::PutObjectOutcome UploadFiles(const std::vector< UploadDescription > &upload_descriptions, const std::string &bucket, const boost::function< void(const std::vector< UploadDescription > &)> &feedback_callback)
std::unique_ptr< S3Facade > s3_facade_
S3UploadManager(const bool enable_encryption)
std::recursive_mutex mutex_


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