TheĀ Azure Blob Storage handles uploaded media by storing the uploaded files in an Azure Storage Service. If you're hosting your web application in Azure this has several benefits.
As the media files are stored in a central location this works well when the different components of the application (such as client and manager) are deployed to different locations.
Azure Blob Storage can be installed by adding the NuGet package:
PM> install-package Piranha.Azure.BlobStorage
You register Azure Blob Storage with the default configuration in ConfigureServices()
with the following code:
services.AddPiranhaBlobStorage(credentials);
The storage credentials can be created manually by providing the storage service name and key.
var credentials = new StorageCredentials(storageName, storageKey);
You can easily configure your credentials from your appsettings.json
by adding a section like this:
{
"Storage": {
"Name": "MyStorageName",
"Key": "MyStorageKey"
}
}
Given that you've setup your app configuration you can then access it like this from your ConfigureServices
.
public class Startup
{
public IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddPiranhaBlobStorage(new StorageCredentials(
Configuration["Storage:Name"],
Configuration["Storage:Key"]
));
...
}
}
You can also register the Blog Storage using its Connection String
which can be found in the Azure portal by clicking Access Keys
.
services.AddPiranhaBlobStorage(myConnectionString);
When registering the service you have access to some configuration options to customize the storage to your needs.
services.AddPiranhaBlobStorage(credentials, containerName: "dev");
The container name in the Storage Service where uploaded media assets will be stored. The container name defaults to uploads
and will create a new container with the same name if it doesn't exist.
To use an already existing container, make sure the access level should is set to Blob (anonymous access for blobs only) to ensure the files are publically available.
The folder structure in the media manager is maintained in the database and not the file storage, so changing this setting after uploading files or seeding example data will break the references to files and they will need to be re-uploaded.
You can change how unique names are generated for uploaded media files. By default a Guid
is added to the beginning of the filename in order to ensure that it is unique, but this can be changed so that files are stored in a folder with a Guid
based name.
services.AddPiranhaBlobStorage(naming: naming: Piranha.Azure.BlobStorageNaming.UniqueFolderNames);
services.AddPiranhaBlobStorage(credentials, scope: ServiceLifetime.Scoped);
By default the service is registered as a Singleton
, but in some cases you might want to handle it differently in your application.