-
Notifications
You must be signed in to change notification settings - Fork 0
/
blobManager.cs
99 lines (71 loc) · 3.05 KB
/
blobManager.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
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
88
89
90
91
92
93
94
95
96
97
98
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace COLT
{
public class blobManager
{
private CloudStorageAccount _storageAccount;
private CloudBlobClient _blobClient;
private CloudBlobContainer _container;
public blobManager(string blobStorageAccountConnectionString, string blobContainer)
{
_storageAccount = CloudStorageAccount.Parse(blobStorageAccountConnectionString);
_blobClient = _storageAccount.CreateCloudBlobClient();
_container = _blobClient.GetContainerReference(blobContainer);
_container.CreateIfNotExistsAsync();
}
public string GeneratePreSignedURL(int durationInMinutes, string key)
{
var blob = _container.GetBlobReference(key);
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(durationInMinutes);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read;
string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);
Uri sourceUri = new Uri(blob.Uri + sasBlobToken);
return sourceUri.ToString();
}
public string GetContainerSASRead(int durationInMinutes)
{
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(durationInMinutes);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read;
return _container.GetSharedAccessSignature(sasConstraints);
}
public string GetBlobURL(string key)
{
return _container.GetBlobReference(key).Uri.ToString();
}
public async Task UploadFileAsync(byte[] bytes, string name)
{
var blob = _container.GetBlockBlobReference(name);
await blob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);
}
public async Task<List<string>> GetFileListAsync(string prefix)
{
List<string> objects = new List<string>();
BlobContinuationToken continuationToken = null;
do
{
var response = await _container.ListBlobsSegmentedAsync(prefix, continuationToken);
continuationToken = response.ContinuationToken;
foreach(CloudBlockBlob blob in response.Results)
{
objects.Add(blob.Name);
}
}
while (continuationToken != null);
return objects;
}
}
}