Skip to content

Commit

Permalink
Breaking change: Make S3ServiceOptions take a RegionEndpoint enumerat…
Browse files Browse the repository at this point in the history
…ion instead of a string, and discard DefaultRegion from the constructor
  • Loading branch information
lilith committed Jun 15, 2020
1 parent 1fc0d39 commit 55a5319
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ namespace Imageflow.Server.Example

// Make S3 containers available at /ri/ and /imageflow-resources/
// If you use credentials, do not check them into your repository
services.AddImageflowS3Service(new S3ServiceOptions( RegionEndpoint.USEast1, null,null)
.MapPrefix("/ri/", "us-east-1", "resizer-images")
.MapPrefix("/imageflow-resources/", "us-west-2", "imageflow-resources"));
services.AddImageflowS3Service(new S3ServiceOptions( null,null)
.MapPrefix("/ri/", RegionEndpoint.USEast1, "resizer-images")
.MapPrefix("/imageflow-resources/", RegionEndpoint.USWest2, "imageflow-resources"));

// Make Azure container available at /azure
services.AddImageflowAzureBlobService(
Expand Down
8 changes: 4 additions & 4 deletions examples/Imageflow.Server.Example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public void ConfigureServices(IServiceCollection services)

// Make S3 containers available at /ri/ and /imageflow-resources/
// If you use credentials, do not check them into your repository
services.AddImageflowS3Service(new S3ServiceOptions( RegionEndpoint.USEast1, null,null)
.MapPrefix("/ri/", "us-east-1", "resizer-images")
.MapPrefix("/imageflow-resources/", "us-west-2", "imageflow-resources"));

services.AddImageflowS3Service(new S3ServiceOptions( null,null)
.MapPrefix("/ri/", RegionEndpoint.USEast1, "resizer-images")
.MapPrefix("/imageflow-resources/", RegionEndpoint.USWest2, "imageflow-resources"));
// Make Azure container available at /azure
services.AddImageflowAzureBlobService(
new AzureBlobServiceOptions(
Expand Down
4 changes: 3 additions & 1 deletion src/Imageflow.Server.Storage.S3/PrefixMapping.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Amazon;

namespace Imageflow.Server.Storage.S3
{
internal struct PrefixMapping
{
internal string Prefix;
internal string Region;
internal RegionEndpoint Region;
internal string Bucket;
internal string BlobPrefix;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Imageflow.Server.Storage.S3/S3Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public class S3Service : IBlobProvider

private readonly List<string> prefixes = new List<string>();

private readonly AmazonS3Client client;
private readonly AWSCredentials credentials;
public S3Service(S3ServiceOptions options, ILogger<S3Service> logger)
{

if (options.AccessKeyId == null)
{
client = new AmazonS3Client(new AnonymousAWSCredentials(),options.DefaultRegion);
credentials = new AnonymousAWSCredentials();
}
else
{
client = new AmazonS3Client(new BasicAWSCredentials(options.AccessKeyId, options.SecretAccessKey), options.DefaultRegion);
credentials = new BasicAWSCredentials(options.AccessKeyId, options.SecretAccessKey);
}

foreach (var m in options.mappings)
Expand Down Expand Up @@ -64,6 +64,7 @@ public async Task<IBlobData> Fetch(string virtualPath)
: mapping.BlobPrefix + "/" + virtualPath.Substring(prefix.Length).TrimStart('/');

try {
using var client = new AmazonS3Client(credentials, mapping.Region);
var req = new Amazon.S3.Model.GetObjectRequest() { BucketName = mapping.Bucket, Key = key };

var s = await client.GetObjectAsync(req);
Expand Down
8 changes: 3 additions & 5 deletions src/Imageflow.Server.Storage.S3/S3ServiceOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ public class S3ServiceOptions

internal readonly string AccessKeyId;
internal readonly string SecretAccessKey;
internal readonly RegionEndpoint DefaultRegion;
internal readonly List<PrefixMapping> mappings = new List<PrefixMapping>();
public S3ServiceOptions( RegionEndpoint defaultRegion, string accessKeyId, string secretAccessKey)
public S3ServiceOptions(string accessKeyId, string secretAccessKey)
{
DefaultRegion = defaultRegion;
this.AccessKeyId = accessKeyId;
this.SecretAccessKey = secretAccessKey;
}

public S3ServiceOptions MapPrefix(string prefix, string region, string bucket)
public S3ServiceOptions MapPrefix(string prefix, RegionEndpoint region, string bucket)
=> MapPrefix(prefix, region, bucket, "");

public S3ServiceOptions MapPrefix(string prefix, string region, string bucket, string blobPrefix)
public S3ServiceOptions MapPrefix(string prefix, RegionEndpoint region, string bucket, string blobPrefix)
{
prefix = prefix.TrimStart('/').TrimEnd('/');
if (prefix.Length == 0)
Expand Down
4 changes: 2 additions & 2 deletions tests/Imageflow.Server.Tests/IntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public async void TestAmazonS3()
{
services.AddImageflowDiskCache(new DiskCacheOptions(diskCacheDir) {AsyncWrites = false});
services.AddImageflowS3Service(
new S3ServiceOptions(RegionEndpoint.USEast1, null, null)
.MapPrefix("/ri/", "us-east-1", "resizer-images"));
new S3ServiceOptions(null, null)
.MapPrefix("/ri/", RegionEndpoint.USEast1, "resizer-images"));
})
.ConfigureWebHost(webHost =>
{
Expand Down

0 comments on commit 55a5319

Please sign in to comment.