-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathAzureBlobStorageService.cs
More file actions
87 lines (70 loc) · 3.1 KB
/
Copy pathAzureBlobStorageService.cs
File metadata and controls
87 lines (70 loc) · 3.1 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
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
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Extensions.Options;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace LinkDotNet.Blog.Web.Features.Services.FileUpload;
public class AzureBlobStorageService : IBlobUploadService
{
private readonly IOptions<UploadConfiguration> azureBlobStorageConfiguration;
public AzureBlobStorageService(IOptions<UploadConfiguration> azureBlobStorageConfiguration)
{
this.azureBlobStorageConfiguration = azureBlobStorageConfiguration;
}
public async Task<string> UploadFileAsync(string fileName, Stream fileStream, UploadOptions options)
{
ArgumentNullException.ThrowIfNull(options);
var containerName = azureBlobStorageConfiguration.Value.ContainerName;
var client = CreateClient(azureBlobStorageConfiguration.Value);
var (rootContainer, subContainer) = SplitContainerName(containerName);
var blobContainerClient = client.GetBlobContainerClient(rootContainer);
var blobClient = blobContainerClient.GetBlobClient($"{subContainer}/{fileName}");
var blobOptions = new BlobUploadOptions();
if (options.SetCacheControlHeader)
{
blobOptions.HttpHeaders = new BlobHttpHeaders
{
CacheControl = "public, max-age=604800"
};
}
await blobClient.UploadAsync(fileStream, blobOptions);
return GetAssetUrl(blobClient.Uri.ToString(), azureBlobStorageConfiguration.Value);
}
private static (string rootContainer, string subContainer) SplitContainerName(string containerName)
{
var containerNames = containerName.Split('/', StringSplitOptions.RemoveEmptyEntries);
if (containerNames.Length == 0)
{
return (string.Empty, string.Empty);
}
var rootContainer = containerNames[0];
var subContainer = string.Join("/", containerNames.Skip(1));
return (rootContainer, subContainer);
}
private static BlobServiceClient CreateClient(UploadConfiguration configuration)
{
if (configuration.AuthenticationMode == AuthenticationMode.ConnectionString.Key)
{
var connectionString = configuration.ConnectionString
?? throw new InvalidOperationException("ConnectionString must be set when using ConnectionString authentication mode");
return new BlobServiceClient(connectionString);
}
var serviceUrl = configuration.ServiceUrl
?? throw new InvalidOperationException("ServiceUrl must be set when using Default authentication mode");
return new BlobServiceClient(new Uri(serviceUrl), new DefaultAzureCredential());
}
private static string GetAssetUrl(string blobUrl, UploadConfiguration config)
{
if (!config.IsCdnEnabled)
{
return blobUrl;
}
var cdnEndpoint = config.CdnEndpoint!.TrimEnd('/');
var blobUri = new Uri(blobUrl);
var path = blobUri.AbsolutePath;
return $"{cdnEndpoint}{path}";
}
}