Skip to content

Commit 18f67f7

Browse files
Don't automatically craete the cache container.
1 parent 9c0bcde commit 18f67f7

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.IO;
55
using System.Threading.Tasks;
6+
using Azure;
67
using Azure.Storage.Blobs;
78
using Azure.Storage.Blobs.Models;
89
using Microsoft.Extensions.Options;
@@ -28,7 +29,6 @@ public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions
2829
AzureBlobStorageCacheOptions options = cacheOptions.Value;
2930

3031
this.container = new BlobContainerClient(options.ConnectionString, options.ContainerName);
31-
this.container.CreateIfNotExistsAsync(PublicAccessType.None);
3232
}
3333

3434
/// <inheritdoc/>
@@ -56,5 +56,30 @@ public Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata)
5656

5757
return blob.UploadAsync(stream, httpHeaders: headers, metadata: metadata.ToDictionary());
5858
}
59+
60+
/// <summary>
61+
/// Creates a new container under the specified account if a container
62+
/// with the same name does not already exist.
63+
/// </summary>
64+
/// <param name="options">The Azure Blob Storage cache options.</param>
65+
/// <param name="accessType">
66+
/// Optionally specifies whether data in the container may be accessed publicly and
67+
/// the level of access. <see cref="PublicAccessType.BlobContainer"/>
68+
/// specifies full public read access for container and blob data. Clients can enumerate
69+
/// blobs within the container via anonymous request, but cannot enumerate containers
70+
/// within the storage account. <see cref="PublicAccessType.Blob"/>
71+
/// specifies public read access for blobs. Blob data within this container can be
72+
/// read via anonymous request, but container data is not available. Clients cannot
73+
/// enumerate blobs within the container via anonymous request. <see cref="PublicAccessType.None"/>
74+
/// specifies that the container data is private to the account owner.
75+
/// </param>
76+
/// <returns>
77+
/// If the container does not already exist, a <see cref="Response{T}"/> describing the newly
78+
/// created container. If the container already exists, <see langword="null"/>.
79+
/// </returns>
80+
public static Response<BlobContainerInfo> CreateIfNotExists(
81+
AzureBlobStorageCacheOptions options,
82+
PublicAccessType accessType)
83+
=> new BlobContainerClient(options.ConnectionString, options.ContainerName).CreateIfNotExists(accessType);
5984
}
6085
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class AzureBlobStorageCacheOptions
1010
{
1111
/// <summary>
1212
/// Gets or sets the Azure Blob Storage connection string.
13+
/// <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string."/>
1314
/// </summary>
1415
public string ConnectionString { get; set; }
1516

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class AzureBlobContainerClientOptions
2323
{
2424
/// <summary>
2525
/// Gets or sets the Azure Blob Storage connection string.
26+
/// <see href="https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string."/>
2627
/// </summary>
2728
public string ConnectionString { get; set; }
2829

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

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

4+
using Azure.Storage.Blobs.Models;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.Extensions.DependencyInjection;
67
using SixLabors.ImageSharp.Web.Caching.Azure;
@@ -64,6 +65,8 @@ protected override void ConfigureServices(IServiceCollection services)
6465
{
6566
options.ConnectionString = TestConstants.AzureConnectionString;
6667
options.ContainerName = TestConstants.AzureCacheContainerName;
68+
69+
AzureBlobStorageCache.CreateIfNotExists(options, PublicAccessType.None);
6770
})
6871
.SetCache<AzureBlobStorageCache>();
6972
}

0 commit comments

Comments
 (0)