Skip to content

Commit 985f9d5

Browse files
committed
Add timeout support to AmazonS3Client
1 parent 7e41144 commit 985f9d5

4 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/ImageSharp.Web.Providers.AWS/AmazonS3ClientFactory.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using Amazon;
6+
using Amazon.Runtime;
67
using Amazon.S3;
78

89
namespace SixLabors.ImageSharp.Web
@@ -25,6 +26,7 @@ public static AmazonS3Client CreateClient(IAWSS3BucketClientOptions options)
2526
// AccessSecret can be empty.
2627
// PathStyle endpoint doesn't support AccelerateEndpoint.
2728
AmazonS3Config config = new() { ServiceURL = options.Endpoint, ForcePathStyle = true, AuthenticationRegion = options.Region };
29+
SetTimeout(config, options.Timeout);
2830
return new AmazonS3Client(options.AccessKey, options.AccessSecret, config);
2931
}
3032
else if (!string.IsNullOrWhiteSpace(options.AccessKey))
@@ -33,18 +35,29 @@ public static AmazonS3Client CreateClient(IAWSS3BucketClientOptions options)
3335
Guard.NotNullOrWhiteSpace(options.Region, nameof(options.Region));
3436
var region = RegionEndpoint.GetBySystemName(options.Region);
3537
AmazonS3Config config = new() { RegionEndpoint = region, UseAccelerateEndpoint = options.UseAccelerateEndpoint };
38+
SetTimeout(config, options.Timeout);
3639
return new AmazonS3Client(options.AccessKey, options.AccessSecret, config);
3740
}
3841
else if (!string.IsNullOrWhiteSpace(options.Region))
3942
{
4043
var region = RegionEndpoint.GetBySystemName(options.Region);
4144
AmazonS3Config config = new() { RegionEndpoint = region, UseAccelerateEndpoint = options.UseAccelerateEndpoint };
45+
SetTimeout(config, options.Timeout);
4246
return new AmazonS3Client(config);
4347
}
4448
else
4549
{
4650
throw new ArgumentException("Invalid configuration.", nameof(options));
4751
}
4852
}
53+
54+
private static void SetTimeout(ClientConfig config, TimeSpan? timeout)
55+
{
56+
// We don't want to override the default timeout if it's not set.
57+
if (timeout.HasValue)
58+
{
59+
config.Timeout = timeout.Value;
60+
}
61+
}
4962
}
5063
}

src/ImageSharp.Web.Providers.AWS/Caching/AWSS3StorageCacheOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ public class AWSS3StorageCacheOptions : IAWSS3BucketClientOptions
2525

2626
/// <inheritdoc/>
2727
public bool UseAccelerateEndpoint { get; set; }
28+
29+
/// <inheritdoc/>
30+
public TimeSpan? Timeout { get; set; }
2831
}
2932
}

src/ImageSharp.Web.Providers.AWS/IAWSS3BucketClientOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ internal interface IAWSS3BucketClientOptions
4343
/// The feature must be enabled on the bucket. Follow AWS instruction on <see href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/transfer-acceleration.html"/>.
4444
/// </summary>
4545
bool UseAccelerateEndpoint { get; set; }
46+
47+
/// <summary>
48+
/// Gets or sets a value indicating the timeout for the S3 client.
49+
/// If the value is set, the value is assigned to the Timeout property of the HttpWebRequest/HttpClient object used to send requests.
50+
/// </summary>
51+
TimeSpan? Timeout { get; set; }
4652
}
4753
}

src/ImageSharp.Web.Providers.AWS/Providers/AWSS3StorageImageProviderOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@ public class AWSS3BucketClientOptions : IAWSS3BucketClientOptions
3838

3939
/// <inheritdoc/>
4040
public bool UseAccelerateEndpoint { get; set; }
41+
42+
/// <inheritdoc/>
43+
public TimeSpan? Timeout { get; set; }
4144
}
4245
}

0 commit comments

Comments
 (0)