-
Notifications
You must be signed in to change notification settings - Fork 2
/
S3StorageManager.cs
76 lines (68 loc) · 2.65 KB
/
S3StorageManager.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
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using System.Threading.Tasks;
namespace Realms.LFS.S3
{
/// <summary>
/// An implementation of the <see cref="RemoteStorageManager"/> that uploads data to AWS S3.
/// </summary>
public class S3StorageManager : RemoteStorageManager
{
private readonly AmazonS3Client _s3Client;
private readonly string _bucket;
/// <summary>
/// Initializes a new instance of the <see cref="S3StorageManager"/> class with the
/// supplied <paramref name="credentials"/>.
/// </summary>
/// <param name="config">The config of the Realm this file manager is tracking.</param>
/// <param name="credentials">The credentials used to connect to the S3 bucket.</param>
/// <param name="region">The region where the bucket is located</param>
/// <param name="bucket">
/// An optional argument indicating the bucket that will be used to upload data to.
/// </param>
public S3StorageManager(RealmConfigurationBase config, AWSCredentials credentials, RegionEndpoint region, string bucket = "realm-lfs-data")
: base(config)
{
_s3Client = new AmazonS3Client(credentials, region);
_bucket = bucket;
}
/// <inheritdoc/>
protected override async Task DeleteFileCore(string id)
{
await _s3Client.DeleteObjectAsync(new DeleteObjectRequest
{
BucketName = _bucket,
Key = id
});
}
/// <inheritdoc/>
protected override async Task DownloadFileCore(string id, string file)
{
var fileTransferUtility = new TransferUtility(_s3Client);
await fileTransferUtility.DownloadAsync(new TransferUtilityDownloadRequest
{
Key = id,
BucketName = _bucket,
FilePath = file,
});
}
/// <inheritdoc/>
protected override async Task<string> UploadFileCore(string id, string file)
{
var fileTransferUtility = new TransferUtility(_s3Client);
var fileTransferUtilityRequest = new TransferUtilityUploadRequest
{
BucketName = _bucket,
FilePath = file,
Key = id,
StorageClass = S3StorageClass.StandardInfrequentAccess,
CannedACL = S3CannedACL.PublicRead,
};
await fileTransferUtility.UploadAsync(fileTransferUtilityRequest);
return $"https://{_bucket}.s3.amazonaws.com/{id}";
}
}
}