-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAzureStorageManager.cs
57 lines (52 loc) · 2.25 KB
/
AzureStorageManager.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
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.Storage.DataMovement;
using System.Threading.Tasks;
namespace Realms.LFS.Azure
{
/// <summary>
/// An implementation of the <see cref="RemoteStorageManager"/> that uploads data to Azure.
/// </summary>
public class AzureStorageManager : RemoteStorageManager
{
private readonly CloudBlobContainer _container;
/// <summary>
/// Initializes a new instance of the <see cref="AzureStorageManager"/> class with the
/// supplied <paramref name="connectionString"/>.
/// </summary>
/// <param name="config">The config of the Realm this file manager is tracking.</param>
/// <param name="connectionString">The connection string used to connect to Azure storage.</param>
/// <param name="container">
/// An optional argument indicating the container that will be used to upload data to.
/// </param>
public AzureStorageManager(RealmConfigurationBase config, string connectionString, string container = "realm-lfs-data")
: base(config)
{
var account = CloudStorageAccount.Parse(connectionString);
var client = account.CreateCloudBlobClient();
_container = client.GetContainerReference(container);
_container.CreateIfNotExists();
}
/// <inheritdoc/>
protected override async Task DeleteFileCore(string id)
{
var blob = _container.GetBlockBlobReference(id);
await blob.DeleteIfExistsAsync();
}
/// <inheritdoc/>
protected override async Task DownloadFileCore(string id, string file)
{
var context = new SingleTransferContext();
var blob = _container.GetBlockBlobReference(id);
await TransferManager.DownloadAsync(blob, file, null, context);
}
/// <inheritdoc/>
protected override async Task<string> UploadFileCore(string id, string file)
{
var context = new SingleTransferContext();
var blob = _container.GetBlockBlobReference(id);
await TransferManager.UploadAsync(file, blob, null, context);
return blob.Uri.AbsoluteUri;
}
}
}