-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathAmazonS3ClientFactory.cs
67 lines (63 loc) · 2.7 KB
/
AmazonS3ClientFactory.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
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
namespace SixLabors.ImageSharp.Web;
internal static class AmazonS3ClientFactory
{
/// <summary>
/// Creates a new bucket under the specified account if a bucket
/// with the same name does not already exist.
/// </summary>
/// <param name="options">The AWS S3 Storage cache options.</param>
/// <param name="serviceProvider">The current service provider.</param>
/// <returns>
/// A new <see cref="AmazonS3Client"/>.
/// </returns>
/// <exception cref="ArgumentException">Invalid configuration.</exception>
public static AmazonS3Client CreateClient(IAWSS3BucketClientOptions options, IServiceProvider serviceProvider)
{
if (options.S3ClientProvider != null)
{
return options.S3ClientProvider(options, serviceProvider);
}
else if (!string.IsNullOrWhiteSpace(options.Endpoint))
{
// AccessKey can be empty.
// AccessSecret can be empty.
// PathStyle endpoint doesn't support AccelerateEndpoint.
AmazonS3Config config = new() { ServiceURL = options.Endpoint, ForcePathStyle = true, AuthenticationRegion = options.Region };
SetTimeout(config, options.Timeout);
return new AmazonS3Client(options.AccessKey, options.AccessSecret, config);
}
else if (!string.IsNullOrWhiteSpace(options.AccessKey))
{
// AccessSecret can be empty.
Guard.NotNullOrWhiteSpace(options.Region, nameof(options.Region));
RegionEndpoint region = RegionEndpoint.GetBySystemName(options.Region);
AmazonS3Config config = new() { RegionEndpoint = region, UseAccelerateEndpoint = options.UseAccelerateEndpoint };
SetTimeout(config, options.Timeout);
return new AmazonS3Client(options.AccessKey, options.AccessSecret, config);
}
else if (!string.IsNullOrWhiteSpace(options.Region))
{
RegionEndpoint region = RegionEndpoint.GetBySystemName(options.Region);
AmazonS3Config config = new() { RegionEndpoint = region, UseAccelerateEndpoint = options.UseAccelerateEndpoint };
SetTimeout(config, options.Timeout);
return new AmazonS3Client(config);
}
else
{
throw new ArgumentException("Invalid configuration.", nameof(options));
}
}
private static void SetTimeout(ClientConfig config, TimeSpan? timeout)
{
// We don't want to override the default timeout if it's not set.
if (timeout.HasValue)
{
config.Timeout = timeout.Value;
}
}
}