Mercurial > hg > orthanc-object-storage
annotate Google/GoogleStoragePlugin.cpp @ 155:cf1c7c4441b4
cleaning
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 22 Jun 2024 17:12:20 +0200 |
parents | 6dd8bb916573 |
children | 80657f326c68 |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
145
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
111
diff
changeset
|
3 * Copyright (C) 2020-2023 Osimis S.A., Belgium |
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
111
diff
changeset
|
4 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
111
diff
changeset
|
5 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
40
diff
changeset
|
21 |
1 | 22 #include "GoogleStoragePlugin.h" |
23 | |
24 #include "google/cloud/storage/client.h" | |
25 | |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
26 #include <Logging.h> |
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
27 |
1 | 28 // Create aliases to make the code easier to read. |
29 namespace gcs = google::cloud::storage; | |
15 | 30 |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
31 |
78 | 32 class GoogleStoragePlugin : public BaseStorage |
15 | 33 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
34 public: |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
35 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
36 std::string bucketName_; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
37 google::cloud::storage::Client mainClient_; // the client that is created at startup. Each thread should copy it when it needs it. (from the doc: Instances of this class created via copy-construction or copy-assignment share the underlying pool of connections. Access to these copies via multiple threads is guaranteed to work. Two threads operating on the same instance of this class is not guaranteed to work.) |
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 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
40 public: |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
41 |
79 | 42 GoogleStoragePlugin(const std::string& nameForLogs, |
43 const std::string& bucketName, | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
44 google::cloud::storage::Client& mainClient, |
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 ); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
48 |
153 | 49 virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) ORTHANC_OVERRIDE; |
50 virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) ORTHANC_OVERRIDE; | |
51 virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) ORTHANC_OVERRIDE; | |
52 virtual bool HasFileExists() ORTHANC_OVERRIDE {return false;}; | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
53 }; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
54 |
1 | 55 |
78 | 56 class Writer : public IStorage::IWriter |
1 | 57 { |
58 std::string path_; | |
59 gcs::Client client_; | |
60 std::string bucketName_; | |
61 gcs::ObjectWriteStream stream_; | |
62 public: | |
63 Writer(const std::string& bucketName, const std::string& path, gcs::Client& client) | |
64 : path_(path), | |
65 client_(client), | |
66 bucketName_(bucketName) | |
67 { | |
68 } | |
69 | |
70 virtual ~Writer() | |
71 { | |
72 } | |
73 | |
153 | 74 virtual void Write(const char* data, size_t size) ORTHANC_OVERRIDE |
1 | 75 { |
76 stream_ = client_.WriteObject(bucketName_, path_); | |
77 | |
78 if (stream_) | |
79 { | |
80 stream_.write(data, size); | |
81 stream_.Close(); | |
82 | |
83 if (!stream_.metadata()) | |
84 { | |
85 throw StoragePluginException("GoogleCloudStorage: error while writing file " + std::string(path_) + ": " + stream_.metadata().status().message()); | |
86 } | |
87 } | |
88 else | |
89 { | |
90 throw StoragePluginException("GoogleCloudStorage: error while opening/writing file " + std::string(path_) + ": " + stream_.metadata().status().message()); | |
91 } | |
92 } | |
93 }; | |
94 | |
95 | |
78 | 96 class Reader : public IStorage::IReader |
1 | 97 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
98 std::list<std::string> paths_; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
99 gcs::Client client_; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
100 std::string bucketName_; |
1 | 101 |
102 public: | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
103 Reader(const std::string& bucketName, const std::list<std::string>& paths, gcs::Client& client) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
104 : paths_(paths), |
1 | 105 client_(client), |
106 bucketName_(bucketName) | |
107 { | |
108 } | |
109 | |
110 virtual ~Reader() | |
111 { | |
112 | |
113 } | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
114 |
153 | 115 virtual size_t GetSize() ORTHANC_OVERRIDE |
1 | 116 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
117 std::string firstExceptionMessage; |
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 for (auto& path: paths_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
120 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
121 try |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
122 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
123 return _GetSize(path); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
124 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
125 catch (StoragePluginException& ex) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
126 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
127 if (firstExceptionMessage.empty()) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
128 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
129 firstExceptionMessage = ex.what(); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
130 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
131 //ignore to retry |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
132 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
133 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
134 throw StoragePluginException(firstExceptionMessage); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
135 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
136 |
153 | 137 virtual void ReadWhole(char* data, size_t size) ORTHANC_OVERRIDE |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
138 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
139 std::string firstExceptionMessage; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
140 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
141 for (auto& path: paths_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
142 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
143 try |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
144 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
145 return _ReadWhole(path, data, size); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
146 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
147 catch (StoragePluginException& ex) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
148 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
149 if (firstExceptionMessage.empty()) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
150 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
151 firstExceptionMessage = ex.what(); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
152 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
153 //ignore to retry |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
154 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
155 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
156 throw StoragePluginException(firstExceptionMessage); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
157 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
158 |
153 | 159 virtual void ReadRange(char* data, size_t size, size_t fromOffset) ORTHANC_OVERRIDE |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
160 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
161 std::string firstExceptionMessage; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
162 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
163 for (auto& path: paths_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
164 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
165 try |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
166 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
167 return _ReadRange(path, data, size, fromOffset); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
168 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
169 catch (StoragePluginException& ex) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
170 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
171 if (firstExceptionMessage.empty()) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
172 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
173 firstExceptionMessage = ex.what(); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
174 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
175 //ignore to retry |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
176 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
177 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
178 throw StoragePluginException(firstExceptionMessage); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
179 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
180 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
181 private: |
153 | 182 virtual void _ReadWhole(const std::string& path, char* data, size_t size) ORTHANC_OVERRIDE |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
183 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
184 auto reader = client_.ReadObject(bucketName_, path); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
185 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
186 if (!reader) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
187 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
188 throw StoragePluginException("error while opening/reading file " + std::string(path) + ": " + reader.status().message()); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
189 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
190 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
191 reader.read(data, size); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
192 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
193 if (!reader) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
194 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
195 throw StoragePluginException("error while reading file " + std::string(path) + ": " + reader.status().message()); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
196 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
197 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
198 |
153 | 199 virtual void _ReadRange(const std::string& path, char* data, size_t size, size_t fromOffset) ORTHANC_OVERRIDE |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
200 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
201 auto reader = client_.ReadObject(bucketName_, path, gcs::ReadRange(fromOffset, fromOffset + size)); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
202 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
203 if (!reader) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
204 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
205 throw StoragePluginException("error while opening/reading file " + std::string(path) + ": " + reader.status().message()); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
206 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
207 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
208 reader.read(data, size); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
209 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
210 if (!reader) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
211 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
212 throw StoragePluginException("error while reading file " + std::string(path) + ": " + reader.status().message()); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
213 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
214 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
215 |
153 | 216 virtual size_t _GetSize(const std::string& path) ORTHANC_OVERRIDE |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
217 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
218 auto objectMetadata = client_.GetObjectMetadata(bucketName_, path); |
1 | 219 |
220 if (objectMetadata) | |
221 { | |
222 std::uint64_t fileSize = static_cast<int64_t>(objectMetadata->size()); | |
223 | |
224 return fileSize; | |
225 } | |
226 else | |
227 { | |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
228 throw StoragePluginException("error while getting the size of " + std::string(path) + ": " + objectMetadata.status().message()); |
1 | 229 } |
230 } | |
231 | |
232 }; | |
233 | |
234 | |
235 | |
236 const char* GoogleStoragePluginFactory::GetStoragePluginName() | |
237 { | |
238 return "Google Cloud Storage"; | |
239 } | |
240 | |
94
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
82
diff
changeset
|
241 const char* GoogleStoragePluginFactory::GetStorageDescription() |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
82
diff
changeset
|
242 { |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
82
diff
changeset
|
243 return "Stores the Orthanc storage area in Google Cloud"; |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
82
diff
changeset
|
244 } |
1bc055199cd2
Added a description for all plugins
Alain Mazy <am@osimis.io>
parents:
82
diff
changeset
|
245 |
79 | 246 IStorage* GoogleStoragePluginFactory::CreateStorage(const std::string& nameForLogs, const OrthancPlugins::OrthancConfiguration& orthancConfig) |
1 | 247 { |
15 | 248 bool enableLegacyStorageStructure; |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
249 bool storageContainsUnknownFiles; |
15 | 250 |
81 | 251 if (!orthancConfig.IsSection(GetConfigurationSectionName())) |
1 | 252 { |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
253 LOG(WARNING) << GetStoragePluginName() << " plugin, section missing. Plugin is not enabled."; |
1 | 254 return nullptr; |
255 } | |
256 | |
257 OrthancPlugins::OrthancConfiguration pluginSection; | |
81 | 258 orthancConfig.GetSection(pluginSection, GetConfigurationSectionName()); |
1 | 259 |
78 | 260 if (!BaseStorage::ReadCommonConfiguration(enableLegacyStorageStructure, storageContainsUnknownFiles, pluginSection)) |
15 | 261 { |
262 return nullptr; | |
263 } | |
264 | |
1 | 265 std::string pathToGoogleCredentials; |
266 | |
267 if (!pluginSection.LookupStringValue(pathToGoogleCredentials, "ServiceAccountFile")) | |
268 { | |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
269 LOG(ERROR) << "GoogleCloudStorage/ServiceAccountFile configuration missing. Unable to initialize plugin"; |
1 | 270 return nullptr; |
271 } | |
272 | |
273 std::string googleBucketName; | |
274 if (!pluginSection.LookupStringValue(googleBucketName, "BucketName")) | |
275 { | |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
276 LOG(ERROR) << "GoogleCloudStorage/BucketName configuration missing. Unable to initialize plugin"; |
1 | 277 return nullptr; |
278 } | |
279 | |
280 // Use service account credentials from a JSON keyfile: | |
281 auto creds = gcs::oauth2::CreateServiceAccountCredentialsFromJsonFilePath(pathToGoogleCredentials); | |
282 if (!creds) | |
283 { | |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
284 LOG(ERROR) << "GoogleCloudStorage plugin: unable to validate credentials. Check the ServiceAccountFile: " << creds.status().message(); |
1 | 285 return nullptr; |
286 } | |
287 | |
288 // Create a client to communicate with Google Cloud Storage. | |
289 google::cloud::StatusOr<gcs::Client> mainClient = gcs::Client(gcs::ClientOptions(*creds)); | |
290 | |
291 if (!mainClient) | |
292 { | |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
293 LOG(ERROR) << "GoogleCloudStorage plugin: unable to create client: " << mainClient.status().message(); |
1 | 294 return nullptr; |
295 } | |
296 | |
79 | 297 return new GoogleStoragePlugin(nameForLogs, googleBucketName, mainClient.value(), enableLegacyStorageStructure, storageContainsUnknownFiles); |
1 | 298 } |
299 | |
79 | 300 GoogleStoragePlugin::GoogleStoragePlugin(const std::string& nameForLogs, const std::string &bucketName, google::cloud::storage::Client& mainClient, bool enableLegacyStorageStructure, bool storageContainsUnknownFiles) |
301 : BaseStorage(nameForLogs, enableLegacyStorageStructure), | |
15 | 302 bucketName_(bucketName), |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
303 mainClient_(mainClient), |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
304 storageContainsUnknownFiles_(storageContainsUnknownFiles) |
1 | 305 { |
306 | |
307 } | |
308 | |
78 | 309 IStorage::IWriter* GoogleStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
1 | 310 { |
311 return new Writer(bucketName_, GetPath(uuid, type, encryptionEnabled), mainClient_); | |
312 } | |
313 | |
78 | 314 IStorage::IReader* GoogleStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
1 | 315 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
316 std::list<std::string> paths; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
317 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
|
318 if (storageContainsUnknownFiles_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
319 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
320 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
|
321 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
322 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
323 return new Reader(bucketName_, paths, mainClient_); |
1 | 324 } |
325 | |
82 | 326 void GoogleStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
1 | 327 { |
328 gcs::Client client(mainClient_); | |
329 | |
330 std::string path = GetPath(uuid, type, encryptionEnabled); | |
331 | |
332 auto deletionStatus = client.DeleteObject(bucketName_, path); | |
333 | |
334 if (!deletionStatus.ok()) | |
335 { | |
336 throw StoragePluginException("GoogleCloudStorage: error while deleting file " + std::string(path) + ": " + deletionStatus.message()); | |
337 } | |
338 } |