-
Notifications
You must be signed in to change notification settings - Fork 0
Storage
Contains IStorageHub service that provides an abstraction that can be used to store files using one or multiple storage containers.
- DevGuild.AspNetCore.Services.Storage
- DevGuild.AspNetCore.Services.Storage.FileSystem
- DevGuild.AspNetCore.Services.Storage.AmazonS3
- FileSystem (via DevGuild.AspNetCore.Services.Storage.FileSystem package) - stores files in the local file system of web server.
- AmazonS3 (via DevGuild.AspNetCore.Services.Storage.AmazonS3) - stores files in Amazon Simple Storage Service.
IStorageHub service provides access to all storage containers configured for the application via GetContainer method:
public void Example1(IStorageHub storageHub)
{
var container = storageHub.GetContainer("Files");
}Storage container provides following methods that can be used for file storage:
- StoreFileAsync - stores the file in the container.
- GetFileUrl / GetFileUrlAsync - gets public URL of the file that can be served to clients.
- GetFileContentAsync - gets the file content.
- DeleteFileAsync - deletes the file from the container.
Configuration of storage service is done in appsettings.json configuration file. You can define multiple storage containers by adding multiple properties to Storage section:
"Storage": {
"Container1": {
},
"Container2": {
}
}Each storage container should define Type of the provider and provider specific options:
"Storage": {
"Files": {
"Type": "FileSystem",
"BaseDirectory": "wwwroot/files",
"BaseUrl": "/files"
}
}To add storage service, add following code to ConfigureServices method of Startup class:
services.AddStorage(this.Configuration.GetSection("Storage")) // Adding storage service with "Storage" as configuration root.
.AddFileSystemProvider() // Adding FileSystem storage provider.
.RegisterContainer("Files"); // Registering "Files" storage container.This will add FileSystem storage provider and register container "Files" from "Storage" application configuration section.