-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathAzureBlobStorage.cs
513 lines (398 loc) · 17.6 KB
/
AzureBlobStorage.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using Azure.Storage.Sas;
using Blobs;
using Microsoft.Identity.Client;
using FluentStorage.Blobs;
using FluentStorage.Azure.Blobs.Gen2.Model;
namespace FluentStorage.Azure.Blobs {
//auth scenarios: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/storage/Azure.Storage.Blobs/samples/Sample02_Auth.cs
class AzureBlobStorage : IAzureBlobStorage {
private readonly BlobServiceClient _client;
private readonly StorageSharedKeyCredential _sasSigningCredentials;
private readonly string _containerName;
private readonly ConcurrentDictionary<string, BlobContainerClient> _containerNameToContainerClient =
new ConcurrentDictionary<string, BlobContainerClient>();
public AzureBlobStorage(
BlobServiceClient blobServiceClient,
string accountName,
StorageSharedKeyCredential sasSigningCredentials = null,
string containerName = null) {
_client = blobServiceClient ?? throw new ArgumentNullException(nameof(blobServiceClient));
_sasSigningCredentials = sasSigningCredentials;
_containerName = containerName;
}
#region [ Interface Methods ]
public virtual async Task<IReadOnlyCollection<Blob>> ListAsync(ListOptions options = null, CancellationToken cancellationToken = default) {
if (options == null)
options = new ListOptions();
var result = new List<Blob>();
var containers = new List<BlobContainerClient>();
if (StoragePath.IsRootPath(options.FolderPath) && _containerName == null) {
// list all of the containers
containers.AddRange(await ListContainersAsync(cancellationToken).ConfigureAwait(false));
result.AddRange(containers.Select(AzConvert.ToBlob));
if (!options.Recurse)
return result;
}
else {
(BlobContainerClient container, string path) = await GetPartsAsync(options.FolderPath, false).ConfigureAwait(false);
if (container == null)
return new List<Blob>();
options = options.Clone();
options.FolderPath = path; //scan from subpath now
containers.Add(container);
}
await Task.WhenAll(containers.Select(c => ListAsync(c, result, options, cancellationToken))).ConfigureAwait(false);
if (options.MaxResults != null) {
result = result.Take(options.MaxResults.Value).ToList();
}
return result;
}
public async Task DeleteAsync(IEnumerable<string> fullPaths, CancellationToken cancellationToken = default) {
GenericValidation.CheckBlobFullPaths(fullPaths);
await Task.WhenAll(fullPaths.Select(fullPath => DeleteAsync(fullPath, cancellationToken))).ConfigureAwait(false);
}
public void Dispose() { }
public async Task<IReadOnlyCollection<bool>> ExistsAsync(IEnumerable<string> fullPaths, CancellationToken cancellationToken = default) {
return await Task.WhenAll(fullPaths.Select(p => ExistsAsync(p, cancellationToken))).ConfigureAwait(false);
}
public async Task<IReadOnlyCollection<Blob>> GetBlobsAsync(IEnumerable<string> fullPaths, CancellationToken cancellationToken = default) {
return await Task.WhenAll(fullPaths.Select(p => GetBlobAsync(p, cancellationToken))).ConfigureAwait(false);
}
public async Task<Stream> OpenReadAsync(string fullPath, CancellationToken cancellationToken = default) {
GenericValidation.CheckBlobFullPath(fullPath);
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, false).ConfigureAwait(false);
BlockBlobClient client = container.GetBlockBlobClient(path);
try {
// Backward compatibility: Explicitly handle empty blobs to ensure they return a MemoryStream,
// preserving the behavior of the old implementation.
var properties = await client.GetPropertiesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
if (properties.Value.ContentLength == 0) {
return new MemoryStream();
}
return await client.OpenReadAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "BlobNotFound") {
return null;
}
}
public Task<ITransaction> OpenTransactionAsync() => throw new NotImplementedException();
public async Task WriteAsync(string fullPath, Stream dataStream,
bool append = false, CancellationToken cancellationToken = default) {
GenericValidation.CheckBlobFullPath(fullPath);
if (dataStream == null)
throw new ArgumentNullException(nameof(dataStream));
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, true).ConfigureAwait(false);
BlockBlobClient client = container.GetBlockBlobClient(path);
try {
await client.UploadAsync(
new StorageSourceStream(dataStream),
cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "OperationNotAllowedInCurrentState") {
//happens when trying to write to a non-file object i.e. folder
}
}
public async Task SetBlobsAsync(IEnumerable<Blob> blobs, CancellationToken cancellationToken = default) {
GenericValidation.CheckBlobFullPaths(blobs);
await Task.WhenAll(blobs.Select(b => SetBlobAsync(b, cancellationToken))).ConfigureAwait(false);
}
#endregion
#region [ IAzureBlobStorage Specific ]
public async Task<AzureStorageLease> AcquireLeaseAsync(
string fullPath,
TimeSpan? maxLeaseTime = null,
string proposedLeaseId = null,
bool waitForRelease = false,
CancellationToken cancellationToken = default) {
GenericValidation.CheckBlobFullPath(fullPath);
if (maxLeaseTime != null) {
if (maxLeaseTime.Value < TimeSpan.FromSeconds(15) || maxLeaseTime.Value >= TimeSpan.FromMinutes(1)) {
throw new ArgumentException(nameof(maxLeaseTime), $"When specifying lease time, make sure it's between 15 seconds and 1 minute, was: {maxLeaseTime.Value}");
}
}
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, true).ConfigureAwait(false);
//get lease client for container or blob
BlobLeaseClient leaseClient;
if (string.IsNullOrEmpty(path)) {
leaseClient = container.GetBlobLeaseClient(proposedLeaseId);
}
else {
//create a new blob if it doesn't exist
if (!(await ExistsAsync(fullPath).ConfigureAwait(false))) {
await WriteAsync(fullPath, new MemoryStream(), false, cancellationToken).ConfigureAwait(false);
}
BlockBlobClient client = container.GetBlockBlobClient(path);
leaseClient = client.GetBlobLeaseClient(proposedLeaseId);
}
while (!cancellationToken.IsCancellationRequested) {
try {
await leaseClient.AcquireAsync(
maxLeaseTime == null ? TimeSpan.MinValue : maxLeaseTime.Value,
cancellationToken: cancellationToken).ConfigureAwait(false);
break;
}
catch (RequestFailedException ex) when (ex.ErrorCode == "LeaseAlreadyPresent") {
if (!waitForRelease) {
throw new StorageException(ErrorCode.Conflict, ex);
}
else {
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
}
}
}
return new AzureStorageLease(leaseClient);
}
public async Task BreakLeaseAsync(string fullPath, bool ignoreErrors = false, CancellationToken cancellationToken = default) {
GenericValidation.CheckBlobFullPath(fullPath);
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, true).ConfigureAwait(false);
//get lease client for container or blob
BlobLeaseClient leaseClient;
if (string.IsNullOrEmpty(path)) {
leaseClient = container.GetBlobLeaseClient();
}
else {
BlockBlobClient client = container.GetBlockBlobClient(path);
leaseClient = client.GetBlobLeaseClient();
}
try {
await leaseClient.BreakAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "LeaseNotPresentWithLeaseOperation") {
if (!ignoreErrors)
throw;
}
catch (RequestFailedException ex) when (ex.ErrorCode == "BlobNotFound") {
if (!ignoreErrors)
throw;
}
}
public async Task<ContainerPublicAccessType> GetContainerPublicAccessAsync(string containerName, CancellationToken cancellationToken = default) {
(BlobContainerClient container, _) = await GetPartsAsync(containerName, true).ConfigureAwait(false);
Response<BlobContainerAccessPolicy> policy =
await container.GetAccessPolicyAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return (ContainerPublicAccessType)(int)policy.Value.BlobPublicAccess;
}
public async Task SetContainerPublicAccessAsync(string containerName, ContainerPublicAccessType containerPublicAccessType, CancellationToken cancellationToken = default) {
(BlobContainerClient container, _) = await GetPartsAsync(containerName, true).ConfigureAwait(false);
await container.SetAccessPolicyAsync(
(PublicAccessType)(int)containerPublicAccessType,
cancellationToken: cancellationToken).ConfigureAwait(false);
}
public Task<string> GetStorageSasAsync(
AccountSasPolicy accountPolicy, bool includeUrl = true, CancellationToken cancellationToken = default) {
if (accountPolicy is null)
throw new ArgumentNullException(nameof(accountPolicy));
if (_sasSigningCredentials == null)
throw new NotSupportedException($"cannot create Shared Access Signature, you have to authenticate using Shared Key in order to issue them.");
string sas = accountPolicy.ToSasQuery(_sasSigningCredentials);
if (includeUrl) {
string url = _client.Uri.ToString();
url += "?";
url += sas;
return Task.FromResult(url);
}
return Task.FromResult(sas);
}
public Task<string> GetContainerSasAsync(
string containerName,
ContainerSasPolicy containerSasPolicy,
bool includeUrl = true,
CancellationToken cancellationToken = default) {
string sas = containerSasPolicy.ToSasQuery(_sasSigningCredentials, containerName);
if (includeUrl) {
string url = _client.Uri.ToString();
url += containerName;
url += "/?";
url += sas;
return Task.FromResult(url);
}
return Task.FromResult(sas);
}
public async Task<string> GetBlobSasAsync(
string fullPath,
BlobSasPolicy blobSasPolicy = null,
bool includeUrl = true,
CancellationToken cancellationToken = default) {
if (blobSasPolicy == null)
blobSasPolicy = new BlobSasPolicy(DateTime.UtcNow, TimeSpan.FromHours(1)) { Permissions = BlobSasPermission.Read };
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, false).ConfigureAwait(false);
string sas = blobSasPolicy.ToSasQuery(_sasSigningCredentials, container.Name, path);
if (includeUrl) {
string url = new Uri(_client.Uri, StoragePath.Normalize(fullPath)).ToString();
url += "?";
url += sas;
return url;
}
return sas;
}
public async Task<Stream> OpenWriteAsync(string fullPath, CancellationToken cancellationToken = default) {
GenericValidation.CheckBlobFullPath(fullPath);
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, true).ConfigureAwait(false);
BlockBlobClient client = container.GetBlockBlobClient(path);
try {
return await client.OpenWriteAsync(true, null, cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "OperationNotAllowedInCurrentState") {
//happens when trying to write to a non-file object i.e. folder
}
return null;
}
#endregion
private async Task SetBlobAsync(Blob blob, CancellationToken cancellationToken) {
if (!(await ExistsAsync(blob, cancellationToken).ConfigureAwait(false)))
return;
(BlobContainerClient container, string path) = await GetPartsAsync(blob, false).ConfigureAwait(false);
if (string.IsNullOrEmpty(path)) {
//it's a container!
await container.SetMetadataAsync(blob.Metadata, cancellationToken: cancellationToken).ConfigureAwait(false);
}
else {
BlockBlobClient client = container.GetBlockBlobClient(path);
await client.SetMetadataAsync(blob.Metadata, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
protected virtual async Task<Blob> GetBlobAsync(string fullPath, CancellationToken cancellationToken) {
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, false).ConfigureAwait(false);
if (container == null)
return null;
if (string.IsNullOrEmpty(path)) {
//it's a container
Response<BlobContainerProperties> attributes = await container.GetPropertiesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return AzConvert.ToBlob(container.Name, attributes);
}
BlobClient client = container.GetBlobClient(path);
try {
Response<BlobProperties> properties = await client.GetPropertiesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return AzConvert.ToBlob(_containerName, path, properties);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "BlobNotFound") {
return null;
}
}
private async Task<IReadOnlyCollection<BlobContainerClient>> ListContainersAsync(CancellationToken cancellationToken) {
var r = new List<BlobContainerClient>();
//check that the special "$logs" container exists
BlobContainerClient logsContainerClient = _client.GetBlobContainerClient("$logs");
Task<Response<BlobContainerProperties>> logsProps = logsContainerClient.GetPropertiesAsync();
//in the meanwhile, enumerate
await foreach (BlobContainerItem container in _client.GetBlobContainersAsync(BlobContainerTraits.Metadata).ConfigureAwait(false)) {
(BlobContainerClient client, _) = await GetPartsAsync(container.Name, false).ConfigureAwait(false);
if (client != null)
r.Add(client);
}
try {
await logsProps.ConfigureAwait(false);
r.Add(logsContainerClient);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "ContainerNotFound") {
}
return r;
}
private async Task ListAsync(BlobContainerClient container,
List<Blob> result,
ListOptions options,
CancellationToken cancellationToken) {
using (var browser = new AzureContainerBrowser(container, _containerName == null, options.NumberOfRecursionThreads ?? ListOptions.MAX_THREADS)) {
IReadOnlyCollection<Blob> containerBlobs =
await browser.ListFolderAsync(options, cancellationToken)
.ConfigureAwait(false);
if (containerBlobs.Count > 0) {
result.AddRange(containerBlobs);
}
}
}
protected virtual async Task DeleteAsync(string fullPath, CancellationToken cancellationToken) {
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, false).ConfigureAwait(false);
if (StoragePath.IsRootPath(path)) {
//deleting the entire container / filesystem
await container.DeleteIfExistsAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
else {
BlockBlobClient blob = string.IsNullOrEmpty(path)
? null
: container.GetBlockBlobClient(StoragePath.Normalize(path));
if (blob != null) {
try {
await blob.DeleteAsync(
DeleteSnapshotsOption.IncludeSnapshots, cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "BlobNotFound") {
//this might be a folder reference, just try it
await foreach (BlobItem recursedFile in
container.GetBlobsAsync(prefix: path, cancellationToken: cancellationToken).ConfigureAwait(false)) {
BlobClient client = container.GetBlobClient(recursedFile.Name);
await client.DeleteIfExistsAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}
}
}
private async Task<bool> ExistsAsync(string fullPath, CancellationToken cancellationToken = default) {
(BlobContainerClient container, string path) = await GetPartsAsync(fullPath, true).ConfigureAwait(false);
if (container == null)
return false;
BlobBaseClient client = container.GetBlobBaseClient(path);
try {
await client.GetPropertiesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "BlobNotFound") {
return false;
}
return true;
}
private async Task<(BlobContainerClient, string)> GetPartsAsync(string fullPath, bool createContainer = true) {
GenericValidation.CheckBlobFullPath(fullPath);
fullPath = StoragePath.Normalize(fullPath);
if (fullPath == null)
throw new ArgumentNullException(nameof(fullPath));
string containerName, relativePath;
if (_containerName == null) {
string[] parts = StoragePath.Split(fullPath);
if (parts.Length == 1) {
containerName = parts[0];
relativePath = string.Empty;
}
else {
containerName = parts[0];
relativePath = StoragePath.Combine(parts.Skip(1)).Substring(1);
}
}
else {
containerName = _containerName;
relativePath = fullPath;
}
if (!_containerNameToContainerClient.TryGetValue(containerName, out BlobContainerClient container)) {
container = _client.GetBlobContainerClient(containerName);
if (_containerName == null) {
try {
//check if container exists
await container.GetPropertiesAsync().ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.ErrorCode == "ContainerNotFound") {
if (createContainer) {
await container.CreateIfNotExistsAsync().ConfigureAwait(false);
}
else {
return (null, null);
}
}
}
_containerNameToContainerClient[containerName] = container;
}
return (container, relativePath);
}
}
}