@@ -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}
0 commit comments