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.
The Local File Storage can be installed by adding the NuGet package:
<PackageReference Include="Piranha.Azure.BlobStorage" Version="7.0.0" />
You register Local File 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"]
));
...
}
}
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.
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.