Using Azure Blob Storage for Media and ImageSharp Cache
Setup your site to use Azure Blob storage for media and ImageSharp cache
There are some scenarios where you may want or need to consider leveraging Azure Blob Storage. An example would be for media in Umbraco sites with substantial media libraries.
Having your site's media in Azure Blob Storage can help your deployments complete more quickly. This has the potential to positively affect site performance, as the ImageSharp cache is moved to Azure Blob Storage.
The setup consists of adding a package to your site, setting the correct configuration, and adding the services and middleware. Before you begin you’ll need to create an Azure Storage Account and a container for your media and ImageSharp cache. In this example, we assume your container name is "mysitestorage" and has already been created.
See the Microsoft documentation for a quickstart guide on how to create a blob storage container.
Installing the package
Before you begin, you need to install the Umbraco.StorageProviders.AzureBlob
NuGet package. There are two approaches to installing the package:
Use your favorite IDE and open up the NuGet Package Manager to search and install the package
Use the command line to install the package
Installing through command line
Navigate to your project folder, which is the folder that contains your .csproj
file. Now use the following dotnet add package
command to install the package:
The correct package will have be installed in your project.
Configuring Blob storage
The next step is to configure your blob storage. There are multiple approaches for this, but in this document, we're going to do it through appsettings.json
. For more configuration options, see the readme on the GitHub repository.
Open up your appsettings.json
file and add the connection string and container name under Umbraco:Storage:AzureBlob:Media
. Your Umbraco section of appsettings will look something like this:
In this example, the container name is mysitestorage
.
You can get your connection string from your Azure Portal under "Access Keys".
Setting the services and middleware
You're almost there. The last step is to set up the required services and middleware. This may sound daunting, but thankfully there are extension methods that do all this for you. All you need to do is invoke them in the ConfigureServices
and Configure
methods in the startup.cs
file.
Invoke the .AddAzureBlobMediaFileSystem()
extention method in the ConfigureServices
method:
If you are using Umbraco 9, follow this step before moving on:
Next invoke UseAzureBlobMediaFileSystem();
in the .WithMiddleware
call, like so:
Now when you launch your site again, the blob storage will be used to store media items as well as the ImageSharp cache. Do note though that the /media
and /cache
folders do not get created until a piece of media is uploaded.
Existing Media files
Any media files you already have on your site will not automatically be added to the blob storage container. You will need to copy the contents on the /wwwroot/media
folder and upload them to a new folder called /media
in your blob storage container. Once you've done that, you can safely delete the wwwroot/media
folder locally, as it is no longer needed.
Any new media files you upload to the site will automatically be added to the Blob Storage.
Last updated