s3_file_uploader_action_server_handler.h
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 #pragma once
17 
18 #include <array>
19 #include <limits>
20 #include <vector>
21 
23 #include <file_uploader_msgs/UploadFilesAction.h>
25 
26 
28 
29 #include <aws/s3/S3Client.h>
30 #include <aws/core/utils/logging/LogMacros.h>
32 
33 
34 #include <s3_common/utils.h>
35 
36 namespace Aws{
37 namespace S3 {
38 
40 
41 template<typename T>
43 {
44 public:
45  static void UploadToS3(S3UploadManager& upload_manager, const std::string& bucket, T& goal_handle)
46  {
47  if (!upload_manager.IsAvailable()) {
48  goal_handle.setRejected();
49  return;
50  }
51  goal_handle.setAccepted();
52  auto goal = goal_handle.getGoal();
53  std::vector<UploadDescription> uploads(goal->files.size());
54  for (size_t i=0; i<goal->files.size(); i++) {
55  uploads.at(i) = {
56  goal->files[i],
57  GenerateObjectKey(goal->files[i], goal->upload_location)
58  };
59  }
60  std::vector<UploadDescription> completed_uploads;
61 
62  auto feedback_callback = [&](const std::vector<UploadDescription>& uploaded_files) {
63  completed_uploads = uploaded_files;
64  file_uploader_msgs::UploadFilesFeedback feedback;
65  feedback.num_remaining = uploads.size() - uploaded_files.size();
66  feedback.num_uploaded = uploaded_files.size();
67  goal_handle.publishFeedback(feedback);
68  };
69 
70  auto outcome = upload_manager.UploadFiles(
71  uploads, bucket, feedback_callback);
72  file_uploader_msgs::UploadFilesResult result;
73  if (outcome.IsSuccess()) {
74  result.result_code.success = static_cast<uint8_t>(true);
75  result.result_code.error_code = std::numeric_limits<int16_t>::lowest();
76  } else {
77  result.result_code.success = static_cast<uint8_t>(false);
78  result.result_code.error_code = static_cast<int16_t>(outcome.GetError().GetErrorType());
79  }
80  for (auto const& upload : completed_uploads) {
81  result.files_uploaded.push_back(upload.object_key);
82  }
83  if (actionlib_msgs::GoalStatus::PREEMPTING == goal_handle.getGoalStatus().status) {
84  // Goal cancel has been requested
85  goal_handle.setCanceled(result, "");
86  return;
87  }
88  if (!outcome.IsSuccess()) {
89  std::stringstream ss;
90  ss << "Goal was aborted due to error uploading files. Error Message: " << outcome.GetError().GetMessage();
91  goal_handle.setAborted(result, ss.str());
92  } else {
93  goal_handle.setSucceeded(result, "");
94  }
95  }
96 
97  static void CancelUploadToS3(S3UploadManager& upload_manager)
98  {
99  AWS_LOG_INFO(__func__, "Cancelling Goal");
100  upload_manager.CancelUpload();
101  }
102 };
103 
104 } // namespace S3
105 } // namespace Aws
virtual bool IsAvailable() const
virtual Model::PutObjectOutcome UploadFiles(const std::vector< UploadDescription > &upload_descriptions, const std::string &bucket, const boost::function< void(const std::vector< UploadDescription > &)> &feedback_callback)
virtual void CancelUpload()
static void UploadToS3(S3UploadManager &upload_manager, const std::string &bucket, T &goal_handle)
static void CancelUploadToS3(S3UploadManager &upload_manager)
actionlib::ActionServer< file_uploader_msgs::UploadFilesAction > UploadFilesActionServer
std::string GenerateObjectKey(const std::string &file_path, const std::string &prefix)


s3_file_uploader
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:30