Mercurial > hg > orthanc-object-storage
annotate Google/GoogleStoragePlugin.cpp @ 42:822e29256ffb
upgrade to Orthanc framework 1.9.1 in AWS S3
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 19 Mar 2021 12:13:18 +0100 |
parents | 516b6e731bb5 |
children | b922ae86bbe1 |
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 | |
19 #include "GoogleStoragePlugin.h" | |
20 | |
21 #include "google/cloud/storage/client.h" | |
22 | |
23 // Create aliases to make the code easier to read. | |
24 namespace gcs = google::cloud::storage; | |
15 | 25 static const char* const PLUGIN_SECTION = "GoogleCloudStorage"; |
26 | |
27 const char* GoogleStoragePlugin::GetConfigurationSectionName() | |
28 { | |
29 return PLUGIN_SECTION; | |
30 } | |
1 | 31 |
32 class Writer : public IStoragePlugin::IWriter | |
33 { | |
34 std::string path_; | |
35 gcs::Client client_; | |
36 std::string bucketName_; | |
37 gcs::ObjectWriteStream stream_; | |
38 public: | |
39 Writer(const std::string& bucketName, const std::string& path, gcs::Client& client) | |
40 : path_(path), | |
41 client_(client), | |
42 bucketName_(bucketName) | |
43 { | |
44 } | |
45 | |
46 virtual ~Writer() | |
47 { | |
48 } | |
49 | |
50 virtual void Write(const char* data, size_t size) | |
51 { | |
52 stream_ = client_.WriteObject(bucketName_, path_); | |
53 | |
54 if (stream_) | |
55 { | |
56 stream_.write(data, size); | |
57 stream_.Close(); | |
58 | |
59 if (!stream_.metadata()) | |
60 { | |
61 throw StoragePluginException("GoogleCloudStorage: error while writing file " + std::string(path_) + ": " + stream_.metadata().status().message()); | |
62 } | |
63 } | |
64 else | |
65 { | |
66 throw StoragePluginException("GoogleCloudStorage: error while opening/writing file " + std::string(path_) + ": " + stream_.metadata().status().message()); | |
67 } | |
68 } | |
69 }; | |
70 | |
71 | |
72 class Reader : public IStoragePlugin::IReader | |
73 { | |
74 std::string path_; | |
75 gcs::Client client_; | |
76 std::string bucketName_; | |
77 | |
78 public: | |
79 Reader(const std::string& bucketName, const std::string& path, gcs::Client& client) | |
80 : path_(path), | |
81 client_(client), | |
82 bucketName_(bucketName) | |
83 { | |
84 } | |
85 | |
86 virtual ~Reader() | |
87 { | |
88 | |
89 } | |
90 virtual size_t GetSize() | |
91 { | |
92 auto objectMetadata = client_.GetObjectMetadata(bucketName_, path_); | |
93 | |
94 if (objectMetadata) | |
95 { | |
96 std::uint64_t fileSize = static_cast<int64_t>(objectMetadata->size()); | |
97 | |
98 return fileSize; | |
99 } | |
100 else | |
101 { | |
102 throw StoragePluginException("error while getting the size of " + std::string(path_) + ": " + objectMetadata.status().message()); | |
103 } | |
104 } | |
105 | |
40
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
106 virtual void ReadWhole(char* data, size_t size) |
1 | 107 { |
108 auto reader = client_.ReadObject(bucketName_, path_); | |
109 | |
110 if (!reader) | |
111 { | |
112 throw StoragePluginException("error while opening/reading file " + std::string(path_) + ": " + reader.status().message()); | |
113 } | |
114 | |
115 reader.read(data, size); | |
116 | |
117 if (!reader) | |
118 { | |
119 throw StoragePluginException("error while reading file " + std::string(path_) + ": " + reader.status().message()); | |
120 } | |
121 } | |
122 | |
40
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
123 virtual void ReadRange(char* data, size_t size, size_t fromOffset) |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
124 { |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
125 auto reader = client_.ReadObject(bucketName_, path_, gcs::ReadRange(fromOffset, fromOffset + size)); |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
126 |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
127 if (!reader) |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
128 { |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
129 throw StoragePluginException("error while opening/reading file " + std::string(path_) + ": " + reader.status().message()); |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
130 } |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
131 |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
132 reader.read(data, size); |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
133 |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
134 if (!reader) |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
135 { |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
136 throw StoragePluginException("error while reading file " + std::string(path_) + ": " + reader.status().message()); |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
137 } |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
138 } |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
139 |
516b6e731bb5
added support for ReadRange in google plugin
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
140 |
1 | 141 }; |
142 | |
143 | |
144 | |
145 const char* GoogleStoragePluginFactory::GetStoragePluginName() | |
146 { | |
147 return "Google Cloud Storage"; | |
148 } | |
149 | |
150 IStoragePlugin* GoogleStoragePluginFactory::CreateStoragePlugin(const OrthancPlugins::OrthancConfiguration& orthancConfig) | |
151 { | |
15 | 152 bool enableLegacyStorageStructure; |
153 | |
1 | 154 if (!orthancConfig.IsSection(PLUGIN_SECTION)) |
155 { | |
156 OrthancPlugins::LogWarning(std::string(GetStoragePluginName()) + " plugin, section missing. Plugin is not enabled."); | |
157 return nullptr; | |
158 } | |
159 | |
160 OrthancPlugins::OrthancConfiguration pluginSection; | |
161 orthancConfig.GetSection(pluginSection, PLUGIN_SECTION); | |
162 | |
15 | 163 if (!BaseStoragePlugin::ReadCommonConfiguration(enableLegacyStorageStructure, pluginSection)) |
164 { | |
165 return nullptr; | |
166 } | |
167 | |
1 | 168 std::string pathToGoogleCredentials; |
169 | |
170 if (!pluginSection.LookupStringValue(pathToGoogleCredentials, "ServiceAccountFile")) | |
171 { | |
172 OrthancPlugins::LogError("GoogleCloudStorage/ServiceAccountFile configuration missing. Unable to initialize plugin"); | |
173 return nullptr; | |
174 } | |
175 | |
176 std::string googleBucketName; | |
177 if (!pluginSection.LookupStringValue(googleBucketName, "BucketName")) | |
178 { | |
179 OrthancPlugins::LogError("GoogleCloudStorage/BucketName configuration missing. Unable to initialize plugin"); | |
180 return nullptr; | |
181 } | |
182 | |
183 // Use service account credentials from a JSON keyfile: | |
184 auto creds = gcs::oauth2::CreateServiceAccountCredentialsFromJsonFilePath(pathToGoogleCredentials); | |
185 if (!creds) | |
186 { | |
187 OrthancPlugins::LogError("GoogleCloudStorage plugin: unable to validate credentials. Check the ServiceAccountFile: " + creds.status().message()); | |
188 return nullptr; | |
189 } | |
190 | |
191 // Create a client to communicate with Google Cloud Storage. | |
192 google::cloud::StatusOr<gcs::Client> mainClient = gcs::Client(gcs::ClientOptions(*creds)); | |
193 | |
194 if (!mainClient) | |
195 { | |
196 OrthancPlugins::LogError("GoogleCloudStorage plugin: unable to create client: " + mainClient.status().message()); | |
197 return nullptr; | |
198 } | |
199 | |
15 | 200 return new GoogleStoragePlugin(googleBucketName, mainClient.value(), enableLegacyStorageStructure); |
1 | 201 } |
202 | |
15 | 203 GoogleStoragePlugin::GoogleStoragePlugin(const std::string &bucketName, google::cloud::storage::Client& mainClient, bool enableLegacyStorageStructure) |
204 : BaseStoragePlugin(enableLegacyStorageStructure), | |
205 bucketName_(bucketName), | |
1 | 206 mainClient_(mainClient) |
207 { | |
208 | |
209 } | |
210 | |
211 IStoragePlugin::IWriter* GoogleStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
212 { | |
213 return new Writer(bucketName_, GetPath(uuid, type, encryptionEnabled), mainClient_); | |
214 } | |
215 | |
216 IStoragePlugin::IReader* GoogleStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
217 { | |
218 return new Reader(bucketName_, GetPath(uuid, type, encryptionEnabled), mainClient_); | |
219 } | |
220 | |
221 void GoogleStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
222 { | |
223 gcs::Client client(mainClient_); | |
224 | |
225 std::string path = GetPath(uuid, type, encryptionEnabled); | |
226 | |
227 auto deletionStatus = client.DeleteObject(bucketName_, path); | |
228 | |
229 if (!deletionStatus.ok()) | |
230 { | |
231 throw StoragePluginException("GoogleCloudStorage: error while deleting file " + std::string(path) + ": " + deletionStatus.message()); | |
232 } | |
233 | |
234 } |