Skip to content

Commit

Permalink
Feature: Added BlobContainerClientProvider option to AzureBlobStora…
Browse files Browse the repository at this point in the history
…geCache and AzureBlobStorageImageProvider.cs
  • Loading branch information
marklagendijk committed Mar 14, 2024
1 parent 0a03dbb commit 659fa6d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ public class AzureBlobStorageCache : IImageCache
/// Initializes a new instance of the <see cref="AzureBlobStorageCache"/> class.
/// </summary>
/// <param name="cacheOptions">The cache options.</param>
public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions)
/// <param name="serviceProvider">The current service provider.</param>
public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions, IServiceProvider serviceProvider)
{
Guard.NotNull(cacheOptions, nameof(cacheOptions));
AzureBlobStorageCacheOptions options = cacheOptions.Value;

this.container = new BlobContainerClient(options.ConnectionString, options.ContainerName);
this.container = options.BlobContainerClientProvider == null
? new BlobContainerClient(options.ConnectionString, options.ContainerName)
: options.BlobContainerClientProvider(options, serviceProvider);
this.cacheFolder = string.IsNullOrEmpty(options.CacheFolder)
? string.Empty
: options.CacheFolder.Trim().Trim('/') + '/';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using Azure.Storage.Blobs;

namespace SixLabors.ImageSharp.Web.Caching.Azure;

/// <summary>
/// Configuration options for the <see cref="AzureBlobStorageCache"/>.
/// </summary>
public class AzureBlobStorageCacheOptions
{
/// <summary>
/// Gets or sets a custom Azure BlobContainerClient provider
/// </summary>
public Func<AzureBlobStorageCacheOptions, IServiceProvider, BlobContainerClient>? BlobContainerClientProvider { get; set; } = null!;

/// <summary>
/// Gets or sets the Azure Blob Storage connection string.
/// <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string."/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,23 @@ private readonly Dictionary<string, BlobContainerClient> containers
/// </summary>
/// <param name="storageOptions">The blob storage options.</param>
/// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param>
/// <param name="serviceProvider">The current service provider</param>
public AzureBlobStorageImageProvider(
IOptions<AzureBlobStorageImageProviderOptions> storageOptions,
FormatUtilities formatUtilities)
FormatUtilities formatUtilities,
IServiceProvider serviceProvider)
{
Guard.NotNull(storageOptions, nameof(storageOptions));

this.formatUtilities = formatUtilities;

foreach (AzureBlobContainerClientOptions container in storageOptions.Value.BlobContainers)
{
this.containers.Add(
container.ContainerName!,
new BlobContainerClient(container.ConnectionString, container.ContainerName));
BlobContainerClient containerClient = container.BlobContainerClientProvider == null
? new BlobContainerClient(container.ConnectionString, container.ContainerName)
: container.BlobContainerClientProvider(container, serviceProvider);

this.containers.Add(container.ContainerName!, containerClient);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using Azure.Storage.Blobs;

namespace SixLabors.ImageSharp.Web.Providers.Azure;

/// <summary>
Expand All @@ -19,6 +21,11 @@ public class AzureBlobStorageImageProviderOptions
/// </summary>
public class AzureBlobContainerClientOptions
{
/// <summary>
/// Gets or sets a custom Azure BlobServiceClient provider
/// </summary>
public Func<AzureBlobContainerClientOptions, IServiceProvider, BlobContainerClient>? BlobContainerClientProvider { get; set; } = null!;

/// <summary>
/// Gets or sets the Azure Blob Storage connection string.
/// <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string."/>
Expand Down

0 comments on commit 659fa6d

Please sign in to comment.