Skip to content

Commit 83c1249

Browse files
Merge pull request #361 from marklagendijk/blob-container-provider
Feature: Added BlobContainerClientProvider option to AzureBlobStorageCache and AzureBlobStorageImageProvider
2 parents d034c29 + 369ee18 commit 83c1249

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ public class AzureBlobStorageCache : IImageCache
2222
/// Initializes a new instance of the <see cref="AzureBlobStorageCache"/> class.
2323
/// </summary>
2424
/// <param name="cacheOptions">The cache options.</param>
25-
public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions)
25+
/// <param name="serviceProvider">The current service provider.</param>
26+
public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions, IServiceProvider serviceProvider)
2627
{
2728
Guard.NotNull(cacheOptions, nameof(cacheOptions));
2829
AzureBlobStorageCacheOptions options = cacheOptions.Value;
2930

30-
this.container = new BlobContainerClient(options.ConnectionString, options.ContainerName);
31+
this.container = options.BlobContainerClientProvider == null
32+
? new BlobContainerClient(options.ConnectionString, options.ContainerName)
33+
: options.BlobContainerClientProvider(options, serviceProvider);
3134
this.cacheFolder = string.IsNullOrEmpty(options.CacheFolder)
3235
? string.Empty
3336
: options.CacheFolder.Trim().Trim('/') + '/';

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using Azure.Storage.Blobs;
5+
46
namespace SixLabors.ImageSharp.Web.Caching.Azure;
57

68
/// <summary>
79
/// Configuration options for the <see cref="AzureBlobStorageCache"/>.
810
/// </summary>
911
public class AzureBlobStorageCacheOptions
1012
{
13+
/// <summary>
14+
/// Gets or sets a custom Azure BlobContainerClient provider
15+
/// </summary>
16+
public Func<AzureBlobStorageCacheOptions, IServiceProvider, BlobContainerClient>? BlobContainerClientProvider { get; set; } = null!;
17+
1118
/// <summary>
1219
/// Gets or sets the Azure Blob Storage connection string.
1320
/// <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string."/>

src/ImageSharp.Web.Providers.Azure/Providers/AzureBlobStorageImageProvider.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,23 @@ private readonly Dictionary<string, BlobContainerClient> containers
4141
/// </summary>
4242
/// <param name="storageOptions">The blob storage options.</param>
4343
/// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param>
44+
/// <param name="serviceProvider">The current service provider</param>
4445
public AzureBlobStorageImageProvider(
4546
IOptions<AzureBlobStorageImageProviderOptions> storageOptions,
46-
FormatUtilities formatUtilities)
47+
FormatUtilities formatUtilities,
48+
IServiceProvider serviceProvider)
4749
{
4850
Guard.NotNull(storageOptions, nameof(storageOptions));
4951

5052
this.formatUtilities = formatUtilities;
5153

5254
foreach (AzureBlobContainerClientOptions container in storageOptions.Value.BlobContainers)
5355
{
54-
this.containers.Add(
55-
container.ContainerName!,
56-
new BlobContainerClient(container.ConnectionString, container.ContainerName));
56+
BlobContainerClient containerClient = container.BlobContainerClientProvider == null
57+
? new BlobContainerClient(container.ConnectionString, container.ContainerName)
58+
: container.BlobContainerClientProvider(container, serviceProvider);
59+
60+
this.containers.Add(container.ContainerName!, containerClient);
5761
}
5862
}
5963

src/ImageSharp.Web.Providers.Azure/Providers/AzureBlobStorageImageProviderOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using Azure.Storage.Blobs;
5+
46
namespace SixLabors.ImageSharp.Web.Providers.Azure;
57

68
/// <summary>
@@ -19,6 +21,11 @@ public class AzureBlobStorageImageProviderOptions
1921
/// </summary>
2022
public class AzureBlobContainerClientOptions
2123
{
24+
/// <summary>
25+
/// Gets or sets a custom Azure BlobServiceClient provider
26+
/// </summary>
27+
public Func<AzureBlobContainerClientOptions, IServiceProvider, BlobContainerClient>? BlobContainerClientProvider { get; set; } = null!;
28+
2229
/// <summary>
2330
/// Gets or sets the Azure Blob Storage connection string.
2431
/// <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string."/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static AzureBlobStorageImageProvider Create(IServiceProvider services)
2020
FormatUtilities utilities = services.GetRequiredService<FormatUtilities>();
2121
InitializeAzureStorage(services, options.Value);
2222

23-
return new AzureBlobStorageImageProvider(options, utilities);
23+
return new AzureBlobStorageImageProvider(options, utilities, services);
2424
}
2525

2626
private static void InitializeAzureStorage(IServiceProvider services, AzureBlobStorageImageProviderOptions options)

0 commit comments

Comments
 (0)