-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAzureBlobStorage.cs
More file actions
45 lines (34 loc) · 1.66 KB
/
AzureBlobStorage.cs
File metadata and controls
45 lines (34 loc) · 1.66 KB
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
using Azure.Storage.Blobs;
namespace Nhs.Appointments.Core.Blob;
public class AzureBlobStorage(BlobServiceClient blobServiceClient) : IAzureBlobStorage
{
public async Task<Stream> GetBlobUploadStream(string containerName, string blobName)
{
var blobClient = await GetBlobClientFromContainerAndBlobNameAsync(containerName, blobName);
return await blobClient.OpenWriteAsync(true);
}
public BlobClient GetBlobClientFromContainerAndBlobName(string containerName, string blobName)
{
var containerClient = ResolveContainerClient(containerName);
return containerClient.GetBlobClient(blobName);
}
public async Task<BlobClient> GetBlobClientFromContainerAndBlobNameAsync(string containerName, string blobName)
{
var containerClient = await ResolveContainerClientAsync(containerName);
return containerClient.GetBlobClient(blobName);
}
private BlobContainerClient ResolveContainerClient(string containerName)
{
var containers = blobServiceClient.GetBlobContainers();
return containers.Any(x => x.Name.Equals(containerName))
? blobServiceClient.GetBlobContainerClient(containerName)
: blobServiceClient.CreateBlobContainer(containerName);
}
private async Task<BlobContainerClient> ResolveContainerClientAsync(string containerName)
{
var exists = await blobServiceClient.GetBlobContainersAsync().AnyAsync(x => x.Name.Equals(containerName));
return exists
? blobServiceClient.GetBlobContainerClient(containerName)
: await blobServiceClient.CreateBlobContainerAsync(containerName);
}
}