TaskHub.Shared

Using Storage

The IStorageService abstracts file operations, allowing you to switch between local file system storage and cloud providers (like S3 or Azure Blobs) without changing your business logic.

Configuration (FileSystem)

Specify the root path where files should be stored.

"Storage": {
  "FileSystem": {
    "RootPath": "/app/storage"
  }
}

Usage Example

public class UploadProfilePhotoHandler(IStorageService storage, IProfileRepository repository) 
    : ICommandHandler<UploadPhotoCommand, Result<Guid>>
{
    public async Task<Result<Guid>> HandleAsync(UploadPhotoCommand cmd, CancellationToken ct)
    {
        // 1. Upload the file
        var fileName = $"{Guid.NewGuid()}{Path.GetExtension(cmd.FileName)}";
        var uploadResult = await storage.UploadAsync(cmd.FileStream, "profiles", fileName, ct);
        
        if (!uploadResult.IsSuccess) return uploadResult;

        // 2. Get the permanent URL or path
        var fileUrl = await storage.GetUrlAsync(uploadResult.Data.Path, ct);

        // ... update database with the path
        return ResultFactory.OnSuccess(uploadResult.Data.Id);
    }
}

Key Methods

Provider-Agnosticism

By coding against IStorageService, your application doesn’t care if the file is on a local disk or in a distributed blob store. This is ideal for containerized deployments.