Skip to content

Commit 430c100

Browse files
Added factory class
1 parent 086f932 commit 430c100

5 files changed

Lines changed: 74 additions & 101 deletions

File tree

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

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Amazon.S3;
1212
using Amazon.S3.Model;
1313
using Microsoft.Extensions.Options;
14+
using SixLabors.ImageSharp.Web.Factories;
15+
using SixLabors.ImageSharp.Web.Providers.AWS;
1416
using SixLabors.ImageSharp.Web.Resolvers;
1517
using SixLabors.ImageSharp.Web.Resolvers.AWS;
1618

@@ -26,6 +28,7 @@ public class AWSS3StorageCache : IImageCache
2628
TaskCreationOptions.None,
2729
TaskContinuationOptions.None,
2830
TaskScheduler.Default);
31+
private static readonly AWSS3Factory AWSS3Factory = new ();
2932

3033
private readonly IAmazonS3 amazonS3Client;
3134
private readonly string bucket;
@@ -34,12 +37,12 @@ public class AWSS3StorageCache : IImageCache
3437
/// Initializes a new instance of the <see cref="AWSS3StorageCache"/> class.
3538
/// </summary>
3639
/// <param name="cacheOptions">The cache options.</param>
37-
public AWSS3StorageCache(IOptions<AWSS3StorageCacheOptions> cacheOptions)
40+
public AWSS3StorageCache(IOptions<AWSS3BucketClientOptions> cacheOptions)
3841
{
3942
Guard.NotNull(cacheOptions, nameof(cacheOptions));
40-
AWSS3StorageCacheOptions options = cacheOptions.Value;
43+
AWSS3BucketClientOptions options = cacheOptions.Value;
4144
this.bucket = options.BucketName;
42-
this.amazonS3Client = CreateClient(options);
45+
this.amazonS3Client = AWSS3Factory.CreateClient(options);
4346
}
4447

4548
/// <inheritdoc/>
@@ -95,10 +98,10 @@ public Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata)
9598
/// created bucket. If the container already exists, <see langword="null"/>.
9699
/// </returns>
97100
public static PutBucketResponse CreateIfNotExists(
98-
AWSS3StorageCacheOptions options,
101+
AWSS3BucketClientOptions options,
99102
S3CannedACL acl)
100103
{
101-
AmazonS3Client client = CreateClient(options);
104+
AmazonS3Client client = AWSS3Factory.CreateClient(options);
102105

103106
bool foundBucket = false;
104107
ListBucketsResponse listBucketsResponse = RunSync(() => client.ListBucketsAsync());
@@ -126,33 +129,6 @@ public static PutBucketResponse CreateIfNotExists(
126129
return null;
127130
}
128131

129-
private static AmazonS3Client CreateClient(AWSS3StorageCacheOptions options)
130-
{
131-
if (!string.IsNullOrWhiteSpace(options.Endpoint))
132-
{
133-
// AccessKey can be empty.
134-
// AccessSecret can be empty.
135-
AmazonS3Config config = new() { ServiceURL = options.Endpoint, ForcePathStyle = true };
136-
return new AmazonS3Client(options.AccessKey, options.AccessSecret, config);
137-
}
138-
else if (!string.IsNullOrWhiteSpace(options.AccessKey))
139-
{
140-
// AccessSecret can be empty.
141-
Guard.NotNullOrWhiteSpace(options.Region, nameof(options.Region));
142-
var region = RegionEndpoint.GetBySystemName(options.Region);
143-
return new AmazonS3Client(options.AccessKey, options.AccessSecret, region);
144-
}
145-
else if (!string.IsNullOrWhiteSpace(options.Region))
146-
{
147-
var region = RegionEndpoint.GetBySystemName(options.Region);
148-
return new AmazonS3Client(region);
149-
}
150-
else
151-
{
152-
throw new ArgumentException("Invalid configuration.", nameof(options));
153-
}
154-
}
155-
156132
/// <summary>
157133
/// Executes an async <see cref="Task{TResult}"/> method which has
158134
/// a <paramref name="task"/> return type synchronously.

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

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using System.Globalization;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Amazon;
9+
using Amazon.S3;
10+
using Amazon.S3.Model;
11+
using SixLabors.ImageSharp.Web.Caching.AWS;
12+
using SixLabors.ImageSharp.Web.Providers.AWS;
13+
14+
namespace SixLabors.ImageSharp.Web.Factories
15+
{
16+
internal class AWSS3Factory
17+
{
18+
/// <summary>
19+
/// Creates a new bucket under the specified account if a bucket
20+
/// with the same name does not already exist.
21+
/// </summary>
22+
/// <param name="options">The AWS S3 Storage cache options.</param>
23+
/// <param name="acl">
24+
/// Specifies whether data in the bucket may be accessed publicly and the level of access.
25+
/// <see cref="S3CannedACL.PublicRead"/> specifies full public read access for bucket
26+
/// and object data. <see cref="S3CannedACL.Private"/> specifies that the bucket
27+
/// data is private to the account owner.
28+
/// </param>
29+
/// <returns>
30+
/// If the bucket does not already exist, a <see cref="PutBucketResponse"/> describing the newly
31+
/// created bucket. If the container already exists, <see langword="null"/>.
32+
/// </returns>
33+
34+
public static AmazonS3Client CreateClient(AWSS3BucketClientOptions options)
35+
{
36+
if (!string.IsNullOrWhiteSpace(options.Endpoint))
37+
{
38+
// AccessKey can be empty.
39+
// AccessSecret can be empty.
40+
AmazonS3Config config = new() { ServiceURL = options.Endpoint, ForcePathStyle = true };
41+
return new AmazonS3Client(options.AccessKey, options.AccessSecret, config);
42+
}
43+
else if (!string.IsNullOrWhiteSpace(options.AccessKey))
44+
{
45+
// AccessSecret can be empty.
46+
Guard.NotNullOrWhiteSpace(options.Region, nameof(options.Region));
47+
var region = RegionEndpoint.GetBySystemName(options.Region);
48+
return new AmazonS3Client(options.AccessKey, options.AccessSecret, region);
49+
}
50+
else if (!string.IsNullOrWhiteSpace(options.Region))
51+
{
52+
var region = RegionEndpoint.GetBySystemName(options.Region);
53+
return new AmazonS3Client(region);
54+
}
55+
else
56+
{
57+
throw new ArgumentException("Invalid configuration.", nameof(options));
58+
}
59+
}
60+
}
61+
}

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.AspNetCore.Http;
1111
using Microsoft.AspNetCore.Http.Extensions;
1212
using Microsoft.Extensions.Options;
13+
using SixLabors.ImageSharp.Web.Factories;
1314
using SixLabors.ImageSharp.Web.Resolvers;
1415
using SixLabors.ImageSharp.Web.Resolvers.AWS;
1516

@@ -20,6 +21,8 @@ namespace SixLabors.ImageSharp.Web.Providers.AWS
2021
/// </summary>
2122
public class AWSS3StorageImageProvider : IImageProvider
2223
{
24+
private static readonly AWSS3Factory AWSS3Factory = new();
25+
2326
/// <summary>
2427
/// Character array to remove from paths.
2528
/// </summary>
@@ -54,33 +57,7 @@ public AWSS3StorageImageProvider(IOptions<AWSS3StorageImageProviderOptions> stor
5457

5558
foreach (AWSS3BucketClientOptions bucket in this.storageOptions.S3Buckets)
5659
{
57-
AmazonS3Client s3Client;
58-
if (!string.IsNullOrEmpty(bucket.Endpoint) &&
59-
bucket.AccessKey != null &&
60-
bucket.AccessSecret != null)
61-
{
62-
var config = new AmazonS3Config
63-
{
64-
ServiceURL = bucket.Endpoint,
65-
ForcePathStyle = true
66-
};
67-
68-
s3Client = new AmazonS3Client(bucket.AccessKey, bucket.AccessSecret, config);
69-
}
70-
else if (!string.IsNullOrEmpty(bucket.AccessKey) &&
71-
!string.IsNullOrEmpty(bucket.AccessSecret) &&
72-
!string.IsNullOrEmpty(bucket.Region))
73-
{
74-
var region = RegionEndpoint.GetBySystemName(bucket.Region);
75-
s3Client = new AmazonS3Client(bucket.AccessKey, bucket.AccessSecret, region);
76-
}
77-
else
78-
{
79-
var region = RegionEndpoint.GetBySystemName(bucket.Region);
80-
s3Client = new AmazonS3Client(region);
81-
}
82-
83-
this.buckets.Add(bucket.BucketName, s3Client);
60+
this.buckets.Add(bucket.BucketName, AWSS3Factory.CreateClient(bucket));
8461
}
8562
}
8663

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected override void ConfigureServices(IServiceCollection services) =>
8888
.AddProvider(AWSS3StorageImageProviderFactory.Create)
8989
.AddProvider<PhysicalFileSystemProvider>()
9090
.AddProcessor<CacheBusterWebProcessor>()
91-
.Configure<AWSS3StorageCacheOptions>(options =>
91+
.Configure<AWSS3BucketClientOptions>(options =>
9292
{
9393
options.Endpoint = TestConstants.AWSEndpoint;
9494
options.BucketName = TestConstants.AWSCacheBucketName;

0 commit comments

Comments
 (0)