Skip to content

Commit 40b5b8c

Browse files
committed
Adding CacheFolder config to AWSS3StorageCache
1 parent 5ff144c commit 40b5b8c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class AWSS3StorageCache : IImageCache
1717
{
1818
private readonly IAmazonS3 amazonS3Client;
1919
private readonly string bucketName;
20+
private readonly string cacheFolder;
2021

2122
/// <summary>
2223
/// Initializes a new instance of the <see cref="AWSS3StorageCache"/> class.
@@ -28,17 +29,21 @@ public AWSS3StorageCache(IOptions<AWSS3StorageCacheOptions> cacheOptions)
2829
AWSS3StorageCacheOptions options = cacheOptions.Value;
2930
this.bucketName = options.BucketName;
3031
this.amazonS3Client = AmazonS3ClientFactory.CreateClient(options);
32+
this.cacheFolder = string.IsNullOrEmpty(options.CacheFolder)
33+
? string.Empty
34+
: options.CacheFolder.Trim().Trim('/') + '/';
3135
}
3236

3337
/// <inheritdoc/>
3438
public async Task<IImageCacheResolver?> GetAsync(string key)
3539
{
36-
GetObjectMetadataRequest request = new() { BucketName = this.bucketName, Key = key };
40+
string keyWithFolder = this.GetKeyWithFolder(key);
41+
GetObjectMetadataRequest request = new() { BucketName = this.bucketName, Key = keyWithFolder };
3742
try
3843
{
3944
// HEAD request throws a 404 if not found.
4045
MetadataCollection metadata = (await this.amazonS3Client.GetObjectMetadataAsync(request)).Metadata;
41-
return new AWSS3StorageCacheResolver(this.amazonS3Client, this.bucketName, key, metadata);
46+
return new AWSS3StorageCacheResolver(this.amazonS3Client, this.bucketName, keyWithFolder, metadata);
4247
}
4348
catch
4449
{
@@ -52,7 +57,7 @@ public Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata)
5257
PutObjectRequest request = new()
5358
{
5459
BucketName = this.bucketName,
55-
Key = key,
60+
Key = this.GetKeyWithFolder(key),
5661
ContentType = metadata.ContentType,
5762
InputStream = stream,
5863
AutoCloseStream = false
@@ -165,4 +170,7 @@ public static TResult RunSync<TResult>(Func<Task<TResult>> task)
165170
}).Unwrap().GetAwaiter().GetResult();
166171
}
167172
}
173+
174+
private string GetKeyWithFolder(string key)
175+
=> this.cacheFolder + key;
168176
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public class AWSS3StorageCacheOptions : IAWSS3BucketClientOptions
1414
/// <inheritdoc/>
1515
public string BucketName { get; set; } = null!;
1616

17+
/// <summary>
18+
/// Gets or sets the cache folder's name that'll store cache files under the configured bucket.
19+
/// </summary>
20+
public string CacheFolder { get; set; } = null!;
21+
1722
/// <inheritdoc/>
1823
public string? AccessKey { get; set; }
1924

0 commit comments

Comments
 (0)