GovUK.Dfe.CoreLibs.FileStorage 0.1.5

Prefix Reserved
dotnet add package GovUK.Dfe.CoreLibs.FileStorage --version 0.1.5
                    
NuGet\Install-Package GovUK.Dfe.CoreLibs.FileStorage -Version 0.1.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GovUK.Dfe.CoreLibs.FileStorage" Version="0.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GovUK.Dfe.CoreLibs.FileStorage" Version="0.1.5" />
                    
Directory.Packages.props
<PackageReference Include="GovUK.Dfe.CoreLibs.FileStorage" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GovUK.Dfe.CoreLibs.FileStorage --version 0.1.5
                    
#r "nuget: GovUK.Dfe.CoreLibs.FileStorage, 0.1.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GovUK.Dfe.CoreLibs.FileStorage@0.1.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GovUK.Dfe.CoreLibs.FileStorage&version=0.1.5
                    
Install as a Cake Addin
#tool nuget:?package=GovUK.Dfe.CoreLibs.FileStorage&version=0.1.5
                    
Install as a Cake Tool

GovUK.Dfe.CoreLibs.FileStorage

This library provides a simple abstraction for file storage with support for three providers:

  • Local: File system storage
  • Azure: Azure File Service storage
  • Hybrid: Combines local storage for file operations with Azure for SAS token generation

Installation

dotnet add package GovUK.Dfe.CoreLibs.FileStorage

Configuration

Add a FileStorage section to your appsettings.json based on your chosen provider:

Local Storage

"FileStorage": {
  "Provider": "Local",
  "Local": {
    "BaseDirectory": "C:\\FileStorage",
    "CreateDirectoryIfNotExists": true,
    "AllowOverwrite": true,
    "MaxFileSizeBytes": 104857600,
    "AllowedExtensions": ["pdf", "docx", "xlsx", "jpg", "png"],
    "AllowedFileNamePattern": "^[a-zA-Z0-9_-]+$"
  }
}

Azure Storage

"FileStorage": {
  "Provider": "Azure",
  "Azure": {
    "ConnectionString": "<storage-connection-string>",
    "ShareName": "<share-name>"
  }
}

Hybrid Storage

Use this mode when you want local storage for file operations but need Azure-specific features like SAS token generation:

"FileStorage": {
  "Provider": "Hybrid",
  "Local": {
    "BaseDirectory": "C:\\FileStorage",
    "CreateDirectoryIfNotExists": true
  },
  "Azure": {
    "ConnectionString": "<storage-connection-string>",
    "ShareName": "<share-name>"
  }
}

Usage

Basic Setup

Register the service in your application:

builder.Services.AddFileStorage(builder.Configuration);

File Operations

Inject IFileStorageService for standard file operations:

public class FileController
{
    private readonly IFileStorageService _fileStorage;

    public FileController(IFileStorageService fileStorage)
    {
        _fileStorage = fileStorage;
    }

    public async Task UploadFile(Stream fileStream, string fileName)
    {
        await _fileStorage.UploadAsync($"documents/{fileName}", fileStream);
    }

    public async Task<Stream> DownloadFile(string fileName)
    {
        return await _fileStorage.DownloadAsync($"documents/{fileName}");
    }

    public async Task DeleteFile(string fileName)
    {
        await _fileStorage.DeleteAsync($"documents/{fileName}");
    }

    public async Task<bool> CheckFileExists(string fileName)
    {
        return await _fileStorage.ExistsAsync($"documents/{fileName}");
    }
}

Azure-Specific Operations (SAS Token Generation)

When using Azure or Hybrid providers, you can inject IAzureSpecificOperations to access Azure-specific features like SAS token generation:

public class SecureFileController
{
    private readonly IFileStorageService _fileStorage;
    private readonly IAzureSpecificOperations _azureOperations;

    public SecureFileController(
        IFileStorageService fileStorage, 
        IAzureSpecificOperations azureOperations)
    {
        _fileStorage = fileStorage;
        _azureOperations = azureOperations;
    }

    // Upload using local storage (in Hybrid mode)
    public async Task UploadFile(Stream fileStream, string fileName)
    {
        await _fileStorage.UploadAsync($"documents/{fileName}", fileStream);
    }

    // Generate a read-only SAS token valid for 1 hour
    public async Task<string> GetSecureDownloadLink(string fileName)
    {
        var sasUri = await _azureOperations.GenerateSasTokenAsync(
            $"documents/{fileName}", 
            TimeSpan.FromHours(1), 
            "r" // read-only permission
        );
        return sasUri;
    }

    // Generate a SAS token with custom permissions and expiration
    public async Task<string> GetSecureUploadLink(string fileName)
    {
        var expiresOn = DateTimeOffset.UtcNow.AddHours(2);
        var sasUri = await _azureOperations.GenerateSasTokenAsync(
            $"documents/{fileName}", 
            expiresOn, 
            "rw" // read-write permissions
        );
        return sasUri;
    }
}

SAS Token Permissions

The permissions parameter supports the following values:

  • "r" - Read
  • "w" - Write
  • "d" - Delete
  • "c" - Create
  • Combinations like "rw", "rd", "rwd", etc.

Provider Comparison

Feature Local Azure Hybrid
Upload/Download Files ✅ ✅ ✅ (uses Local)
Delete Files ✅ ✅ ✅ (uses Local)
Check File Exists ✅ ✅ ✅ (uses Local)
Generate SAS Tokens ❌ ✅ ✅ (uses Azure)
Performance Fast Network dependent Fast for files, Network for SAS
Cost Free (local disk) Azure storage costs Azure storage costs

Use Cases

  • Local: Development, testing, small-scale deployments
  • Azure: Production cloud environments, distributed systems
  • Hybrid: When you need local performance but require secure external access via SAS tokens
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.5 555 1/29/2026
0.1.5-prerelease-36 678 1/27/2026
0.1.4 436 10/28/2025
0.1.4-prerelease-8 156 10/24/2025
0.1.4-prerelease-7 175 10/24/2025
0.1.4-prerelease-10 225 10/28/2025
0.1.3 322 9/8/2025
0.1.0 215 9/8/2025
0.1.0-prerelease-146 224 9/8/2025
0.1.0-prerelease-143 213 9/8/2025