'How to write custom implementation of IFileprovider for Azure Blob Container

For serving images,js and css in asp.net core MVC application ,I use app.UseStaticFiles method and serve from a physical location.

 app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider("c://staticfiles"),
                RequestPath = new PathString("/cdn"),
                ServeUnknownFileTypes=true
            });

but now I want to use Azure Blob storage containers to serve those file . I add Azure.Storage.Blobs nuget package in my application but did not found any FileProvider class for Azure Blob Storage .

How do I do this?

Thanks.



Solution 1:[1]

One of the workaround is to use Strathweb.AspNetCore.AzureBlobFileProvider which provides blob file provider. You can set up your Blob Storage using connection string or SAS token and then use the Blob File Provider to enable access to all the files in the Blob Storage.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var blobFileProvider = app.ApplicationServices.GetRequiredService<AzureBlobFileProvider>();
    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = blobFileProvider,
        RequestPath = "/files"
    });

    app.UseDirectoryBrowser(new DirectoryBrowserOptions
    {
        FileProvider = blobFileProvider,
        RequestPath = "/files"
    });
}

REFERENCES: Azure Blob Storage IFileProvider for ASP.NET Core

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SwethaKandikonda-MT