-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAWS3StorageService.cs
389 lines (300 loc) · 17.7 KB
/
AWS3StorageService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright 2021-2024 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.SecurityToken;
using Amazon.SecurityToken.Model;
using Ardalis.GuardClauses;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Monai.Deploy.Storage.API;
using Monai.Deploy.Storage.Common;
using Monai.Deploy.Storage.Configuration;
using Monai.Deploy.Storage.S3Policy;
using Newtonsoft.Json;
namespace Monai.Deploy.Storage.AWSS3
{
public class Awss3StorageService : IStorageService
{
private readonly ILogger<Awss3StorageService> _logger;
private readonly AmazonS3Client _client;
private readonly AmazonSecurityTokenServiceClient _tokenServiceClient;
private readonly StorageServiceConfiguration _options;
public string Name => "AWS S3 Storage Service";
public Awss3StorageService(IOptions<StorageServiceConfiguration> options, ILogger<Awss3StorageService> logger)
{
Guard.Against.Null(options, nameof(options));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
var configuration = options.Value;
ValidateConfiguration(configuration);
_options = configuration;
var accessKey = configuration.Settings[ConfigurationKeys.AccessKey];
var accessToken = configuration.Settings[ConfigurationKeys.AccessToken];
var region = configuration.Settings[ConfigurationKeys.Region];
var credentialServiceUrl = configuration.Settings[ConfigurationKeys.CredentialServiceUrl];
_client = new AmazonS3Client(accessKey, accessToken, RegionEndpoint.GetBySystemName(region));
var config = new AmazonSecurityTokenServiceConfig
{
AuthenticationRegion = RegionEndpoint.USEast1.SystemName,
ServiceURL = credentialServiceUrl
};
_tokenServiceClient = new AmazonSecurityTokenServiceClient(accessKey, accessToken, config);
}
private void ValidateConfiguration(StorageServiceConfiguration configuration)
{
Guard.Against.Null(configuration, nameof(configuration));
foreach (var key in ConfigurationKeys.RequiredKeys)
{
if (!configuration.Settings.ContainsKey(key))
{
throw new ConfigurationException($"{Name} is missing configuration for {key}.");
}
}
}
#region ServiceAccount
public async Task CopyObjectAsync(string sourceBucketName, string sourceObjectName, string destinationBucketName, string destinationObjectName, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(sourceBucketName, nameof(sourceBucketName));
Guard.Against.NullOrWhiteSpace(sourceObjectName, nameof(sourceObjectName));
Guard.Against.NullOrWhiteSpace(destinationBucketName, nameof(destinationBucketName));
Guard.Against.NullOrWhiteSpace(destinationObjectName, nameof(destinationObjectName));
await _client.CopyObjectAsync(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName, cancellationToken: cancellationToken).ConfigureAwait(false);
}
public async Task<Stream> GetObjectAsync(string bucketName, string objectName, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrWhiteSpace(objectName, nameof(objectName));
var obj = await _client.GetObjectAsync(bucketName, objectName, cancellationToken: cancellationToken).ConfigureAwait(false);
return obj.ResponseStream;
}
public async Task<IList<VirtualFileInfo>> ListObjectsAsync(string bucketName, string prefix = "", bool recursive = false, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
var request = new ListObjectsV2Request { BucketName = bucketName, Prefix = prefix };
var files = new List<VirtualFileInfo>();
ListObjectsV2Response response;
do
{
response = await _client.ListObjectsV2Async(request, cancellationToken);
response.S3Objects.ForEach(obj => files.Add(new VirtualFileInfo(Path.GetFileName(obj.Key), obj.Key, obj.ETag, (ulong)obj.Size)));
request.ContinuationToken = response.NextContinuationToken;
}
while (response.IsTruncated);
return files;
}
public async Task PutObjectAsync(string bucketName, string objectName, Stream data, long size, string contentType, Dictionary<string, string> metadata, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrWhiteSpace(objectName, nameof(objectName));
Guard.Against.Null(data, nameof(data));
Guard.Against.NullOrWhiteSpace(contentType, nameof(contentType));
var por = new PutObjectRequest
{
BucketName = bucketName, Key = objectName, InputStream = data
};
await _client.PutObjectAsync(por, cancellationToken).ConfigureAwait(false);
}
public async Task RemoveObjectAsync(string bucketName, string objectName, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrWhiteSpace(objectName, nameof(objectName));
var dor = new DeleteObjectRequest { BucketName = bucketName, Key = objectName };
await _client.DeleteObjectAsync(dor, cancellationToken);
}
public async Task RemoveObjectsAsync(string bucketName, IEnumerable<string> objectNames, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrEmpty(objectNames, nameof(objectNames));
var KeyVersionList = new List<KeyVersion>();
foreach (var objectName in objectNames)
{
var keyv = new KeyVersion
{
Key = objectName
};
KeyVersionList.Add(keyv);
}
var dor = new DeleteObjectsRequest { BucketName = bucketName, Objects = KeyVersionList };
await _client.DeleteObjectsAsync(dor, cancellationToken).ConfigureAwait(false);
}
public async Task CreateFolderAsync(string bucketName, string folderPath, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrEmpty(folderPath, nameof(folderPath));
var stubFile = folderPath + "/stubFile.txt";
var por = new PutObjectRequest { BucketName = bucketName, Key = stubFile, ContentBody = "stub file" };
await _client.PutObjectAsync(por, cancellationToken).ConfigureAwait(false);
}
public async Task<Credentials> CreateTemporaryCredentialsAsync(string bucketName, string folderName, int durationSeconds = 3600, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrEmpty(folderName, nameof(folderName));
var policy = PolicyExtensions.ToPolicy(bucketName, folderName);
var policyString = JsonConvert.SerializeObject(policy, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var assumeRoleRequest = new AssumeRoleRequest
{
DurationSeconds = durationSeconds,
Policy = policyString
};
var role = await _tokenServiceClient.AssumeRoleAsync(assumeRoleRequest, cancellationToken: cancellationToken);
return role.Credentials;
}
#endregion ServiceAccount
#region TemporaryCredentials
public async Task CopyObjectWithCredentialsAsync(string sourceBucketName, string sourceObjectName, string destinationBucketName, string destinationObjectName, Credentials credentials, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(sourceBucketName, nameof(sourceBucketName));
Guard.Against.NullOrWhiteSpace(sourceObjectName, nameof(sourceObjectName));
Guard.Against.NullOrWhiteSpace(destinationBucketName, nameof(destinationBucketName));
Guard.Against.NullOrWhiteSpace(destinationObjectName, nameof(destinationObjectName));
IsCredentialsNull(credentials);
var client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, RegionEndpoint.GetBySystemName(_options.Settings[ConfigurationKeys.Region]));
await client.CopyObjectAsync(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName, cancellationToken: cancellationToken).ConfigureAwait(false);
}
public async Task<Stream> GetObjectWithCredentialsAsync(string bucketName, string objectName, Credentials credentials, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrWhiteSpace(objectName, nameof(objectName));
IsCredentialsNull(credentials);
var client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, RegionEndpoint.GetBySystemName(_options.Settings[ConfigurationKeys.Region]));
var obj = await client.GetObjectAsync(bucketName, objectName, cancellationToken: cancellationToken).ConfigureAwait(false);
return obj.ResponseStream;
}
public async Task<IList<VirtualFileInfo>> ListObjectsWithCredentialsAsync(string bucketName, Credentials credentials, string prefix = "", bool recursive = false, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
IsCredentialsNull(credentials);
var client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, RegionEndpoint.GetBySystemName(_options.Settings[ConfigurationKeys.Region]));
var request = new ListObjectsV2Request { BucketName = bucketName, Prefix = prefix };
var files = new List<VirtualFileInfo>();
ListObjectsV2Response response;
do
{
response = await client.ListObjectsV2Async(request, cancellationToken);
response.S3Objects.ForEach(obj => files.Add(new VirtualFileInfo(Path.GetFileName(obj.Key), obj.Key, obj.ETag, (ulong)obj.Size)));
request.ContinuationToken = response.NextContinuationToken;
}
while (response.IsTruncated);
return files;
}
public async Task PutObjectWithCredentialsAsync(string bucketName, string objectName, Stream data, long size, string contentType, Dictionary<string, string> metadata, Credentials credentials, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrWhiteSpace(objectName, nameof(objectName));
Guard.Against.Null(data, nameof(data));
Guard.Against.NullOrWhiteSpace(contentType, nameof(contentType));
IsCredentialsNull(credentials);
var client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, RegionEndpoint.GetBySystemName(_options.Settings[ConfigurationKeys.Region]));
var por = new PutObjectRequest
{
BucketName = bucketName, Key = objectName, InputStream = data
};
await client.PutObjectAsync(por, cancellationToken).ConfigureAwait(false);
}
public async Task RemoveObjectWithCredentialsAsync(string bucketName, string objectName, Credentials credentials, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrWhiteSpace(objectName, nameof(objectName));
IsCredentialsNull(credentials);
var client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, RegionEndpoint.GetBySystemName(_options.Settings[ConfigurationKeys.Region]));
var dor = new DeleteObjectRequest { BucketName = bucketName, Key = objectName };
await client.DeleteObjectAsync(dor, cancellationToken);
}
public async Task RemoveObjectsWithCredentialsAsync(string bucketName, IEnumerable<string> objectNames, Credentials credentials, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrEmpty(objectNames, nameof(objectNames));
var client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, RegionEndpoint.GetBySystemName(_options.Settings[ConfigurationKeys.Region]));
IsCredentialsNull(credentials);
var keyVersionList = new List<KeyVersion>();
foreach (var objectName in objectNames)
{
var keyVersion = new KeyVersion
{
Key = objectName
};
keyVersionList.Add(keyVersion);
}
var dor = new DeleteObjectsRequest { BucketName = bucketName, Objects = keyVersionList };
await client.DeleteObjectsAsync(dor, cancellationToken).ConfigureAwait(false);
}
public async Task CreateFolderWithCredentialsAsync(string bucketName, string folderPath, Credentials credentials, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrEmpty(folderPath, nameof(folderPath));
IsCredentialsNull(credentials);
var client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, RegionEndpoint.GetBySystemName(_options.Settings[ConfigurationKeys.Region]));
var stubFile = folderPath + "/stubFile.txt";
var por = new PutObjectRequest { BucketName = bucketName, Key = stubFile, ContentBody = "stub file" };
await client.PutObjectAsync(por, cancellationToken).ConfigureAwait(false);
}
#endregion TemporaryCredentials
private void IsCredentialsNull(Credentials credentials)
{
Guard.Against.Null(credentials, nameof(credentials));
Guard.Against.NullOrWhiteSpace(credentials.AccessKeyId, nameof(credentials.AccessKeyId));
Guard.Against.NullOrWhiteSpace(credentials.SecretAccessKey, nameof(credentials.SecretAccessKey));
Guard.Against.NullOrWhiteSpace(credentials.SessionToken, nameof(credentials.SessionToken));
}
public async Task<Dictionary<string, bool>> VerifyObjectsExistAsync(string bucketName, IReadOnlyList<string> artifactList, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.Null(artifactList, nameof(artifactList));
var existingObjectsDict = new Dictionary<string, bool>();
foreach (var artifact in artifactList)
{
try
{
var fileObjects = await ListObjectsAsync(bucketName, artifact);
var folderObjects = await ListObjectsAsync(bucketName, artifact.EndsWith("/") ? artifact : $"{artifact}/", true);
if (!folderObjects.Any() && !fileObjects.Any())
{
_logger.FileNotFoundError(bucketName, $"{artifact}");
existingObjectsDict.Add(artifact, false);
continue;
}
existingObjectsDict.Add(artifact, true);
}
catch (Exception e)
{
_logger.LogError(e.Message);
existingObjectsDict.Add(artifact, false);
}
}
return existingObjectsDict;
}
public async Task<bool> VerifyObjectExistsAsync(string bucketName, string artifactName, CancellationToken cancellationToken = default)
{
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
Guard.Against.NullOrWhiteSpace(artifactName, nameof(artifactName));
var fileObjects = await ListObjectsAsync(bucketName, artifactName);
var folderObjects = await ListObjectsAsync(bucketName, artifactName.EndsWith("/") ? artifactName : $"{artifactName}/", true);
if (folderObjects.Any() || fileObjects.Any())
{
return true;
}
_logger.FileNotFoundError(bucketName, $"{artifactName}");
return false;
}
}
}