forked from antonputra/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonS3Uploader.cs
More file actions
30 lines (24 loc) · 930 Bytes
/
Copy pathAmazonS3Uploader.cs
File metadata and controls
30 lines (24 loc) · 930 Bytes
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
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace cs_app_aot;
public sealed class AmazonS3Uploader
{
private readonly AmazonS3Client _client;
public AmazonS3Uploader(string? accessKey, string? secretKey, string? endpoint, string? region)
{
var creds = new BasicAWSCredentials(accessKey, secretKey);
var cfg = new AmazonS3Config
{
ForcePathStyle = true
};
if (!string.IsNullOrWhiteSpace(endpoint))
cfg.ServiceURL = endpoint;
if (!string.IsNullOrWhiteSpace(region))
cfg.RegionEndpoint = RegionEndpoint.GetBySystemName(region);
_client = new AmazonS3Client(creds, cfg);
}
public Task Upload(string? bucket, string key, string? path, CancellationToken ct = default)
=> _client.PutObjectAsync(new PutObjectRequest { BucketName = bucket, Key = key, FilePath = path }, ct);
}