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