dotnet-storage is a .NET library that provides file storage functionality for ASP.NET Core applications.
You can install dotnet-storage via NuGet package manager. Use the following commands:
Open your terminal and navigate to your ASP.NET Core project directory. Run the following command:
dotnet add package dotnet-storage
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>();
You can configure dotnet-storage by adding settings to your appsettings.json
file. The following settings are available:
Here's a appsettings.json
configuration for dotnet-storage:
"StorageSettings": {
"ActiveDriver": "localfilesystem",
"LocalFileSystem": {
"RootPath": ""
},
}
"StorageSettings": {
"ActiveDriver": "amazons3",
"AmazonS3": {
"AccessKey": "",
"SecretKey": "",
"BucketName": "",
"Region": "",
"ServiceURL": ""
}
}
To demonstrate how to use dotnet-storage, let's create a simple ASP.NET Core controller that utilizes the package's functionality.
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.");
}
}
}
curl -X 'POST' \
'https://localhost:7151/api/Sample' \
-H 'accept: */*' \
-H 'Content-Type: multipart/form-data' \
-F '[email protected];type=image/jpeg'
{
"filePath": "uploads/04bdc90d-ad2a-4079-9c11-424be8aa02e3.jpeg"
}
The dotnet-storage is open-sourced software licensed under the MIT license.