Mercurial > hg > orthanc-object-storage
annotate Aws/AwsS3StoragePlugin.cpp @ 57:ba1be668e475
fix initialization of the aws static library
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Jun 2021 16:55:24 +0200 |
parents | 1c3e34f5c5c6 |
children | d10696f94959 |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
37
f55b2afdf53d
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
33
diff
changeset
|
3 * Copyright (C) 2020-2021 Osimis S.A., Belgium |
1 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 #include "AwsS3StoragePlugin.h" | |
20 | |
21 #include <aws/core/Aws.h> | |
22 #include <aws/s3/S3Client.h> | |
23 #include <aws/s3/model/PutObjectRequest.h> | |
24 #include <aws/s3/model/GetObjectRequest.h> | |
25 #include <aws/s3/model/ListObjectsRequest.h> | |
26 #include <aws/s3/model/DeleteObjectRequest.h> | |
27 #include <aws/core/auth/AWSCredentialsProvider.h> | |
28 #include <aws/core/utils/memory/stl/AWSStringStream.h> | |
57
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
29 #include <aws/crt/Api.h> |
1 | 30 |
31 #include <boost/lexical_cast.hpp> | |
32 #include <iostream> | |
33 #include <fstream> | |
34 | |
35 const char* ALLOCATION_TAG = "OrthancS3"; | |
15 | 36 static const char* const PLUGIN_SECTION = "AwsS3Storage"; |
1 | 37 |
15 | 38 class AwsS3StoragePlugin : public BaseStoragePlugin |
1 | 39 { |
40 public: | |
41 | |
42 Aws::S3::S3Client client_; | |
43 std::string bucketName_; | |
44 | |
45 public: | |
46 | |
15 | 47 AwsS3StoragePlugin(const Aws::S3::S3Client& client, const std::string& bucketName, bool enableLegacyStorageStructure); |
1 | 48 |
57
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
49 virtual ~AwsS3StoragePlugin(); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
50 |
1 | 51 virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); |
52 virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); | |
53 virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); | |
15 | 54 virtual const char* GetConfigurationSectionName() {return PLUGIN_SECTION;}; |
1 | 55 }; |
56 | |
57 | |
58 class Writer : public IStoragePlugin::IWriter | |
59 { | |
60 std::string path_; | |
61 Aws::S3::S3Client client_; | |
62 std::string bucketName_; | |
63 | |
64 public: | |
65 Writer(const Aws::S3::S3Client& client, const std::string& bucketName, const std::string& path) | |
66 : path_(path), | |
67 client_(client), | |
68 bucketName_(bucketName) | |
69 { | |
70 } | |
71 | |
72 virtual ~Writer() | |
73 { | |
74 } | |
75 | |
76 virtual void Write(const char* data, size_t size) | |
77 { | |
78 Aws::S3::Model::PutObjectRequest putObjectRequest; | |
79 | |
80 putObjectRequest.SetBucket(bucketName_.c_str()); | |
81 putObjectRequest.SetKey(path_.c_str()); | |
82 | |
83 std::shared_ptr<Aws::StringStream> stream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG, std::ios_base::in | std::ios_base::binary); | |
84 | |
85 stream->rdbuf()->pubsetbuf(const_cast<char*>(data), size); | |
86 stream->rdbuf()->pubseekpos(size); | |
87 stream->seekg(0); | |
88 | |
89 putObjectRequest.SetBody(stream); | |
90 | |
91 auto result = client_.PutObject(putObjectRequest); | |
92 | |
93 if (!result.IsSuccess()) | |
94 { | |
95 throw StoragePluginException(std::string("error while writing file ") + path_ + ": " + result.GetError().GetExceptionName().c_str() + " " + result.GetError().GetMessage().c_str()); | |
96 } | |
97 } | |
98 }; | |
99 | |
100 | |
101 class Reader : public IStoragePlugin::IReader | |
102 { | |
103 std::string path_; | |
104 Aws::S3::S3Client client_; | |
105 std::string bucketName_; | |
106 | |
107 public: | |
108 Reader(const Aws::S3::S3Client& client, const std::string& bucketName, const std::string& path) | |
109 : path_(path), | |
110 client_(client), | |
111 bucketName_(bucketName) | |
112 { | |
113 } | |
114 | |
115 virtual ~Reader() | |
116 { | |
117 | |
118 } | |
119 virtual size_t GetSize() | |
120 { | |
121 Aws::S3::Model::ListObjectsRequest listObjectRequest; | |
122 listObjectRequest.SetBucket(bucketName_.c_str()); | |
123 listObjectRequest.SetPrefix(path_.c_str()); | |
124 | |
125 auto result = client_.ListObjects(listObjectRequest); | |
126 | |
127 if (result.IsSuccess()) | |
128 { | |
129 Aws::Vector<Aws::S3::Model::Object> objectList = | |
130 result.GetResult().GetContents(); | |
131 | |
132 if (objectList.size() == 1) | |
133 { | |
134 return objectList[0].GetSize(); | |
135 } | |
32 | 136 else if (objectList.size() > 1) |
137 { | |
138 throw StoragePluginException(std::string("error while reading file ") + path_ + ": multiple objet with same name !"); | |
139 } | |
140 throw StoragePluginException(std::string("error while reading file ") + path_ + ": object not found !"); | |
1 | 141 } |
142 else | |
143 { | |
144 throw StoragePluginException(std::string("error while reading file ") + path_ + ": " + result.GetError().GetExceptionName().c_str() + " " + result.GetError().GetMessage().c_str()); | |
145 } | |
146 } | |
41 | 147 |
148 virtual void ReadWhole(char* data, size_t size) | |
149 { | |
150 _Read(data, size, 0, false); | |
151 } | |
152 | |
153 virtual void ReadRange(char* data, size_t size, size_t fromOffset) | |
154 { | |
155 _Read(data, size, fromOffset, true); | |
156 } | |
157 | |
158 void _Read(char* data, size_t size, size_t fromOffset, bool useRange) | |
1 | 159 { |
160 Aws::S3::Model::GetObjectRequest getObjectRequest; | |
161 getObjectRequest.SetBucket(bucketName_.c_str()); | |
162 getObjectRequest.SetKey(path_.c_str()); | |
163 | |
41 | 164 if (useRange) |
165 { | |
166 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests | |
167 std::string range = std::string("bytes=") + boost::lexical_cast<std::string>(fromOffset) + "-" + boost::lexical_cast<std::string>(fromOffset + size -1); | |
168 getObjectRequest.SetRange(range.c_str()); | |
169 } | |
170 | |
1 | 171 getObjectRequest.SetResponseStreamFactory( |
172 [data, size]() | |
173 { | |
174 std::unique_ptr<Aws::StringStream> | |
175 istream(Aws::New<Aws::StringStream>(ALLOCATION_TAG)); | |
176 | |
177 istream->rdbuf()->pubsetbuf(static_cast<char*>(data), | |
178 size); | |
179 | |
180 return istream.release(); | |
181 }); | |
182 | |
183 // Get the object | |
184 auto result = client_.GetObject(getObjectRequest); | |
185 if (result.IsSuccess()) | |
186 { | |
187 } | |
188 else | |
189 { | |
190 throw StoragePluginException(std::string("error while reading file ") + path_ + ": " + result.GetError().GetExceptionName().c_str() + " " + result.GetError().GetMessage().c_str()); | |
191 } | |
192 } | |
193 | |
194 }; | |
195 | |
196 | |
197 | |
41 | 198 |
199 | |
1 | 200 const char* AwsS3StoragePluginFactory::GetStoragePluginName() |
201 { | |
202 return "AWS S3 Storage"; | |
203 } | |
204 | |
57
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
205 |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
206 static std::unique_ptr<Aws::Crt::ApiHandle> api_; |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
207 static std::unique_ptr<Aws::SDKOptions> sdkOptions_; |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
208 |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
209 |
1 | 210 IStoragePlugin* AwsS3StoragePluginFactory::CreateStoragePlugin(const OrthancPlugins::OrthancConfiguration& orthancConfig) |
211 { | |
57
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
212 if (sdkOptions_.get() != NULL) |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
213 { |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
214 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, "Cannot initialize twice"); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
215 } |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
216 |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
217 api_.reset(new Aws::Crt::ApiHandle); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
218 |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
219 sdkOptions_.reset(new Aws::SDKOptions); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
220 sdkOptions_->cryptoOptions.initAndCleanupOpenSSL = false; // Done by the Orthanc framework |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
221 sdkOptions_->httpOptions.initAndCleanupCurl = false; // Done by the Orthanc framework |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
222 |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
223 Aws::InitAPI(*sdkOptions_); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
224 |
15 | 225 bool enableLegacyStorageStructure; |
226 | |
1 | 227 if (!orthancConfig.IsSection(PLUGIN_SECTION)) |
228 { | |
229 OrthancPlugins::LogWarning(std::string(GetStoragePluginName()) + " plugin, section missing. Plugin is not enabled."); | |
230 return nullptr; | |
231 } | |
232 | |
233 OrthancPlugins::OrthancConfiguration pluginSection; | |
234 orthancConfig.GetSection(pluginSection, PLUGIN_SECTION); | |
235 | |
15 | 236 if (!BaseStoragePlugin::ReadCommonConfiguration(enableLegacyStorageStructure, pluginSection)) |
237 { | |
238 return nullptr; | |
239 } | |
240 | |
1 | 241 std::string bucketName; |
242 std::string region; | |
243 std::string accessKey; | |
244 std::string secretKey; | |
245 | |
246 if (!pluginSection.LookupStringValue(bucketName, "BucketName")) | |
247 { | |
248 OrthancPlugins::LogError("AwsS3Storage/BucketName configuration missing. Unable to initialize plugin"); | |
249 return nullptr; | |
250 } | |
251 | |
252 if (!pluginSection.LookupStringValue(region, "Region")) | |
253 { | |
254 OrthancPlugins::LogError("AwsS3Storage/Region configuration missing. Unable to initialize plugin"); | |
255 return nullptr; | |
256 } | |
257 | |
57
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
258 const std::string endpoint = pluginSection.GetStringValue("Endpoint", ""); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
259 const unsigned int connectTimeout = pluginSection.GetUnsignedIntegerValue("ConnectTimeout", 30); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
260 const unsigned int requestTimeout = pluginSection.GetUnsignedIntegerValue("RequestTimeout", 1200); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
261 const bool virtualAddressing = pluginSection.GetBooleanValue("VirtualAddressing", true); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
262 const std::string caFile = orthancConfig.GetStringValue("HttpsCACertificates", ""); |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
diff
changeset
|
263 |
1 | 264 try |
265 { | |
266 Aws::SDKOptions options; | |
267 Aws::InitAPI(options); | |
268 | |
269 Aws::Client::ClientConfiguration configuration; | |
57
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
270 |
1 | 271 configuration.region = region.c_str(); |
6
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
272 configuration.scheme = Aws::Http::Scheme::HTTPS; |
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
273 configuration.connectTimeoutMs = connectTimeout * 1000; |
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
274 configuration.requestTimeoutMs = requestTimeout * 1000; |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
diff
changeset
|
275 configuration.httpRequestTimeoutMs = requestTimeout * 1000; |
6
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
276 |
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
277 if (!endpoint.empty()) |
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
278 { |
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
279 configuration.endpointOverride = endpoint.c_str(); |
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
280 } |
393fcf337462
AWS: added 3 configurations: Endpoint, ConnectionTimeout, RequestTimeout
Alain Mazy
parents:
1
diff
changeset
|
281 |
57
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
282 if (!caFile.empty()) |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
283 { |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
284 configuration.caFile = caFile; |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
285 } |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
286 |
53
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
287 if (pluginSection.LookupStringValue(accessKey, "AccessKey") && pluginSection.LookupStringValue(secretKey, "SecretKey")) |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
288 { |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
289 OrthancPlugins::LogInfo("AWS S3 Storage: using credentials from the configuration file"); |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
290 Aws::Auth::AWSCredentials credentials(accessKey.c_str(), secretKey.c_str()); |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
291 |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
292 Aws::S3::S3Client client(credentials, configuration, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, virtualAddressing); |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
293 |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
294 OrthancPlugins::LogInfo("AWS S3 storage initialized"); |
1 | 295 |
53
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
296 return new AwsS3StoragePlugin(client, bucketName, enableLegacyStorageStructure); |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
297 } |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
298 else |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
299 { |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
300 // when using default credentials, credentials are not checked at startup but only the first time you try to access the bucket ! |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
301 OrthancPlugins::LogInfo("AWS S3 Storage: using default credentials provider"); |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
302 Aws::S3::S3Client client(configuration, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, virtualAddressing); |
1 | 303 |
53
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
304 OrthancPlugins::LogInfo("AWS S3 storage initialized"); |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
305 |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
306 return new AwsS3StoragePlugin(client, bucketName, enableLegacyStorageStructure); |
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
307 } |
1 | 308 } |
309 catch (const std::exception& e) | |
310 { | |
53
1c3e34f5c5c6
AWS S3: if no access & secret keys are provided, now getting the credentials from the default credentials manager
Alain Mazy <am@osimis.io>
parents:
41
diff
changeset
|
311 OrthancPlugins::LogError(std::string("AWS S3 Storage plugin: failed to initialize plugin: ") + e.what()); |
1 | 312 return nullptr; |
313 } | |
314 | |
315 } | |
316 | |
57
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
317 |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
318 AwsS3StoragePlugin::~AwsS3StoragePlugin() |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
319 { |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
320 assert(sdkOptions_.get() != NULL); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
321 Aws::ShutdownAPI(*sdkOptions_); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
322 api_.reset(); |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
323 } |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
324 |
ba1be668e475
fix initialization of the aws static library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
325 |
15 | 326 AwsS3StoragePlugin::AwsS3StoragePlugin(const Aws::S3::S3Client& client, const std::string& bucketName, bool enableLegacyStorageStructure) |
327 : BaseStoragePlugin(enableLegacyStorageStructure), | |
328 client_(client), | |
1 | 329 bucketName_(bucketName) |
330 { | |
331 | |
332 } | |
333 | |
334 IStoragePlugin::IWriter* AwsS3StoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
335 { | |
336 return new Writer(client_, bucketName_, GetPath(uuid, type, encryptionEnabled)); | |
337 } | |
338 | |
339 IStoragePlugin::IReader* AwsS3StoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
340 { | |
341 return new Reader(client_, bucketName_, GetPath(uuid, type, encryptionEnabled)); | |
342 } | |
343 | |
344 void AwsS3StoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
345 { | |
346 std::string path = GetPath(uuid, type, encryptionEnabled); | |
347 | |
348 Aws::S3::Model::DeleteObjectRequest deleteObjectRequest; | |
349 deleteObjectRequest.SetBucket(bucketName_.c_str()); | |
350 deleteObjectRequest.SetKey(path.c_str()); | |
351 | |
352 auto result = client_.DeleteObject(deleteObjectRequest); | |
353 | |
354 if (!result.IsSuccess()) | |
355 { | |
356 throw StoragePluginException(std::string("error while deleting file ") + path + ": " + result.GetError().GetExceptionName().c_str() + " " + result.GetError().GetMessage().c_str()); | |
357 } | |
358 | |
359 } |