dotnet-storage
1.0.6
dotnet add package dotnet-storage --version 1.0.6
NuGet\Install-Package dotnet-storage -Version 1.0.6
<PackageReference Include="dotnet-storage" Version="1.0.6" />
<PackageVersion Include="dotnet-storage" Version="1.0.6" />
<PackageReference Include="dotnet-storage" />
paket add dotnet-storage --version 1.0.6
#r "nuget: dotnet-storage, 1.0.6"
#:package dotnet-storage@1.0.6
#addin nuget:?package=dotnet-storage&version=1.0.6
#tool nuget:?package=dotnet-storage&version=1.0.6
dotnet-storage
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 | Versions 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. |
-
net8.0
- AWSSDK.S3 (>= 3.7.205.11)
- Microsoft.Extensions.Options (>= 7.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.