Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added BlobContainerClientProvider option to AzureBlobStorageCache and AzureBlobStorageImageProvider #361

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static AzureBlobStorageImageProvider Create(IServiceProvider services)
FormatUtilities utilities = services.GetRequiredService<FormatUtilities>();
InitializeAzureStorage(services, options.Value);

return new AzureBlobStorageImageProvider(options, utilities);
return new AzureBlobStorageImageProvider(options, utilities, services);
}

private static void InitializeAzureStorage(IServiceProvider services, AzureBlobStorageImageProviderOptions options)
Expand Down
Loading