-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCloudflareR2Service.cs
More file actions
47 lines (40 loc) · 1.48 KB
/
CloudflareR2Service.cs
File metadata and controls
47 lines (40 loc) · 1.48 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
using Amazon.S3;
using Amazon.S3.Model;
using ByteSync.ServerCommon.Business.Settings;
using ByteSync.ServerCommon.Interfaces.Services;
using Microsoft.Extensions.Options;
namespace ByteSync.ServerCommon.Services;
public class CloudflareR2Service : ICloudflareR2Service
{
private readonly CloudflareR2Settings _cloudflareR2Settings;
private AmazonS3Client? _s3Client;
public CloudflareR2Service(IOptions<CloudflareR2Settings> cloudflareR2Settings)
{
_cloudflareR2Settings = cloudflareR2Settings.Value;
}
public async Task<ListObjectsV2Response> ListObjectsAsync(ListObjectsV2Request request, CancellationToken cancellationToken)
{
var s3Client = GetS3Client();
return await s3Client.ListObjectsV2Async(request, cancellationToken);
}
public async Task<DeleteObjectResponse> DeleteObjectAsync(DeleteObjectRequest request, CancellationToken cancellationToken)
{
var s3Client = GetS3Client();
return await s3Client.DeleteObjectAsync(request, cancellationToken);
}
private AmazonS3Client GetS3Client()
{
if (_s3Client == null)
{
_s3Client = new AmazonS3Client(
_cloudflareR2Settings.AccessKeyId,
_cloudflareR2Settings.SecretAccessKey,
new AmazonS3Config
{
ServiceURL = _cloudflareR2Settings.Endpoint,
ForcePathStyle = true
});
}
return _s3Client;
}
}