Azure Blob Storage IFileProvider for ASP.NET Core

· 545 words · 3 minutes to read

As part of my recent talks on ASP.NET core, I have been showing how to build a custom IFileProvider for ASP.NET Core. The example that I was using was Azure Blob Storage - and exposing files from there as if they were local files that are part of your application.

I have pushed that code to Github and decided to package it as Nuget package, which, hopefully, someone will find useful.

IFileProviders 🔗

With IFileProviders in ASP.NET Core, you can expose an arbitrary set of files to the framework, and allow them to be part of the static file functionality (your application server exposing a set of static files), or other features such as Razor view look ups.

ASP.NET core ships with 3 built-in file providers:

  • PhysicalFileProvider - used to access static files deployed together with your application
  • ManifestEmbeddedFileProvider - used to access files embedded in assemblies
  • CompositeFileProvider - used to merge multiple file providers together

You can read more about file providers in the official ASP.NET Core documentation.

Azure Blob File Provider 🔗

With a blob file provider, you can allow remotely located (in an Azure Blob Storage account) files to be treated as if they were an integral part of your application. This is very convenient, as it can help you decouple the deployable part of the application from the static resources which you may want to manage using a different lifecycle.

In order to take advantage of the blob file provider, you just need to reference the Nuget package in your project and do a very simple configuration.

<PackageReference Include="Strathweb.AspNetCore.AzureBlobFileProvider" Version="0.2.0-preview" />

The package is currently prerelease, due to a dependency on Microsoft.Azure.Storage.Blob which is also prerelease-only. This is the split WindowsAzure.Storage library allowing us to use Blob connectivity without dragging in all the code to deal with queues, Table Storage and so on.

You can set up your Blob Storage to be accessed using a connection string…:

public void ConfigureServices(IServiceCollection services)
{
    var blobOptions = new AzureBlobOptions
    {
       ConnectionString = "{my connection string}",
       DocumentContainer = "{blob container name}"
    }
    
    var azureBlobFileProvider = new AzureBlobFileProvider(blobOptions);
    services.AddSingleton(azureBlobFileProvider);
}

…or using an SAS token:

public void ConfigureServices(IServiceCollection services)
{
    var blobOptions = new AzureBlobOptions
    {
       BaseUri = "{base URL of the storage account}",
       Token = "{SAS token}",
       DocumentContainer = "{blob container name}"
    }
    
    var azureBlobFileProvider = new AzureBlobFileProvider(blobOptions);
    services.AddSingleton(azureBlobFileProvider);
}

Of course in this case the values are shown to be hardcoded, but in normal scenarios you’d probably want the AzureBlobOptions to be bound directly from configuration, i.e. from appsettings.json.

You can then use the Blob File Provider to - for example enable access to all the files in the Blob Storage under the /files path of your application.

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"
    });
}

Now, if you navigate to /files, you will be able to view the Blob Storage files.

Finally, since the Blob Storage file provider is now configured in the DI container, you can inject them into controllers or other code parts where you need access to Blob Storage, without having to build the blob access code yourself.

You can find the source code here on Github.

About


Hi! I'm Filip W., a software architect from Zürich 🇨🇭. I like Toronto Maple Leafs 🇨🇦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴󠁧󠁢󠁳󠁣󠁴󠁿.

You can find me on Github, on Mastodon and on Bluesky.

My Introduction to Quantum Computing with Q# and QDK book
Microsoft MVP