-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure-blobs.cs
More file actions
executable file
·46 lines (39 loc) · 1.59 KB
/
azure-blobs.cs
File metadata and controls
executable file
·46 lines (39 loc) · 1.59 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
// Program.cs
builder.Services.AddSingleton<AzureConnectors>();
// AzureConnectors.cs
public class AzureConnectors
{
private readonly Lazy<BlobServiceClient> _blobServiceClient;
public BlobServiceClient BlobServiceClient => _blobServiceClient.Value;
public BlobContainerClient GetBlobContainer(string name) => BlobServiceClient.GetBlobContainerClient(name);
public AzureConnectors(IConfiguration configuration, ILogger<AzureConnectors> logger)
{
_blobServiceClient = new(() =>
{
logger.LogInformation("Initialized BlobServiceClient");
return new BlobServiceClient(configuration.GetConnectionString("Storage"));
}, LazyThreadSafetyMode.ExecutionAndPublication);
}
}
// BlobUploadService.cs
public class BlobUploadService
{
private readonly AzureConnectors _azureConnectors;
public BlobUploadService(AzureConnectors azureConnectors)
{
_azureConnectors = azureConnectors;
}
public async Task UploadFile(Stream stream, string container, string blob, CancellationToken ctn)
{
var client = _azureConnectors.GetBlobContainer(container).GetBlockBlobClient(blob);
await client.UploadAsync(stream, new BlobHttpHeaders()
{
CacheControl = "max-age=4000",
ContentType = "image/jpg"
}, cancellationToken: ctn);
}
}
// appsettings.json
"ConnectionStrings": {
"Storage" : "DefaultEndpointsProtocol=https;AccountName=webinariolocalst;AccountKey=2mX0cYkeCnFhWg7QWaGRe4dcTSo8je+B8H25Y/oErS7aB2x34bBLwfWMkOeVsSA+/YmAMRIAaCZ++AStvMTjew==;EndpointSuffix=core.windows.net"
}