dotnet-storage 1.0.6

dotnet add package dotnet-storage --version 1.0.6
                    
NuGet\Install-Package dotnet-storage -Version 1.0.6
                    
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="dotnet-storage" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="dotnet-storage" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="dotnet-storage" />
                    
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 dotnet-storage --version 1.0.6
                    
#r "nuget: dotnet-storage, 1.0.6"
                    
#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 dotnet-storage@1.0.6
                    
#: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=dotnet-storage&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=dotnet-storage&version=1.0.6
                    
Install as a Cake Tool

dotnet-storage

NuGet Version License

dotnet-storage is a .NET library that provides file storage functionality for ASP.NET Core applications.

Installation

You can install dotnet-storage via NuGet package manager. Use the following commands:

.NET CLI

Open your terminal and navigate to your ASP.NET Core project directory. Run the following command:

dotnet add package dotnet-storage

Configuration program.cs

It is Very important to configure the package in startup/program.cs. This will allow the package to work properly

// Configuration from appsettings.json
builder.Services.Configure<StorageConfiguration>(builder.Configuration.GetSection("StorageSettings"));

// Register your storage drivers
builder.Services.AddTransient<LocalFileSystemStorageDriver>();
builder.Services.AddTransient<AmazonS3StorageDriver>();

// Register the StorageService
builder.Services.AddTransient<IStorageService, StorageService>();

Configuration via appsettings.json

You can configure dotnet-storage by adding settings to your appsettings.json file. The following settings are available:

Sample appsettings.json Configuration

Here's a appsettings.json configuration for dotnet-storage:

Local localfilesystem Configuration

  "StorageSettings": {
    "ActiveDriver": "localfilesystem", 
    "LocalFileSystem": {
      "RootPath": ""
    },
  }

Amazons3/minio Configuration

  "StorageSettings": {
    "ActiveDriver": "amazons3", 
    "AmazonS3": {
      "AccessKey": "",
      "SecretKey": "",
      "BucketName": "",
      "Region": "",
      "ServiceURL": ""
    }
  }

Example Usage in an ASP.NET Core Application

To demonstrate how to use dotnet-storage, let's create a simple ASP.NET Core controller that utilizes the package's functionality.

Step 1: Create an ASP.NET Core Controller

In your ASP.NET Core application, create a new controller. For example, let's create a SampleController:

using Microsoft.AspNetCore.Mvc;
using Storage.contracts;
namespace example.Controllers;

[ApiController]
[Route("api/[controller]")]
public class SampleController : ControllerBase
{
    private readonly IStorageService storageService;
    private readonly ILogger<SampleController> logger;

    public SampleController(IStorageService storageService, ILogger<SampleController> logger)
    {
        this.storageService = storageService ?? throw new ArgumentNullException(nameof(storageService));
        this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    [HttpPost(Name = "upload")]
    public async Task<IActionResult> UploadFile(IFormFile file)
    {
        try
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("Invalid file.");
            }

            // Generate a unique file name or use a predefined naming convention
            string fileExtension = Path.GetExtension(file.FileName);
            var uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
            var filePath = "uploads/" + uniqueFileName; // You can customize the storage path

            using (var stream = file.OpenReadStream())
            {
                await storageService.StoreAsync(filePath, stream);
            }

            return Ok(new { FilePath = filePath });
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "Error while uploading the file.");
            return StatusCode(500, "Internal server error.");
        }
    }
}

Here's an api call using curl

curl -X 'POST' \
  'https://localhost:7151/api/Sample' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'file=@8fb41467-6983-478a-9086-260cb0a12782.jpeg;type=image/jpeg'

response

{
  "filePath": "uploads/04bdc90d-ad2a-4079-9c11-424be8aa02e3.jpeg"
}

License

The dotnet-storage is open-sourced software licensed under the MIT license.

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
1.0.6 272 4/8/2025
1.0.5 208 3/27/2025
1.0.4 265 6/5/2024
1.0.3 219 4/17/2024
1.0.2 201 4/17/2024
1.0.1 649 10/18/2023
1.0.0 478 10/17/2023