Skip to content

Commit 98a43eb

Browse files
committed
Adding CacheFolder config to AzureBlobStorageCache
1 parent 6096704 commit 98a43eb

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/ImageSharp.Web.Providers.Azure/Caching/AzureBlobStorageCache.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace SixLabors.ImageSharp.Web.Caching.Azure;
1616
public class AzureBlobStorageCache : IImageCache
1717
{
1818
private readonly BlobContainerClient container;
19+
private readonly string cacheFolder;
1920

2021
/// <summary>
2122
/// Initializes a new instance of the <see cref="AzureBlobStorageCache"/> class.
@@ -27,12 +28,15 @@ public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions
2728
AzureBlobStorageCacheOptions options = cacheOptions.Value;
2829

2930
this.container = new BlobContainerClient(options.ConnectionString, options.ContainerName);
31+
this.cacheFolder = string.IsNullOrEmpty(options.CacheFolder)
32+
? string.Empty
33+
: options.CacheFolder.Trim().Trim('/') + '/';
3034
}
3135

3236
/// <inheritdoc/>
3337
public async Task<IImageCacheResolver?> GetAsync(string key)
3438
{
35-
BlobClient blob = this.container.GetBlobClient(key);
39+
BlobClient blob = this.container.GetBlobClient(this.GetBlobName(key));
3640

3741
if (!await blob.ExistsAsync())
3842
{
@@ -45,7 +49,7 @@ public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions
4549
/// <inheritdoc/>
4650
public Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata)
4751
{
48-
BlobClient blob = this.container.GetBlobClient(key);
52+
BlobClient blob = this.container.GetBlobClient(this.GetBlobName(key));
4953

5054
BlobHttpHeaders headers = new()
5155
{
@@ -79,4 +83,7 @@ public static Response<BlobContainerInfo> CreateIfNotExists(
7983
AzureBlobStorageCacheOptions options,
8084
PublicAccessType accessType)
8185
=> new BlobContainerClient(options.ConnectionString, options.ContainerName).CreateIfNotExists(accessType);
86+
87+
private string GetBlobName(string key)
88+
=> this.cacheFolder + key;
8289
}

src/ImageSharp.Web.Providers.Azure/Caching/AzureBlobStorageCacheOptions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ public class AzureBlobStorageCacheOptions
1616

1717
/// <summary>
1818
/// Gets or sets the Azure Blob Storage container name.
19-
/// Must conform to Azure Blob Storage container naming guidlines.
19+
/// Must conform to Azure Blob Storage container naming guidelines.
2020
/// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names"/>
2121
/// </summary>
2222
public string ContainerName { get; set; } = null!;
23+
24+
/// <summary>
25+
/// Gets or sets the cache folder name that'll store cache files under the configured container.
26+
/// Must conform to Azure Blob Storage directory naming guidelines.
27+
/// <see href="https://learn.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#directory-names"/>
28+
/// </summary>
29+
public string CacheFolder { get; set; } = null!;
2330
}

0 commit comments

Comments
 (0)