Inherited content from the old documentation, pending rewrite.
Introduction
If your application needs to use a cloud file storage service (Azure, AWS, etc.), it is recommended to use the service defined by the ICloudStorage interface of the Lagoon toolkit. This interface exposes the main file manipulation methods for Azure Blob Storage and AWS S3
Cloud Storage Provider Concepts
When working with cloud storage, the concept of a container (for Azure) or bucket (for AWS) replaces the notion of a "storage folder": you upload a file to a container and download it from a container. Typically, an application only needs a single container (or bucket) to store its files. Containers and buckets do not directly manage the concept of folders; however, the "/" character in the file name allows you to simulate a folder structure in the file management interface. For example, a file named "export/data.zip" in a container named app-data will simulate folder behavior in the management interface provided by AWS or Azure: |--app-data |----export |------test.zip
File Download Pattern with a Cloud Service
Typically, when you want to send a file, you send it directly from a Controller in your application. To make the most of the cloud storage service, instead of sending the file directly from your Controller, you should ask the cloud provider to generate a download link and return it to the client. The client can then download the file directly from the storage provider (and your server does not handle the file data transmission). Schematic view of file download:
Setup
Add the Lagoon.Storage.Azure or Lagoon.Storage.AWS package to your project and enable the service:
/// <summary>
/// Register the services for the application.
/// </summary>
/// <param name="services">The service collection.</param>
/// <remarks>The base implementation does nothing.</remarks>
protected override void OnConfigureServices(IServiceCollection services)
{
// For Azure with Lagoon.Storage.Azure
services.AddCloudStorage<AzureStorage>();
// For AWS with Lagoon.Storage.Aws
services.AddCloudStorage<AwsStorage>();
}
Configuration
Azure
Configure the appSettings.json file to use Azure Blob Storage:
"CloudStorage": {
"Azure": {
"StorageAccountName": null,
"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=**XX**;AccountKey=**YY**;EndpointSuffix=core.windows.net"
}
}
Depending on the Azure Blob Storage service configuration, you should define either the key:
- StorageAccountName: If Azure is configured to use AzureDefaultIdentity
- ConnectionString: If Azure is configured with an AccountName / AccountKey pair
AWS
Local
To simulate cloud behavior, you can configure your appSettings.Development.json:
"CloudStorage": {
"Local": {
"RootPath": "C:\\Temp\\DraftData\\"
}
}
Containers (or buckets) will be created at the root of the folder specified by the CloudStorage:Local:RootPath key
Usage
Inject the ICloudStorage service into your controller
[Authorize()]
[Route("api/[controller]")]
public class DownloadController : LgControllerBase
{
private readonly ICloudStorage _storage;
public DownloadController(ICloudStorage storage)
{
_storage = storage;
}
}
Example of generating a download link for a file*
[HttpGet("GetDownloadLink/{id:guid}")]
public ActionResult<string> GetDownloadLink(Guid id)
{
_ = id; // Should be used to retrieve doc info (bucket and path)
return Ok(_storage.GeneratePresignedUrl("my-bucket", "test/test.zip", 0.1));
}