Mercurial > hg > orthanc-object-storage
annotate Google/GoogleStoragePlugin.cpp @ 80:b3787f4aa16a
fix
author | Alain Mazy <am@osimis.io> |
---|---|
date | Sat, 15 Oct 2022 07:49:30 +0200 |
parents | 16514270d9ca |
children | e29987d80ecf |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
37
f55b2afdf53d
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
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:
40
diff
changeset
|
19 |
1 | 20 #include "GoogleStoragePlugin.h" |
21 | |
22 #include "google/cloud/storage/client.h" | |
23 | |
24 // Create aliases to make the code easier to read. | |
25 namespace gcs = google::cloud::storage; | |
15 | 26 |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
27 |
78 | 28 class GoogleStoragePlugin : public BaseStorage |
15 | 29 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
30 public: |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
31 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
32 std::string bucketName_; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
33 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
|
34 bool storageContainsUnknownFiles_; |
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 public: |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
37 |
79 | 38 GoogleStoragePlugin(const std::string& nameForLogs, |
39 const std::string& bucketName, | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
40 google::cloud::storage::Client& mainClient, |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
41 bool enableLegacyStorageStructure, |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
42 bool storageContainsUnknownFiles |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
43 ); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
44 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
45 virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
46 virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); |
77 | 47 virtual bool DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled); |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
48 }; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
49 |
1 | 50 |
78 | 51 class Writer : public IStorage::IWriter |
1 | 52 { |
53 std::string path_; | |
54 gcs::Client client_; | |
55 std::string bucketName_; | |
56 gcs::ObjectWriteStream stream_; | |
57 public: | |
58 Writer(const std::string& bucketName, const std::string& path, gcs::Client& client) | |
59 : path_(path), | |
60 client_(client), | |
61 bucketName_(bucketName) | |
62 { | |
63 } | |
64 | |
65 virtual ~Writer() | |
66 { | |
67 } | |
68 | |
69 virtual void Write(const char* data, size_t size) | |
70 { | |
71 stream_ = client_.WriteObject(bucketName_, path_); | |
72 | |
73 if (stream_) | |
74 { | |
75 stream_.write(data, size); | |
76 stream_.Close(); | |
77 | |
78 if (!stream_.metadata()) | |
79 { | |
80 throw StoragePluginException("GoogleCloudStorage: error while writing file " + std::string(path_) + ": " + stream_.metadata().status().message()); | |
81 } | |
82 } | |
83 else | |
84 { | |
85 throw StoragePluginException("GoogleCloudStorage: error while opening/writing file " + std::string(path_) + ": " + stream_.metadata().status().message()); | |
86 } | |
87 } | |
88 }; | |
89 | |
90 | |
78 | 91 class Reader : public IStorage::IReader |
1 | 92 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
93 std::list<std::string> paths_; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
94 gcs::Client client_; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
95 std::string bucketName_; |
1 | 96 |
97 public: | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
98 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
|
99 : paths_(paths), |
1 | 100 client_(client), |
101 bucketName_(bucketName) | |
102 { | |
103 } | |
104 | |
105 virtual ~Reader() | |
106 { | |
107 | |
108 } | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
109 |
1 | 110 virtual size_t GetSize() |
111 { | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
112 std::string firstExceptionMessage; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
113 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
114 for (auto& path: paths_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
115 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
116 try |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
117 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
118 return _GetSize(path); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
119 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
120 catch (StoragePluginException& ex) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
121 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
122 if (firstExceptionMessage.empty()) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
123 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
124 firstExceptionMessage = ex.what(); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
125 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
126 //ignore to retry |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
127 } |
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 throw StoragePluginException(firstExceptionMessage); |
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 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
132 void ReadWhole(char* data, size_t size) |
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 std::string 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 for (auto& path: paths_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
137 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
138 try |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
139 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
140 return _ReadWhole(path, data, size); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
141 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
142 catch (StoragePluginException& ex) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
143 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
144 if (firstExceptionMessage.empty()) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
145 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
146 firstExceptionMessage = ex.what(); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
147 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
148 //ignore to retry |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
149 } |
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 throw StoragePluginException(firstExceptionMessage); |
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 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
154 void ReadRange(char* data, size_t size, size_t fromOffset) |
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 std::string 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 for (auto& path: paths_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
159 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
160 try |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
161 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
162 return _ReadRange(path, data, size, fromOffset); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
163 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
164 catch (StoragePluginException& ex) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
165 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
166 if (firstExceptionMessage.empty()) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
167 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
168 firstExceptionMessage = ex.what(); |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
169 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
170 //ignore to retry |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
171 } |
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 throw StoragePluginException(firstExceptionMessage); |
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 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
176 private: |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
177 virtual void _ReadWhole(const std::string& path, char* data, size_t size) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
178 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
179 auto reader = client_.ReadObject(bucketName_, path); |
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 if (!reader) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
182 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
183 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
|
184 } |
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 reader.read(data, size); |
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 if (!reader) |
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 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
|
191 } |
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 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
194 virtual void _ReadRange(const std::string& path, char* data, size_t size, size_t fromOffset) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
195 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
196 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
|
197 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
198 if (!reader) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
199 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
200 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
|
201 } |
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 reader.read(data, size); |
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 if (!reader) |
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 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
|
208 } |
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 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
211 size_t _GetSize(const std::string& path) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
212 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
213 auto objectMetadata = client_.GetObjectMetadata(bucketName_, path); |
1 | 214 |
215 if (objectMetadata) | |
216 { | |
217 std::uint64_t fileSize = static_cast<int64_t>(objectMetadata->size()); | |
218 | |
219 return fileSize; | |
220 } | |
221 else | |
222 { | |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
223 throw StoragePluginException("error while getting the size of " + std::string(path) + ": " + objectMetadata.status().message()); |
1 | 224 } |
225 } | |
226 | |
227 }; | |
228 | |
229 | |
230 | |
231 const char* GoogleStoragePluginFactory::GetStoragePluginName() | |
232 { | |
233 return "Google Cloud Storage"; | |
234 } | |
235 | |
79 | 236 IStorage* GoogleStoragePluginFactory::CreateStorage(const std::string& nameForLogs, const OrthancPlugins::OrthancConfiguration& orthancConfig) |
1 | 237 { |
15 | 238 bool enableLegacyStorageStructure; |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
239 bool storageContainsUnknownFiles; |
15 | 240 |
1 | 241 if (!orthancConfig.IsSection(PLUGIN_SECTION)) |
242 { | |
243 OrthancPlugins::LogWarning(std::string(GetStoragePluginName()) + " plugin, section missing. Plugin is not enabled."); | |
244 return nullptr; | |
245 } | |
246 | |
247 OrthancPlugins::OrthancConfiguration pluginSection; | |
248 orthancConfig.GetSection(pluginSection, PLUGIN_SECTION); | |
249 | |
78 | 250 if (!BaseStorage::ReadCommonConfiguration(enableLegacyStorageStructure, storageContainsUnknownFiles, pluginSection)) |
15 | 251 { |
252 return nullptr; | |
253 } | |
254 | |
1 | 255 std::string pathToGoogleCredentials; |
256 | |
257 if (!pluginSection.LookupStringValue(pathToGoogleCredentials, "ServiceAccountFile")) | |
258 { | |
259 OrthancPlugins::LogError("GoogleCloudStorage/ServiceAccountFile configuration missing. Unable to initialize plugin"); | |
260 return nullptr; | |
261 } | |
262 | |
263 std::string googleBucketName; | |
264 if (!pluginSection.LookupStringValue(googleBucketName, "BucketName")) | |
265 { | |
266 OrthancPlugins::LogError("GoogleCloudStorage/BucketName configuration missing. Unable to initialize plugin"); | |
267 return nullptr; | |
268 } | |
269 | |
270 // Use service account credentials from a JSON keyfile: | |
271 auto creds = gcs::oauth2::CreateServiceAccountCredentialsFromJsonFilePath(pathToGoogleCredentials); | |
272 if (!creds) | |
273 { | |
274 OrthancPlugins::LogError("GoogleCloudStorage plugin: unable to validate credentials. Check the ServiceAccountFile: " + creds.status().message()); | |
275 return nullptr; | |
276 } | |
277 | |
278 // Create a client to communicate with Google Cloud Storage. | |
279 google::cloud::StatusOr<gcs::Client> mainClient = gcs::Client(gcs::ClientOptions(*creds)); | |
280 | |
281 if (!mainClient) | |
282 { | |
283 OrthancPlugins::LogError("GoogleCloudStorage plugin: unable to create client: " + mainClient.status().message()); | |
284 return nullptr; | |
285 } | |
286 | |
79 | 287 return new GoogleStoragePlugin(nameForLogs, googleBucketName, mainClient.value(), enableLegacyStorageStructure, storageContainsUnknownFiles); |
1 | 288 } |
289 | |
79 | 290 GoogleStoragePlugin::GoogleStoragePlugin(const std::string& nameForLogs, const std::string &bucketName, google::cloud::storage::Client& mainClient, bool enableLegacyStorageStructure, bool storageContainsUnknownFiles) |
291 : BaseStorage(nameForLogs, enableLegacyStorageStructure), | |
15 | 292 bucketName_(bucketName), |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
293 mainClient_(mainClient), |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
294 storageContainsUnknownFiles_(storageContainsUnknownFiles) |
1 | 295 { |
296 | |
297 } | |
298 | |
78 | 299 IStorage::IWriter* GoogleStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
1 | 300 { |
301 return new Writer(bucketName_, GetPath(uuid, type, encryptionEnabled), mainClient_); | |
302 } | |
303 | |
78 | 304 IStorage::IReader* GoogleStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
1 | 305 { |
70
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
306 std::list<std::string> paths; |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
307 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
|
308 if (storageContainsUnknownFiles_) |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
309 { |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
310 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
|
311 } |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
312 |
16e419fe80c5
Google & Azure: Added the "EnableLegacyUnknownFiles" configuration option
Alain Mazy <am@osimis.io>
parents:
56
diff
changeset
|
313 return new Reader(bucketName_, paths, mainClient_); |
1 | 314 } |
315 | |
77 | 316 bool GoogleStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
1 | 317 { |
318 gcs::Client client(mainClient_); | |
319 | |
320 std::string path = GetPath(uuid, type, encryptionEnabled); | |
321 | |
322 auto deletionStatus = client.DeleteObject(bucketName_, path); | |
323 | |
324 if (!deletionStatus.ok()) | |
325 { | |
326 throw StoragePluginException("GoogleCloudStorage: error while deleting file " + std::string(path) + ": " + deletionStatus.message()); | |
327 } | |
328 | |
77 | 329 return false; // not 100% sure that the file has been deleted |
1 | 330 } |