Search code examples
c#asp.net-coreazure-blob-storageimagesharp

ImageSharp.Web with AzureBlobStorageImageProvider giving 404's


Following the example code I'm attempting to hook up an ImageSharp.Web's AzureBlobStorageImageProvider but all image requests result in 404's. Any idea what I'm missing from my wiring-up?

Looking at the docs it seems the url convention should be https://localhost:44336/container-name/file-name.jpg?width=30

In my Startup.cs I have the following code:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddImageSharp()
        .Configure<AzureBlobStorageImageProviderOptions>(options =>
        {
            // The "BlobContainers" collection allows registration of multiple containers.
            options.BlobContainers.Add(new AzureBlobContainerClientOptions
            {
                ConnectionString = "REDACTED",
                ContainerName = "test-container"
            });
        })
        .AddProvider<AzureBlobStorageImageProvider>()
        .Configure<AzureBlobStorageCacheOptions>(options =>
        {
            options.ConnectionString = "REDACTED";
            options.ContainerName = "cache";

            // Optionally create the cache container on startup if not already created.
            AzureBlobStorageCache.CreateIfNotExists(options, PublicAccessType.None);
        })
        .SetCache<AzureBlobStorageCache>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    app.UseImageSharp();
}

Any ideas what I might be missing?


Solution

  • It seems you need to ClearProviders() first before adding the AzureBlobStorageImageProvider. So the code becomes:

    .ClearProviders()
    .AddProvider<AzureBlobStorageImageProvider>()