Skip to content

Storage

Arsenty Politov edited this page Mar 15, 2019 · 1 revision

Storage services

Contains IStorageHub service that provides an abstraction that can be used to store files using one or multiple storage containers.

Related packages

  • DevGuild.AspNetCore.Services.Storage
  • DevGuild.AspNetCore.Services.Storage.FileSystem
  • DevGuild.AspNetCore.Services.Storage.AmazonS3

Supported providers

  • 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.

Getting storage container

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

Using storage container

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

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

Adding services

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.

Clone this wiki locally