Skip to content

Commit 8390d0a

Browse files
committed
Migrated to Azure.Storage.Blobs and Cofoundry 0.8
1 parent 712517c commit 8390d0a

File tree

2 files changed

+39
-72
lines changed

2 files changed

+39
-72
lines changed

src/Cofoundry.Plugins.Azure/AzureBlobFileService.cs

Lines changed: 37 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using Cofoundry.Core.Configuration;
1+
using Azure;
2+
using Azure.Storage.Blobs;
3+
using Azure.Storage.Blobs.Models;
4+
using Cofoundry.Core.Configuration;
25
using Cofoundry.Domain.Data;
3-
using Microsoft.Azure.Storage;
4-
using Microsoft.Azure.Storage.Blob;
56
using System;
67
using System.Collections.Concurrent;
78
using System.Collections.Generic;
@@ -20,7 +21,7 @@ public class AzureBlobFileService : IFileStoreService
2021
{
2122
#region constructor
2223

23-
private readonly CloudBlobClient _blobClient;
24+
private readonly BlobServiceClient _blobServiceClient;
2425
private static ConcurrentDictionary<string, byte> _initializedContainers = new ConcurrentDictionary<string, byte>();
2526

2627
public AzureBlobFileService(
@@ -31,11 +32,10 @@ AzureSettings settings
3132

3233
if (string.IsNullOrWhiteSpace(settings.BlobStorageConnectionString))
3334
{
34-
throw new InvalidConfigurationException(typeof(AzureSettings), "The BlobStorageConnectionString is required to use the AzureBlobFileService");
35+
throw new InvalidConfigurationException(typeof(AzureSettings), $"The {nameof(settings.BlobStorageConnectionString)} setting is required to use the {nameof(AzureBlobFileService)}");
3536
}
3637

37-
var storageAccount = CloudStorageAccount.Parse(settings.BlobStorageConnectionString);
38-
_blobClient = storageAccount.CreateCloudBlobClient();
38+
_blobServiceClient = new BlobServiceClient(settings.BlobStorageConnectionString);
3939
}
4040

4141
#endregion
@@ -51,9 +51,9 @@ AzureSettings settings
5151
public async Task<bool> ExistsAsync(string containerName, string fileName)
5252
{
5353
var container = await GetBlobContainerAsync(containerName);
54-
var blockBlob = container.GetBlockBlobReference(fileName);
54+
var blobClient = container.GetBlobClient(fileName);
5555

56-
return await blockBlob.ExistsAsync();
56+
return await blobClient.ExistsAsync();
5757
}
5858

5959
/// <summary>
@@ -65,22 +65,16 @@ public async Task<bool> ExistsAsync(string containerName, string fileName)
6565
public async Task<Stream> GetAsync(string containerName, string fileName)
6666
{
6767
var container = await GetBlobContainerAsync(containerName);
68-
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
69-
Stream stream = null;
68+
var blobClient = container.GetBlobClient(fileName);
7069

7170
try
7271
{
73-
return await blockBlob.OpenReadAsync();
72+
return await blobClient.OpenReadAsync();
7473
}
75-
catch (StorageException ex)
74+
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.BlobNotFound)
7675
{
77-
if (ex.RequestInformation.HttpStatusCode != 404)
78-
{
79-
throw;
80-
}
76+
return null;
8177
}
82-
83-
return stream;
8478
}
8579

8680
/// <summary>
@@ -97,13 +91,13 @@ public Task CreateAsync(string containerName, string fileName, Stream stream)
9791
public async Task CreateOrReplaceAsync(string containerName, string fileName, Stream stream)
9892
{
9993
var container = await GetBlobContainerAsync(containerName);
100-
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
94+
var blockClient = container.GetBlobClient(fileName);
10195

10296
if (stream.Position != 0)
10397
{
10498
stream.Position = 0;
10599
}
106-
await blockBlob.UploadFromStreamAsync(stream);
100+
await blockClient.UploadAsync(stream, true);
107101
}
108102

109103
/// <summary>
@@ -122,8 +116,9 @@ public Task CreateIfNotExistsAsync(string containerName, string fileName, Stream
122116
public async Task DeleteAsync(string containerName, string fileName)
123117
{
124118
var container = await GetBlobContainerAsync(containerName);
125-
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
126-
await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null);
119+
var blockBlob = container.GetBlobClient(fileName);
120+
121+
await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);
127122
}
128123

129124
/// <summary>
@@ -134,22 +129,15 @@ public async Task DeleteAsync(string containerName, string fileName)
134129
public async Task DeleteDirectoryAsync(string containerName, string directoryName)
135130
{
136131
var container = await GetBlobContainerAsync(containerName);
137-
var directory = container.GetDirectoryReference(directoryName);
138132

139-
BlobContinuationToken continuationToken = null;
140-
var blobs = new List<IListBlobItem>();
133+
var blobs = new List<BlobItem>();
141134

142-
do
135+
await foreach (var item in container.GetBlobsAsync(BlobTraits.None, BlobStates.None, directoryName))
143136
{
144-
// each segment is max 5000 items
145-
var segment = await directory.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, continuationToken, null, null);
146-
continuationToken = segment.ContinuationToken;
147-
blobs.AddRange(segment.Results);
148-
137+
blobs.Add(item);
149138
}
150-
while (continuationToken != null);
151139

152-
await DeleteBlobsAsync(blobs);
140+
await DeleteBlobsAsync(container, blobs);
153141
}
154142

155143
/// <summary>
@@ -170,54 +158,36 @@ public async Task ClearContainerAsync(string containerName)
170158
{
171159
var container = await GetBlobContainerAsync(containerName);
172160

173-
BlobContinuationToken continuationToken = null;
174-
var blobs = new List<IListBlobItem>();
161+
var blobs = new List<BlobItem>();
175162

176-
do
163+
await foreach (var item in container.GetBlobsAsync())
177164
{
178-
// each segment is max 5000 items
179-
var segment = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.None, null, continuationToken, null, null);
180-
continuationToken = segment.ContinuationToken;
181-
blobs.AddRange(segment.Results);
182-
165+
blobs.Add(item);
183166
}
184-
while (continuationToken != null);
185167

186-
await DeleteBlobsAsync(blobs);
168+
await DeleteBlobsAsync(container, blobs);
187169
}
188170

189171
#endregion
190172

191173
#region privates
192174

193-
private async Task DeleteBlobsAsync(IEnumerable<IListBlobItem> blobs)
175+
private async Task DeleteBlobsAsync(BlobContainerClient container, IEnumerable<BlobItem> blobs)
194176
{
195177
foreach (var blobItem in blobs)
196178
{
197-
var blockBlob = blobItem as CloudBlockBlob;
198-
if (blockBlob != null)
199-
{
200-
await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null);
201-
}
202-
else
203-
{
204-
var pageBlob = blobItem as CloudPageBlob;
205-
if (pageBlob != null)
206-
{
207-
await pageBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null);
208-
}
209-
}
179+
await container.DeleteBlobIfExistsAsync(blobItem.Name, DeleteSnapshotsOption.IncludeSnapshots);
210180
}
211181
}
212182

213183
private async Task CreateAsync(string containerName, string fileName, Stream stream, bool throwExceptionIfNotExists)
214184
{
215185
var container = await GetBlobContainerAsync(containerName);
216-
var blockBlob = container.GetBlockBlobReference(fileName);
186+
var blobClient = container.GetBlobClient(fileName);
217187

218188
// Don't overwrite:
219189
// http://stackoverflow.com/a/14938608/716689
220-
var accessCondition = AccessCondition.GenerateIfNotExistsCondition();
190+
//var accessCondition = AccessCondition.GenerateIfNotExistsCondition();
221191

222192
try
223193
{
@@ -226,25 +196,22 @@ private async Task CreateAsync(string containerName, string fileName, Stream str
226196
stream.Position = 0;
227197
}
228198

229-
await blockBlob.UploadFromStreamAsync(stream, accessCondition, null, null);
199+
await blobClient.UploadAsync(stream);
200+
//await blobClient.UploadFromStreamAsync(stream, accessCondition, null, null);
230201
}
231-
catch (StorageException ex)
202+
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.BlobAlreadyExists)
232203
{
233-
if (ex.RequestInformation.HttpStatusCode == 409)
234-
{
235-
if (throwExceptionIfNotExists) throw new InvalidOperationException("File already exists", ex);
236-
}
237-
else
204+
if (throwExceptionIfNotExists)
238205
{
239-
throw;
206+
throw new InvalidOperationException("File already exists", ex);
240207
}
241208
}
242209
}
243210

244-
private async Task<CloudBlobContainer> GetBlobContainerAsync(string containerName)
211+
private async Task<BlobContainerClient> GetBlobContainerAsync(string containerName)
245212
{
246213
containerName = containerName.ToLower();
247-
var container = _blobClient.GetContainerReference(containerName);
214+
var container = _blobServiceClient.GetBlobContainerClient(containerName);
248215

249216
// initalize container
250217
if (_initializedContainers.TryAdd(containerName, 0))

src/Cofoundry.Plugins.Azure/Cofoundry.Plugins.Azure.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19-
<PackageReference Include="Cofoundry.Domain" Version="0.7.0" />
20-
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.7" />
19+
<PackageReference Include="Azure.Storage.Blobs" Version="12.7.0" />
20+
<PackageReference Include="Cofoundry.Domain" Version="0.8.0" />
2121
</ItemGroup>
2222

2323
</Project>

0 commit comments

Comments
 (0)