1
|
1 /**
|
|
2 * Cloud storage plugins for Orthanc
|
|
3 * Copyright (C) 2017-2020 Osimis S.A., Belgium
|
|
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
|
|
106 virtual void Read(char* data, size_t size)
|
|
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
|
|
123 };
|
|
124
|
|
125
|
|
126
|
|
127 const char* GoogleStoragePluginFactory::GetStoragePluginName()
|
|
128 {
|
|
129 return "Google Cloud Storage";
|
|
130 }
|
|
131
|
|
132 IStoragePlugin* GoogleStoragePluginFactory::CreateStoragePlugin(const OrthancPlugins::OrthancConfiguration& orthancConfig)
|
|
133 {
|
15
|
134 bool enableLegacyStorageStructure;
|
|
135
|
1
|
136 if (!orthancConfig.IsSection(PLUGIN_SECTION))
|
|
137 {
|
|
138 OrthancPlugins::LogWarning(std::string(GetStoragePluginName()) + " plugin, section missing. Plugin is not enabled.");
|
|
139 return nullptr;
|
|
140 }
|
|
141
|
|
142 OrthancPlugins::OrthancConfiguration pluginSection;
|
|
143 orthancConfig.GetSection(pluginSection, PLUGIN_SECTION);
|
|
144
|
15
|
145 if (!BaseStoragePlugin::ReadCommonConfiguration(enableLegacyStorageStructure, pluginSection))
|
|
146 {
|
|
147 return nullptr;
|
|
148 }
|
|
149
|
1
|
150 std::string pathToGoogleCredentials;
|
|
151
|
|
152 if (!pluginSection.LookupStringValue(pathToGoogleCredentials, "ServiceAccountFile"))
|
|
153 {
|
|
154 OrthancPlugins::LogError("GoogleCloudStorage/ServiceAccountFile configuration missing. Unable to initialize plugin");
|
|
155 return nullptr;
|
|
156 }
|
|
157
|
|
158 std::string googleBucketName;
|
|
159 if (!pluginSection.LookupStringValue(googleBucketName, "BucketName"))
|
|
160 {
|
|
161 OrthancPlugins::LogError("GoogleCloudStorage/BucketName configuration missing. Unable to initialize plugin");
|
|
162 return nullptr;
|
|
163 }
|
|
164
|
|
165 // Use service account credentials from a JSON keyfile:
|
|
166 auto creds = gcs::oauth2::CreateServiceAccountCredentialsFromJsonFilePath(pathToGoogleCredentials);
|
|
167 if (!creds)
|
|
168 {
|
|
169 OrthancPlugins::LogError("GoogleCloudStorage plugin: unable to validate credentials. Check the ServiceAccountFile: " + creds.status().message());
|
|
170 return nullptr;
|
|
171 }
|
|
172
|
|
173 // Create a client to communicate with Google Cloud Storage.
|
|
174 google::cloud::StatusOr<gcs::Client> mainClient = gcs::Client(gcs::ClientOptions(*creds));
|
|
175
|
|
176 if (!mainClient)
|
|
177 {
|
|
178 OrthancPlugins::LogError("GoogleCloudStorage plugin: unable to create client: " + mainClient.status().message());
|
|
179 return nullptr;
|
|
180 }
|
|
181
|
15
|
182 return new GoogleStoragePlugin(googleBucketName, mainClient.value(), enableLegacyStorageStructure);
|
1
|
183 }
|
|
184
|
15
|
185 GoogleStoragePlugin::GoogleStoragePlugin(const std::string &bucketName, google::cloud::storage::Client& mainClient, bool enableLegacyStorageStructure)
|
|
186 : BaseStoragePlugin(enableLegacyStorageStructure),
|
|
187 bucketName_(bucketName),
|
1
|
188 mainClient_(mainClient)
|
|
189 {
|
|
190
|
|
191 }
|
|
192
|
|
193 IStoragePlugin::IWriter* GoogleStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
|
|
194 {
|
|
195 return new Writer(bucketName_, GetPath(uuid, type, encryptionEnabled), mainClient_);
|
|
196 }
|
|
197
|
|
198 IStoragePlugin::IReader* GoogleStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
|
|
199 {
|
|
200 return new Reader(bucketName_, GetPath(uuid, type, encryptionEnabled), mainClient_);
|
|
201 }
|
|
202
|
|
203 void GoogleStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
|
|
204 {
|
|
205 gcs::Client client(mainClient_);
|
|
206
|
|
207 std::string path = GetPath(uuid, type, encryptionEnabled);
|
|
208
|
|
209 auto deletionStatus = client.DeleteObject(bucketName_, path);
|
|
210
|
|
211 if (!deletionStatus.ok())
|
|
212 {
|
|
213 throw StoragePluginException("GoogleCloudStorage: error while deleting file " + std::string(path) + ": " + deletionStatus.message());
|
|
214 }
|
|
215
|
|
216 }
|