Mercurial > hg > orthanc-object-storage
annotate Azure/AzureBlobStoragePlugin.cpp @ 34:7ddd840563c9
windows build for Azure plugin
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Tue, 10 Nov 2020 14:14:24 +0100 |
parents | 2a02b21f0a19 |
children | f55b2afdf53d |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
3 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
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 "AzureBlobStoragePlugin.h" | |
20 | |
21 #include <was/storage_account.h> | |
22 #include <was/blob.h> | |
23 #include <boost/lexical_cast.hpp> | |
12
a203d0a4fd8f
Azure: now removing spaces and CR at the end of the connectionString since it prevented the plugin to initialize.
Alain Mazy
parents:
11
diff
changeset
|
24 #include <boost/algorithm/string.hpp> |
1 | 25 #include "cpprest/rawptrstream.h" |
26 | |
27 | |
28 // Create aliases to make the code easier to read. | |
29 namespace as = azure::storage; | |
15 | 30 static const char* const PLUGIN_SECTION = "AzureBlobStorage"; |
1 | 31 |
15 | 32 class AzureBlobStoragePlugin : public BaseStoragePlugin |
1 | 33 { |
34 public: | |
35 | |
36 as::cloud_blob_client blobClient_; | |
37 as::cloud_blob_container blobContainer_; | |
38 public: | |
39 | |
40 // AzureBlobStoragePlugin(const std::string& connectionString, | |
41 // const std::string& containerName | |
42 // ); | |
15 | 43 AzureBlobStoragePlugin(const as::cloud_blob_client& blobClient, const as::cloud_blob_container& blobContainer, bool enableLegacyStorageStructure); |
1 | 44 |
45 virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); | |
46 virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); | |
47 virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); | |
15 | 48 virtual const char* GetConfigurationSectionName() {return PLUGIN_SECTION;}; |
1 | 49 }; |
50 | |
51 | |
52 class Writer : public IStoragePlugin::IWriter | |
53 { | |
54 std::string path_; | |
55 as::cloud_blob_client client_; | |
56 as::cloud_blob_container container_; | |
57 | |
58 public: | |
59 Writer(const as::cloud_blob_container& container, const std::string& path, const as::cloud_blob_client& client) | |
60 : path_(path), | |
61 client_(client), | |
62 container_(container) | |
63 { | |
64 } | |
65 | |
66 virtual ~Writer() | |
67 { | |
68 } | |
69 | |
70 virtual void Write(const char* data, size_t size) | |
71 { | |
72 try | |
73 { | |
74 concurrency::streams::istream inputStream = concurrency::streams::rawptr_stream<uint8_t>::open_istream(reinterpret_cast<const uint8_t*>(data), size); | |
34 | 75 azure::storage::cloud_block_blob blob = container_.get_block_blob_reference(utility::conversions::to_string_t(path_)); |
1 | 76 blob.upload_from_stream(inputStream); |
77 inputStream.close().wait(); | |
78 } | |
79 catch (std::exception& ex) | |
80 { | |
81 throw StoragePluginException("AzureBlobStorage: error writing file " + std::string(path_) + ": " + ex.what()); | |
82 } | |
83 } | |
84 }; | |
85 | |
86 | |
87 class Reader : public IStoragePlugin::IReader | |
88 { | |
89 std::string path_; | |
90 as::cloud_blob_client client_; | |
91 as::cloud_blob_container container_; | |
92 as::cloud_block_blob block_; | |
93 | |
94 public: | |
95 Reader(const as::cloud_blob_container& container, const std::string& path, const as::cloud_blob_client& client) | |
96 : path_(path), | |
97 client_(client), | |
98 container_(container) | |
99 { | |
100 try | |
101 { | |
34 | 102 block_ = container_.get_block_blob_reference(utility::conversions::to_string_t(path_)); |
1 | 103 block_.download_attributes(); // to retrieve the properties |
104 } | |
105 catch (std::exception& ex) | |
106 { | |
107 throw StoragePluginException("AzureBlobStorage: error opening file for reading " + std::string(path_) + ": " + ex.what()); | |
108 } | |
109 } | |
110 | |
111 virtual ~Reader() | |
112 { | |
113 | |
114 } | |
115 virtual size_t GetSize() | |
116 { | |
117 try | |
118 { | |
119 return block_.properties().size(); | |
120 } | |
121 catch (std::exception& ex) | |
122 { | |
123 throw StoragePluginException("AzureBlobStorage: error while reading file " + std::string(path_) + ": " + ex.what()); | |
124 } | |
125 } | |
126 | |
127 virtual void Read(char* data, size_t size) | |
128 { | |
129 try | |
130 { | |
131 concurrency::streams::ostream outputStream = concurrency::streams::rawptr_stream<uint8_t>::open_ostream(reinterpret_cast<uint8_t*>(data), size); | |
132 | |
133 block_.download_to_stream(outputStream); | |
134 } | |
135 catch (std::exception& ex) | |
136 { | |
137 throw StoragePluginException("AzureBlobStorage: error while reading file " + std::string(path_) + ": " + ex.what()); | |
138 } | |
139 } | |
140 | |
141 }; | |
142 | |
143 | |
144 | |
145 const char* AzureBlobStoragePluginFactory::GetStoragePluginName() | |
146 { | |
147 return "Azure Blob Storage"; | |
148 } | |
149 | |
150 IStoragePlugin* AzureBlobStoragePluginFactory::CreateStoragePlugin(const OrthancPlugins::OrthancConfiguration& orthancConfig) | |
151 { | |
152 std::string connectionString; | |
153 std::string containerName; | |
15 | 154 bool enableLegacyStorageStructure; |
1 | 155 |
156 if (orthancConfig.IsSection(PLUGIN_SECTION)) | |
157 { | |
158 OrthancPlugins::OrthancConfiguration pluginSection; | |
159 orthancConfig.GetSection(pluginSection, PLUGIN_SECTION); | |
160 | |
15 | 161 if (!BaseStoragePlugin::ReadCommonConfiguration(enableLegacyStorageStructure, pluginSection)) |
162 { | |
163 return nullptr; | |
164 } | |
165 | |
1 | 166 if (!pluginSection.LookupStringValue(connectionString, "ConnectionString")) |
167 { | |
168 OrthancPlugins::LogError("AzureBlobStorage/ConnectionString configuration missing. Unable to initialize plugin"); | |
169 return nullptr; | |
170 } | |
171 | |
172 if (!pluginSection.LookupStringValue(containerName, "ContainerName")) | |
173 { | |
174 OrthancPlugins::LogError("AzureBlobStorage/ContainerName configuration missing. Unable to initialize plugin"); | |
175 return nullptr; | |
176 } | |
177 | |
12
a203d0a4fd8f
Azure: now removing spaces and CR at the end of the connectionString since it prevented the plugin to initialize.
Alain Mazy
parents:
11
diff
changeset
|
178 boost::trim(connectionString); // without that, if there's an EOL in the string, it fails with "provided uri is invalid" |
a203d0a4fd8f
Azure: now removing spaces and CR at the end of the connectionString since it prevented the plugin to initialize.
Alain Mazy
parents:
11
diff
changeset
|
179 boost::trim(containerName); |
1 | 180 } |
181 else if (orthancConfig.IsSection("BlobStorage")) // backward compatibility with the old plugin: | |
182 { | |
183 OrthancPlugins::LogWarning("AzureBlobStorage: you're using an old configuration format for the plugin."); | |
184 | |
185 OrthancPlugins::OrthancConfiguration pluginSection; | |
186 orthancConfig.GetSection(pluginSection, "BlobStorage"); | |
187 | |
188 std::string accountName; | |
189 std::string accountKey; | |
190 | |
191 if (!pluginSection.LookupStringValue(containerName, "ContainerName")) | |
192 { | |
193 OrthancPlugins::LogError("BlobStorage/AccountName configuration missing. Unable to initialize plugin"); | |
194 return nullptr; | |
195 } | |
196 | |
197 if (!pluginSection.LookupStringValue(accountName, "AccountName")) | |
198 { | |
199 OrthancPlugins::LogError("BlobStorage/AccountName configuration missing. Unable to initialize plugin"); | |
200 return nullptr; | |
201 } | |
202 | |
203 if (!pluginSection.LookupStringValue(accountKey, "AccountKey")) | |
204 { | |
205 OrthancPlugins::LogError("BlobStorage/ContainerName configuration missing. Unable to initialize plugin"); | |
206 return nullptr; | |
207 } | |
208 | |
209 std::ostringstream connectionStringBuilder; | |
210 connectionStringBuilder << "DefaultEndpointsProtocol=https;AccountName=" << accountName << ";AccountKey=" << accountKey; | |
211 connectionString = connectionStringBuilder.str(); | |
212 } | |
213 else | |
214 { | |
215 OrthancPlugins::LogWarning(std::string(GetStoragePluginName()) + " plugin, section missing. Plugin is not enabled."); | |
216 return nullptr; | |
217 } | |
218 | |
219 try | |
220 { | |
11 | 221 OrthancPlugins::LogInfo("Connecting to Azure storage ..."); |
222 | |
34 | 223 as::cloud_storage_account storageAccount = as::cloud_storage_account::parse(utility::conversions::to_string_t(connectionString)); |
11 | 224 OrthancPlugins::LogInfo("Storage account created"); |
1 | 225 |
226 as::cloud_blob_client blobClient = storageAccount.create_cloud_blob_client(); | |
11 | 227 OrthancPlugins::LogInfo("Blob client created"); |
228 | |
34 | 229 as::cloud_blob_container blobContainer = blobClient.get_container_reference(utility::conversions::to_string_t(containerName)); |
11 | 230 OrthancPlugins::LogInfo("Accessing blob container"); |
1 | 231 |
232 // Return value is true if the container did not exist and was successfully created. | |
233 bool containerCreated = blobContainer.create_if_not_exists(); | |
234 | |
235 if (containerCreated) | |
236 { | |
237 OrthancPlugins::LogWarning("Blob Storage Area container has been created. **** check in the Azure console that your container is private ****"); | |
238 } | |
239 | |
240 OrthancPlugins::LogInfo("Blob storage initialized"); | |
241 | |
15 | 242 return new AzureBlobStoragePlugin(blobClient, blobContainer, enableLegacyStorageStructure); |
1 | 243 } |
244 catch (const std::exception& e) | |
245 { | |
246 OrthancPlugins::LogError(std::string("AzureBlobStorage plugin: failed to initialize plugin: ") + e.what()); | |
247 return nullptr; | |
248 } | |
249 | |
250 } | |
251 | |
15 | 252 AzureBlobStoragePlugin::AzureBlobStoragePlugin(const as::cloud_blob_client& blobClient, const as::cloud_blob_container& blobContainer, bool enableLegacyStorageStructure) |
253 : BaseStoragePlugin(enableLegacyStorageStructure), | |
254 blobClient_(blobClient), | |
1 | 255 blobContainer_(blobContainer) |
256 { | |
257 | |
258 } | |
259 | |
260 IStoragePlugin::IWriter* AzureBlobStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
261 { | |
262 return new Writer(blobContainer_, GetPath(uuid, type, encryptionEnabled), blobClient_); | |
263 } | |
264 | |
265 IStoragePlugin::IReader* AzureBlobStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
266 { | |
267 return new Reader(blobContainer_, GetPath(uuid, type, encryptionEnabled), blobClient_); | |
268 } | |
269 | |
270 void AzureBlobStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
271 { | |
272 std::string path = GetPath(uuid, type, encryptionEnabled); | |
273 | |
274 try | |
275 { | |
34 | 276 as::cloud_block_blob blockBlob = blobContainer_.get_block_blob_reference(utility::conversions::to_string_t(path)); |
1 | 277 |
278 blockBlob.delete_blob(); | |
279 } | |
280 catch (std::exception& ex) | |
281 { | |
282 throw StoragePluginException("AzureBlobStorage: error while deleting file " + std::string(path) + ": " + ex.what()); | |
283 } | |
284 } |