Skip to content

Commit d8c16d4

Browse files
Merge pull request #287 from mdupras/aws-s3-client-timeout
Aws s3 client timeout
2 parents 7e41144 + 0823d89 commit d8c16d4

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
lines changed

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
3+
using System;
34

45
namespace SixLabors.ImageSharp.Web.Caching.AWS
56
{
@@ -25,5 +26,8 @@ public class AWSS3StorageCacheOptions : IAWSS3BucketClientOptions
2526

2627
/// <inheritdoc/>
2728
public bool UseAccelerateEndpoint { get; set; }
29+
30+
/// <inheritdoc/>
31+
public TimeSpan? Timeout { get; set; }
2832
}
2933
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
5+
46
namespace SixLabors.ImageSharp.Web
57
{
68
/// <summary>
@@ -43,5 +45,11 @@ internal interface IAWSS3BucketClientOptions
4345
/// 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"/>.
4446
/// </summary>
4547
bool UseAccelerateEndpoint { get; set; }
48+
49+
/// <summary>
50+
/// Gets or sets a value indicating the timeout for the S3 client.
51+
/// If the value is set, the value is assigned to the Timeout property of the HttpWebRequest/HttpClient object used to send requests.
52+
/// </summary>
53+
TimeSpan? Timeout { get; set; }
4654
}
4755
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using System.Collections.Generic;
56

67
namespace SixLabors.ImageSharp.Web.Providers.AWS
@@ -38,5 +39,8 @@ public class AWSS3BucketClientOptions : IAWSS3BucketClientOptions
3839

3940
/// <inheritdoc/>
4041
public bool UseAccelerateEndpoint { get; set; }
42+
43+
/// <inheritdoc/>
44+
public TimeSpan? Timeout { get; set; }
4145
}
4246
}

tests/ImageSharp.Web.Tests/TestUtilities/AWSS3StorageCacheTestServerFixture.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ protected override void ConfigureCustomServices(IServiceCollection services, IIm
2121
BucketName = TestConstants.AWSBucketName,
2222
AccessKey = TestConstants.AWSAccessKey,
2323
AccessSecret = TestConstants.AWSAccessSecret,
24-
Region = TestConstants.AWSRegion
24+
Region = TestConstants.AWSRegion,
25+
Timeout = TestConstants.AWSTimeout,
2526
}))
2627
.AddProvider(AWSS3StorageImageProviderFactory.Create)
2728
.Configure<AWSS3StorageCacheOptions>(o =>
@@ -31,6 +32,7 @@ protected override void ConfigureCustomServices(IServiceCollection services, IIm
3132
o.AccessKey = TestConstants.AWSAccessKey;
3233
o.AccessSecret = TestConstants.AWSAccessSecret;
3334
o.Region = TestConstants.AWSRegion;
35+
o.Timeout = TestConstants.AWSTimeout;
3436

3537
AWSS3StorageCache.CreateIfNotExists(o, S3CannedACL.Private);
3638
})

tests/ImageSharp.Web.Tests/TestUtilities/TestConstants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
5+
46
namespace SixLabors.ImageSharp.Web.Tests.TestUtilities
57
{
68
public static class TestConstants
@@ -18,5 +20,6 @@ public static class TestConstants
1820
public const string PhysicalTestImage = "http://localhost/" + ImagePath;
1921
public const string AzureTestImage = "http://localhost/" + AzureContainerName + "/" + ImagePath;
2022
public const string AWSTestImage = "http://localhost/" + AWSBucketName + "/" + ImagePath;
23+
public static readonly TimeSpan AWSTimeout = TimeSpan.FromSeconds(10);
2124
}
2225
}

0 commit comments

Comments
 (0)