Mercurial > hg > orthanc-object-storage
annotate Azure/AzureBlobStoragePlugin.cpp @ 94:1bc055199cd2
Added a description for all plugins
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 19 Dec 2022 09:52:58 +0100 |
parents | 9b2a76fe987a |
children | f4e05641c108 |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
37
f55b2afdf53d
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
34
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 | |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
48
diff
changeset
|
19 |
1 | 20 #include "AzureBlobStoragePlugin.h" |
21 | |
22 #include <was/storage_account.h> | |
23 #include <was/blob.h> | |
24 #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
|
25 #include <boost/algorithm/string.hpp> |
1 | 26 #include "cpprest/rawptrstream.h" |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
27 #include "cpprest/details/basic_types.h" |
1 | 28 |
29 // Create aliases to make the code easier to read. | |
30 namespace as = azure::storage; | |
31 | |
78 | 32 class AzureBlobStoragePlugin : public BaseStorage |
1 | 33 { |
34 public: | |
35 | |
36 as::cloud_blob_client blobClient_; | |
37 as::cloud_blob_container blobContainer_; | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
38 bool storageContainsUnknownFiles_; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
39 |
1 | 40 public: |
41 | |
79 | 42 AzureBlobStoragePlugin(const std::string& nameForLogs, |
43 const as::cloud_blob_client& blobClient, | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
44 const as::cloud_blob_container& blobContainer, |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
45 bool enableLegacyStorageStructure, |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
46 bool storageContainsUnknownFiles |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
47 ); |
1 | 48 |
49 virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); | |
50 virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); | |
51 virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); | |
52 }; | |
53 | |
54 | |
78 | 55 class Writer : public IStorage::IWriter |
1 | 56 { |
57 std::string path_; | |
58 as::cloud_blob_client client_; | |
59 as::cloud_blob_container container_; | |
60 | |
61 public: | |
62 Writer(const as::cloud_blob_container& container, const std::string& path, const as::cloud_blob_client& client) | |
63 : path_(path), | |
64 client_(client), | |
65 container_(container) | |
66 { | |
67 } | |
68 | |
69 virtual ~Writer() | |
70 { | |
71 } | |
72 | |
73 virtual void Write(const char* data, size_t size) | |
74 { | |
75 try | |
76 { | |
77 concurrency::streams::istream inputStream = concurrency::streams::rawptr_stream<uint8_t>::open_istream(reinterpret_cast<const uint8_t*>(data), size); | |
34 | 78 azure::storage::cloud_block_blob blob = container_.get_block_blob_reference(utility::conversions::to_string_t(path_)); |
1 | 79 blob.upload_from_stream(inputStream); |
80 inputStream.close().wait(); | |
81 } | |
82 catch (std::exception& ex) | |
83 { | |
84 throw StoragePluginException("AzureBlobStorage: error writing file " + std::string(path_) + ": " + ex.what()); | |
85 } | |
86 } | |
87 }; | |
88 | |
89 | |
78 | 90 class Reader : public IStorage::IReader |
1 | 91 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
92 std::string path_; |
1 | 93 as::cloud_blob_client client_; |
94 as::cloud_blob_container container_; | |
95 as::cloud_block_blob block_; | |
96 | |
97 public: | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
98 Reader(const as::cloud_blob_container& container, const std::list<std::string>& paths, const as::cloud_blob_client& client) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
99 : client_(client), |
1 | 100 container_(container) |
101 { | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
102 std::string firstExceptionMessage; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
103 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
104 for (auto& path: paths) |
1 | 105 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
106 try |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
107 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
108 block_ = container_.get_block_blob_reference(utility::conversions::to_string_t(path)); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
109 block_.download_attributes(); // to retrieve the properties |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
110 path_ = path; |
71 | 111 return; |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
112 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
113 catch (std::exception& ex) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
114 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
115 if (firstExceptionMessage.empty()) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
116 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
117 firstExceptionMessage = "AzureBlobStorage: error opening file for reading " + std::string(path) + ": " + ex.what(); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
118 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
119 //ignore to retry |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
120 } |
1 | 121 } |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
122 throw StoragePluginException(firstExceptionMessage); |
1 | 123 } |
124 | |
125 virtual ~Reader() | |
126 { | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
127 } |
1 | 128 |
129 virtual size_t GetSize() | |
130 { | |
131 try | |
132 { | |
133 return block_.properties().size(); | |
134 } | |
135 catch (std::exception& ex) | |
136 { | |
137 throw StoragePluginException("AzureBlobStorage: error while reading file " + std::string(path_) + ": " + ex.what()); | |
138 } | |
139 } | |
140 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
141 virtual void ReadWhole(char* data, size_t size) |
1 | 142 { |
143 try | |
144 { | |
145 concurrency::streams::ostream outputStream = concurrency::streams::rawptr_stream<uint8_t>::open_ostream(reinterpret_cast<uint8_t*>(data), size); | |
146 | |
147 block_.download_to_stream(outputStream); | |
148 } | |
149 catch (std::exception& ex) | |
150 { | |
151 throw StoragePluginException("AzureBlobStorage: error while reading file " + std::string(path_) + ": " + ex.what()); | |
152 } | |
153 } | |
154 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
155 virtual void ReadRange(char* data, size_t size, size_t fromOffset) |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
156 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
157 try |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
158 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
159 concurrency::streams::ostream outputStream = concurrency::streams::rawptr_stream<uint8_t>::open_ostream(reinterpret_cast<uint8_t*>(data), size); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
160 |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
161 block_.download_range_to_stream(outputStream, fromOffset, size); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
162 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
163 catch (std::exception& ex) |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
164 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
165 throw StoragePluginException("AzureBlobStorage: error while reading partial file " + std::string(path_) + ": " + ex.what()); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
166 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
167 } |
1 | 168 }; |
169 | |
170 | |
171 | |
172 const char* AzureBlobStoragePluginFactory::GetStoragePluginName() | |
173 { | |
174 return "Azure Blob Storage"; | |
175 } | |
176 | |
94
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
92
diff
changeset
|
177 const char* AzureBlobStoragePluginFactory::GetStorageDescription() |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
92
diff
changeset
|
178 { |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
92
diff
changeset
|
179 return "Stores the Orthanc storage area in Azure Blob"; |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
92
diff
changeset
|
180 } |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
92
diff
changeset
|
181 |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
92
diff
changeset
|
182 |
48 | 183 bool IsSasTokenAccountLevel(utility::string_t sasToken) |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
184 { |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
185 // Use cpprestsdk's utility::string_t here since the expected argument is the return value of |
89 | 186 // as::storage_credentials::sas_token(), which is type utility::string_t (which is a std::wstring on Windows and a std::string on Linux) |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
187 size_t newIdx = 0; |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
188 size_t prevIdx = 0; |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
189 while ((newIdx = sasToken.find('&', prevIdx)) != utility::string_t::npos) |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
190 { |
48 | 191 utility::string_t kvpair = sasToken.substr(prevIdx, newIdx - prevIdx); |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
192 prevIdx = newIdx + 1; // start next time from char after '&' |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
193 |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
194 size_t equalsIdx = kvpair.find('='); |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
195 utility::string_t key = kvpair.substr(0, equalsIdx); |
89 | 196 #ifdef WIN32 |
197 const wchar_t* srt = L"srt"; | |
198 #else | |
199 const char* srt = "srt"; | |
200 #endif | |
201 if (key == srt) // only account SAS has this parameter | |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
202 return true; |
89 | 203 |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
204 } |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
205 |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
206 return false; |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
207 } |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
208 |
79 | 209 IStorage* AzureBlobStoragePluginFactory::CreateStorage(const std::string& nameForLogs, const OrthancPlugins::OrthancConfiguration& orthancConfig) |
1 | 210 { |
211 std::string connectionString; | |
212 std::string containerName; | |
15 | 213 bool enableLegacyStorageStructure; |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
214 bool storageContainsUnknownFiles; |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
215 bool createContainerIfNotExists; |
1 | 216 |
81 | 217 if (orthancConfig.IsSection(GetConfigurationSectionName())) |
1 | 218 { |
219 OrthancPlugins::OrthancConfiguration pluginSection; | |
81 | 220 orthancConfig.GetSection(pluginSection, GetConfigurationSectionName()); |
1 | 221 |
78 | 222 if (!BaseStorage::ReadCommonConfiguration(enableLegacyStorageStructure, storageContainsUnknownFiles, pluginSection)) |
15 | 223 { |
224 return nullptr; | |
225 } | |
226 | |
1 | 227 if (!pluginSection.LookupStringValue(connectionString, "ConnectionString")) |
228 { | |
229 OrthancPlugins::LogError("AzureBlobStorage/ConnectionString configuration missing. Unable to initialize plugin"); | |
230 return nullptr; | |
231 } | |
232 | |
233 if (!pluginSection.LookupStringValue(containerName, "ContainerName")) | |
234 { | |
235 OrthancPlugins::LogError("AzureBlobStorage/ContainerName configuration missing. Unable to initialize plugin"); | |
236 return nullptr; | |
237 } | |
238 | |
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
|
239 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
|
240 boost::trim(containerName); |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
241 |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
242 createContainerIfNotExists = pluginSection.GetBooleanValue("CreateContainerIfNotExists", true); |
1 | 243 } |
244 else if (orthancConfig.IsSection("BlobStorage")) // backward compatibility with the old plugin: | |
245 { | |
246 OrthancPlugins::LogWarning("AzureBlobStorage: you're using an old configuration format for the plugin."); | |
247 | |
248 OrthancPlugins::OrthancConfiguration pluginSection; | |
249 orthancConfig.GetSection(pluginSection, "BlobStorage"); | |
250 | |
251 std::string accountName; | |
252 std::string accountKey; | |
253 | |
254 if (!pluginSection.LookupStringValue(containerName, "ContainerName")) | |
255 { | |
256 OrthancPlugins::LogError("BlobStorage/AccountName configuration missing. Unable to initialize plugin"); | |
257 return nullptr; | |
258 } | |
259 | |
260 if (!pluginSection.LookupStringValue(accountName, "AccountName")) | |
261 { | |
262 OrthancPlugins::LogError("BlobStorage/AccountName configuration missing. Unable to initialize plugin"); | |
263 return nullptr; | |
264 } | |
265 | |
266 if (!pluginSection.LookupStringValue(accountKey, "AccountKey")) | |
267 { | |
268 OrthancPlugins::LogError("BlobStorage/ContainerName configuration missing. Unable to initialize plugin"); | |
269 return nullptr; | |
270 } | |
271 | |
272 std::ostringstream connectionStringBuilder; | |
273 connectionStringBuilder << "DefaultEndpointsProtocol=https;AccountName=" << accountName << ";AccountKey=" << accountKey; | |
274 connectionString = connectionStringBuilder.str(); | |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
275 |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
276 createContainerIfNotExists = pluginSection.GetBooleanValue("CreateContainerIfNotExists", true); |
1 | 277 } |
278 else | |
279 { | |
280 OrthancPlugins::LogWarning(std::string(GetStoragePluginName()) + " plugin, section missing. Plugin is not enabled."); | |
281 return nullptr; | |
282 } | |
283 | |
284 try | |
285 { | |
11 | 286 OrthancPlugins::LogInfo("Connecting to Azure storage ..."); |
287 | |
34 | 288 as::cloud_storage_account storageAccount = as::cloud_storage_account::parse(utility::conversions::to_string_t(connectionString)); |
11 | 289 OrthancPlugins::LogInfo("Storage account created"); |
1 | 290 |
291 as::cloud_blob_client blobClient = storageAccount.create_cloud_blob_client(); | |
11 | 292 OrthancPlugins::LogInfo("Blob client created"); |
293 | |
34 | 294 as::cloud_blob_container blobContainer = blobClient.get_container_reference(utility::conversions::to_string_t(containerName)); |
11 | 295 OrthancPlugins::LogInfo("Accessing blob container"); |
1 | 296 |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
297 // blobContainer.create_if_not_exists() throws an error if a service SAS (for a blob container) |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
298 // was used in the connectionString. |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
299 // Only allow the use of this function when using storage account-level credentials, whether |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
300 // through accountName/accountKey combo or account SAS. |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
301 if ((storageAccount.credentials().is_account_key() || |
48 | 302 (storageAccount.credentials().is_sas() && IsSasTokenAccountLevel(storageAccount.credentials().sas_token()))) |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
303 && createContainerIfNotExists) |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
304 { |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
305 // Return value is true if the container did not exist and was successfully created. |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
306 bool containerCreated = blobContainer.create_if_not_exists(); |
1 | 307 |
46
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
308 if (containerCreated) |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
309 { |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
310 OrthancPlugins::LogWarning("Blob Storage Area container has been created. **** check in the Azure console that your container is private ****"); |
3b8fab63313d
Add "CreateContainerIfNotExists" config for Azure
Mark Poscablo <Mark.Poscablo@varian.com>
parents:
15
diff
changeset
|
311 } |
1 | 312 } |
313 | |
314 OrthancPlugins::LogInfo("Blob storage initialized"); | |
315 | |
79 | 316 return new AzureBlobStoragePlugin(nameForLogs, blobClient, blobContainer, enableLegacyStorageStructure, storageContainsUnknownFiles); |
1 | 317 } |
318 catch (const std::exception& e) | |
319 { | |
320 OrthancPlugins::LogError(std::string("AzureBlobStorage plugin: failed to initialize plugin: ") + e.what()); | |
321 return nullptr; | |
322 } | |
323 | |
324 } | |
325 | |
79 | 326 AzureBlobStoragePlugin::AzureBlobStoragePlugin(const std::string& nameForLogs, const as::cloud_blob_client& blobClient, const as::cloud_blob_container& blobContainer, bool enableLegacyStorageStructure, bool storageContainsUnknownFiles) |
327 : BaseStorage(nameForLogs, enableLegacyStorageStructure), | |
15 | 328 blobClient_(blobClient), |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
329 blobContainer_(blobContainer), |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
330 storageContainsUnknownFiles_(storageContainsUnknownFiles) |
1 | 331 { |
332 | |
333 } | |
334 | |
78 | 335 IStorage::IWriter* AzureBlobStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
1 | 336 { |
337 return new Writer(blobContainer_, GetPath(uuid, type, encryptionEnabled), blobClient_); | |
338 } | |
339 | |
78 | 340 IStorage::IReader* AzureBlobStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
1 | 341 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
342 std::list<std::string> paths; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
343 paths.push_back(GetPath(uuid, type, encryptionEnabled, false)); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
344 if (storageContainsUnknownFiles_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
345 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
346 paths.push_back(GetPath(uuid, type, encryptionEnabled, true)); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
347 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
348 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
349 return new Reader(blobContainer_, paths, blobClient_); |
1 | 350 } |
351 | |
352 void AzureBlobStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
353 { | |
354 std::string path = GetPath(uuid, type, encryptionEnabled); | |
355 | |
356 try | |
357 { | |
34 | 358 as::cloud_block_blob blockBlob = blobContainer_.get_block_blob_reference(utility::conversions::to_string_t(path)); |
1 | 359 |
360 blockBlob.delete_blob(); | |
361 } | |
362 catch (std::exception& ex) | |
363 { | |
364 throw StoragePluginException("AzureBlobStorage: error while deleting file " + std::string(path) + ": " + ex.what()); | |
365 } | |
366 } |