Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aws s3 client timeout #287

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ImageSharp.Web.Providers.AWS/AmazonS3ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;

namespace SixLabors.ImageSharp.Web
Expand All @@ -25,6 +26,7 @@ public static AmazonS3Client CreateClient(IAWSS3BucketClientOptions options)
// 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))
Expand All @@ -33,18 +35,29 @@ public static AmazonS3Client CreateClient(IAWSS3BucketClientOptions options)
Guard.NotNullOrWhiteSpace(options.Region, nameof(options.Region));
var 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))
{
var 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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;

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

/// <inheritdoc/>
public bool UseAccelerateEndpoint { get; set; }

/// <inheritdoc/>
public TimeSpan? Timeout { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/ImageSharp.Web.Providers.AWS/IAWSS3BucketClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;

namespace SixLabors.ImageSharp.Web
{
/// <summary>
Expand Down Expand Up @@ -43,5 +45,11 @@ internal interface IAWSS3BucketClientOptions
/// 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"/>.
/// </summary>
bool UseAccelerateEndpoint { get; set; }

/// <summary>
/// Gets or sets a value indicating the timeout for the S3 client.
/// If the value is set, the value is assigned to the Timeout property of the HttpWebRequest/HttpClient object used to send requests.
/// </summary>
TimeSpan? Timeout { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Collections.Generic;

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

/// <inheritdoc/>
public bool UseAccelerateEndpoint { get; set; }

/// <inheritdoc/>
public TimeSpan? Timeout { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ protected override void ConfigureCustomServices(IServiceCollection services, IIm
BucketName = TestConstants.AWSBucketName,
AccessKey = TestConstants.AWSAccessKey,
AccessSecret = TestConstants.AWSAccessSecret,
Region = TestConstants.AWSRegion
Region = TestConstants.AWSRegion,
Timeout = TestConstants.AWSTimeout,
}))
.AddProvider(AWSS3StorageImageProviderFactory.Create)
.Configure<AWSS3StorageCacheOptions>(o =>
Expand All @@ -31,6 +32,7 @@ protected override void ConfigureCustomServices(IServiceCollection services, IIm
o.AccessKey = TestConstants.AWSAccessKey;
o.AccessSecret = TestConstants.AWSAccessSecret;
o.Region = TestConstants.AWSRegion;
o.Timeout = TestConstants.AWSTimeout;

AWSS3StorageCache.CreateIfNotExists(o, S3CannedACL.Private);
})
Expand Down
3 changes: 3 additions & 0 deletions tests/ImageSharp.Web.Tests/TestUtilities/TestConstants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;

namespace SixLabors.ImageSharp.Web.Tests.TestUtilities
{
public static class TestConstants
Expand All @@ -18,5 +20,6 @@ public static class TestConstants
public const string PhysicalTestImage = "http://localhost/" + ImagePath;
public const string AzureTestImage = "http://localhost/" + AzureContainerName + "/" + ImagePath;
public const string AWSTestImage = "http://localhost/" + AWSBucketName + "/" + ImagePath;
public static readonly TimeSpan AWSTimeout = TimeSpan.FromSeconds(10);
}
}