-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathAzureUpload.cs
More file actions
73 lines (57 loc) · 2.49 KB
/
AzureUpload.cs
File metadata and controls
73 lines (57 loc) · 2.49 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
using System;
using System.IO;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs;
using Azure;
using System.Net;
using System.Threading.Tasks;
namespace Bot.Builder.Community.Components.Azure.BlobUpload.Upload
{
internal class AzureUpload : IAzureUpload
{
readonly BlobServiceClient _blobServiceClient;
BlobContainerClient _blobContainerClient;
private readonly PublicAccessType _publicAccessType;
private readonly DeleteSnapshotsOption _deleteSnapshotsOption;
public AzureUpload(string connectionString, string containerName,
PublicAccessType publicAccessType = PublicAccessType.None, DeleteSnapshotsOption deleteSnapshotsOption = DeleteSnapshotsOption.None)
{
_blobServiceClient = new BlobServiceClient(connectionString);
_blobContainerClient = _blobServiceClient.GetBlobContainerClient(containerName);
this._publicAccessType = publicAccessType;
this._deleteSnapshotsOption = deleteSnapshotsOption;
_blobServiceClient = new BlobServiceClient(connectionString);
CreateContainer(containerName);
if (_blobContainerClient == null)
throw new Exception("Create Container is failed");
}
private async void CreateContainer(string containerName)
{
try
{
_blobContainerClient = _blobServiceClient.GetBlobContainerClient(containerName);
if (_blobContainerClient != null)
{
bool isExits = await _blobContainerClient.ExistsAsync();
if (isExits)
return;
await _blobContainerClient.CreateIfNotExistsAsync(publicAccessType: _publicAccessType, cancellationToken: default);
}
}
catch (RequestFailedException)
{
}
}
public async Task<string> UploadAsync(string fileUrl, string fileName)
{
var wc = new WebClient();
var stream = new MemoryStream(wc.DownloadData(fileUrl));
var blob = _blobContainerClient.GetBlobClient(fileName);
if (_deleteSnapshotsOption == DeleteSnapshotsOption.None)
return blob.Uri.AbsoluteUri;
await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);
var result = await blob.UploadAsync(stream);
return blob.Uri.AbsoluteUri;
}
}
}