-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlobStorageClient.cs
43 lines (37 loc) · 1.42 KB
/
BlobStorageClient.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
using Azure.Storage.Blobs;
using Azure.Storage.Sas;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Threading.Tasks;
namespace CodeSandbox.Orchestrator.Services
{
public class BlobStorageClient
{
private readonly BlobServiceClient _blobServiceClient;
private readonly string _containerName;
public BlobStorageClient(
BlobServiceClient blobServiceClient,
IConfiguration configuration)
{
_blobServiceClient = blobServiceClient;
_containerName = configuration["StorageContainer"];
}
public async Task<(string containerName, string blobName, string sasUrl)> Upload(
Stream stream)
{
var blobName = $"{Guid.NewGuid()}.cs";
var container = _blobServiceClient.GetBlobContainerClient(_containerName);
var blob = container.GetBlobClient(blobName);
await blob.UploadAsync(stream);
var sasUrl = blob.GenerateSasUri(BlobSasPermissions.Read, DateTimeOffset.UtcNow.AddMinutes(30));
return (_containerName, blobName, sasUrl.ToString());
}
public async Task Delete(string containerName, string blobName)
{
var container = _blobServiceClient.GetBlobContainerClient(containerName);
var blob = container.GetBlobClient(blobName);
await blob.DeleteAsync();
}
}
}