comparison Aws/AwsS3StoragePlugin.cpp @ 15:2a02b21f0a19

migration + storage structure
author Alain Mazy
date Tue, 01 Sep 2020 13:08:49 +0200
parents 393fcf337462
children e1f52b851827 8d2b29fd4de5
comparison
equal deleted inserted replaced
14:234946ea2038 15:2a02b21f0a19
30 #include <boost/lexical_cast.hpp> 30 #include <boost/lexical_cast.hpp>
31 #include <iostream> 31 #include <iostream>
32 #include <fstream> 32 #include <fstream>
33 33
34 const char* ALLOCATION_TAG = "OrthancS3"; 34 const char* ALLOCATION_TAG = "OrthancS3";
35 35 static const char* const PLUGIN_SECTION = "AwsS3Storage";
36 class AwsS3StoragePlugin : public IStoragePlugin 36
37 class AwsS3StoragePlugin : public BaseStoragePlugin
37 { 38 {
38 public: 39 public:
39 40
40 Aws::S3::S3Client client_; 41 Aws::S3::S3Client client_;
41 std::string bucketName_; 42 std::string bucketName_;
42 43
43 public: 44 public:
44 45
45 AwsS3StoragePlugin(const Aws::S3::S3Client& client, const std::string& bucketName); 46 AwsS3StoragePlugin(const Aws::S3::S3Client& client, const std::string& bucketName, bool enableLegacyStorageStructure);
46 47
47 virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); 48 virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
48 virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); 49 virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
49 virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); 50 virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
50 private: 51 virtual const char* GetConfigurationSectionName() {return PLUGIN_SECTION;};
51 virtual std::string GetPath(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
52 }; 52 };
53 53
54 54
55 class Writer : public IStoragePlugin::IWriter 55 class Writer : public IStoragePlugin::IWriter
56 { 56 {
176 return "AWS S3 Storage"; 176 return "AWS S3 Storage";
177 } 177 }
178 178
179 IStoragePlugin* AwsS3StoragePluginFactory::CreateStoragePlugin(const OrthancPlugins::OrthancConfiguration& orthancConfig) 179 IStoragePlugin* AwsS3StoragePluginFactory::CreateStoragePlugin(const OrthancPlugins::OrthancConfiguration& orthancConfig)
180 { 180 {
181 static const char* const PLUGIN_SECTION = "AwsS3Storage"; 181 bool enableLegacyStorageStructure;
182
182 if (!orthancConfig.IsSection(PLUGIN_SECTION)) 183 if (!orthancConfig.IsSection(PLUGIN_SECTION))
183 { 184 {
184 OrthancPlugins::LogWarning(std::string(GetStoragePluginName()) + " plugin, section missing. Plugin is not enabled."); 185 OrthancPlugins::LogWarning(std::string(GetStoragePluginName()) + " plugin, section missing. Plugin is not enabled.");
185 return nullptr; 186 return nullptr;
186 } 187 }
187 188
188 OrthancPlugins::OrthancConfiguration pluginSection; 189 OrthancPlugins::OrthancConfiguration pluginSection;
189 orthancConfig.GetSection(pluginSection, PLUGIN_SECTION); 190 orthancConfig.GetSection(pluginSection, PLUGIN_SECTION);
191
192 if (!BaseStoragePlugin::ReadCommonConfiguration(enableLegacyStorageStructure, pluginSection))
193 {
194 return nullptr;
195 }
190 196
191 std::string bucketName; 197 std::string bucketName;
192 std::string region; 198 std::string region;
193 std::string accessKey; 199 std::string accessKey;
194 std::string secretKey; 200 std::string secretKey;
240 246
241 Aws::S3::S3Client client(credentials, configuration); 247 Aws::S3::S3Client client(credentials, configuration);
242 248
243 OrthancPlugins::LogInfo("AWS S3 storage initialized"); 249 OrthancPlugins::LogInfo("AWS S3 storage initialized");
244 250
245 return new AwsS3StoragePlugin(client, bucketName); 251 return new AwsS3StoragePlugin(client, bucketName, enableLegacyStorageStructure);
246 } 252 }
247 catch (const std::exception& e) 253 catch (const std::exception& e)
248 { 254 {
249 OrthancPlugins::LogError(std::string("AzureBlobStorage plugin: failed to initialize plugin: ") + e.what()); 255 OrthancPlugins::LogError(std::string("AzureBlobStorage plugin: failed to initialize plugin: ") + e.what());
250 return nullptr; 256 return nullptr;
251 } 257 }
252 258
253 } 259 }
254 260
255 AwsS3StoragePlugin::AwsS3StoragePlugin(const Aws::S3::S3Client& client, const std::string& bucketName) 261 AwsS3StoragePlugin::AwsS3StoragePlugin(const Aws::S3::S3Client& client, const std::string& bucketName, bool enableLegacyStorageStructure)
256 : client_(client), 262 : BaseStoragePlugin(enableLegacyStorageStructure),
263 client_(client),
257 bucketName_(bucketName) 264 bucketName_(bucketName)
258 { 265 {
259 266
260 } 267 }
261 268
283 { 290 {
284 throw StoragePluginException(std::string("error while deleting file ") + path + ": " + result.GetError().GetExceptionName().c_str() + " " + result.GetError().GetMessage().c_str()); 291 throw StoragePluginException(std::string("error while deleting file ") + path + ": " + result.GetError().GetExceptionName().c_str() + " " + result.GetError().GetMessage().c_str());
285 } 292 }
286 293
287 } 294 }
288
289 std::string AwsS3StoragePlugin::GetPath(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
290 {
291 std::string path = std::string(uuid);
292
293 if (type == OrthancPluginContentType_Dicom)
294 {
295 path += ".dcm";
296 }
297 else if (type == OrthancPluginContentType_DicomAsJson)
298 {
299 path += ".json";
300 }
301 else
302 {
303 path += ".unk";
304 }
305
306 if (encryptionEnabled)
307 {
308 path += ".enc";
309 }
310 return path;
311 }