Threem.File.UploaderKit
1.1.0
dotnet add package Threem.File.UploaderKit --version 1.1.0
NuGet\Install-Package Threem.File.UploaderKit -Version 1.1.0
<PackageReference Include="Threem.File.UploaderKit" Version="1.1.0" />
<PackageVersion Include="Threem.File.UploaderKit" Version="1.1.0" />
<PackageReference Include="Threem.File.UploaderKit" />
paket add Threem.File.UploaderKit --version 1.1.0
#r "nuget: Threem.File.UploaderKit, 1.1.0"
#:package Threem.File.UploaderKit@1.1.0
#addin nuget:?package=Threem.File.UploaderKit&version=1.1.0
#tool nuget:?package=Threem.File.UploaderKit&version=1.1.0
Threem.File.UploaderKit
A robust file upload solution for .NET applications that handles file storage in Azure Blob Storage with metadata management in either MongoDB or SQL Server.
Features
Dual Storage Support: Store files in Azure Blob Storage while maintaining metadata in either:
MongoDB (for document-based flexibility), or
SQL Server (for relational data needs)
Automatic Configuration: Simple setup with just one line of code
Comprehensive File Handling:
SHA256 hash generation
MIME type detection
File size tracking
Custom metadata support
Logging Integration: Built-in NLog support with configurable log directory
Demo Project Available: Check out the Threem.File.UploaderKit Demo API for a working example
Installation
dotnet add package Threem.File.UploaderKit
β‘ Quick Start
1. Configure appsettings.json
{
"FileUploader": {
"StoreType": "SqlServer", // or "MongoDB"
"Mongo": {
"ConnectionString":"your-mongodb-connection-string",
"Database": "FileMetadataDB",
"Collection": "FileMetadata"
},
"Sql": {
"ConnectionString": "Your_SQL_Connection_String"
},
"AzureBlobConnectionString": "Your_Azure_Blob_Connection_String",
"AzureBlobContainer": "your-container-name",
"LogDirectory": "C:\\Logs\\FileUploader"
}
}
β Tip: Choose the StoreType based on where you want to store your file metadata (SQL Server or MongoDB).
2. Set up your Program.cs
var settings = builder.Configuration.GetSection("FileUploader").Get<FileUploaderSettings>();
// Add services to the container
builder.Services.AddControllers();
// Configure File Uploader with automatic setup
builder.Services.AddFileUploader(settings);
π‘ The AddFileUploader() method automatically reads your config and sets everything up.
3. Create your controller
Hereβs how to implement an endpoint that accepts files and uploads them:
[ApiController]
[Route("[controller]")]
[Consumes("multipart/form-data")]
public class FileUploadController : ControllerBase
{
private readonly IFileUploader _fileUploader;
public FileUploadController(IFileUploader fileUploader)
{
_fileUploader = fileUploader;
}
[HttpPost("upload")]
public async Task<IActionResult> Upload(
[FromForm] IFormFile file,
[FromQuery] string clientId)
{
await _fileUploader.UploadAsync(file, clientId, new Dictionary<string, object>
{
{ "status", "uploaded" },
{ "uploadedBy", "API" }
});
return Ok(new { Message = "Upload successful" });
}
}
π You can enrich the uploaded file with metadata using the dictionary passed to UploadAsync.
βοΈ Configuration Options
π FileUploaderSettings
| Property | Type | Required | Description |
| --------------------------- | ------ | -------- | ----------------------------------------------- |
| StoreType
| enum | β
Yes | MongoDB or SqlServer |
| AzureBlobConnectionString
| string | β
Yes | Azure Blob Storage connection string |
| AzureBlobContainer
| string | β
Yes | Name of the target container |
| LogDirectory
| string | β
Yes | Directory path for logs |
π MongoDB Config (when StoreType = MongoDB)
| Property | Type | Required | Description |
| ------------------ | ------ | -------- | ------------------ |
| ConnectionString
| string | β
Yes | MongoDB connection |
| Database
| string | β
Yes | Database name |
| Collection
| string | β
Yes | Collection name |
π SQL Server Config (when StoreType = SqlServer)
| Property | Type | Required | Description |
| ------------------ | ------ | -------- | --------------------- |
| ConnectionString
| string | β
Yes | SQL Server connection |
π§± Database Schema Requirements
π MongoDB
Automatically creates the required collection. No schema setup needed.
π§± SQL Server
Create this table in your database:
CREATE TABLE FileMetadata (
Id NVARCHAR(255) PRIMARY KEY,
FileName NVARCHAR(255) NOT NULL,
ClientId NVARCHAR(255) NOT NULL,
BlobUri NVARCHAR(MAX) NOT NULL,
UploadDate DATETIME2 NOT NULL,
FileSize BIGINT NOT NULL,
MimeType NVARCHAR(100) NOT NULL,
SHA256 NVARCHAR(64) NOT NULL,
AdditionalData NVARCHAR(MAX) NULL
);
π Logging Configuration (NLog)
Add nlog.config
to your project.
Register NLog in Program.cs:
builder.Logging.AddNLog("nlog.config");
For Console Application Use Code eg(appsettings Remains the same)
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Threem.File.UploaderKit;
using Threem.File.UploaderKit.Models;
// Create Host with DI and Configuration
using IHost host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((ctx, config) =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureServices((ctx, services) =>
{
var settings = ctx.Configuration.GetSection("FileUploader").Get<FileUploaderSettings>();
services.AddFileUploader(settings);
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Information);
logging.AddNLog("nlog.config"); // β
Use NLog in console app
})
.Build();
var logger = host.Services.GetRequiredService<ILogger<Program>>();
var uploader = host.Services.GetRequiredService<IFileUploader>();
string clientId = "console-client-001";
// 1οΈβ£ Upload via Stream
var filePath = @"C:\Temp\example.docx";
await using (var stream = File.OpenRead(filePath))
{
await uploader.UploadAsync(stream, Path.GetFileName(filePath), clientId, new Dictionary<string, object>
{
{ "status", "uploaded" },
{ "uploadedBy", "ConsoleApp-Stream" }
});
}
// 2οΈβ£ Upload via byte[]
var fileBytes = await File.ReadAllBytesAsync(filePath);
await uploader.UploadAsync(fileBytes, Path.GetFileName(filePath), clientId, new Dictionary<string, object>
{
{ "status", "uploaded" },
{ "uploadedBy", "ConsoleApp-ByteArray" }
});
// 3οΈβ£ Upload via Base64
var base64String = Convert.ToBase64String(fileBytes);
await uploader.UploadAsync(base64String, Path.GetFileName(filePath), clientId, new Dictionary<string, object>
{
{ "status", "uploaded" },
{ "uploadedBy", "ConsoleApp-Base64" }
});
Console.WriteLine("β
All uploads completed successfully!");
π€ Contributing
Contributions are welcome! Please:
Code follows existing style
New features include tests
Documentation is updated
π Issues & Support
If you face any issues or need support, please reach out via email:
π§ info@threemsolutions.com
π License
This package is 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
- Azure.Storage.Blobs (>= 12.24.0)
- Microsoft.AspNetCore.StaticFiles (>= 2.3.0)
- Microsoft.Data.SqlClient (>= 6.0.1)
- Microsoft.Extensions.Configuration (>= 9.0.7)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.7)
- Microsoft.Extensions.Logging (>= 9.0.4)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.7)
- MongoDB.Driver (>= 3.3.0)
- NLog (>= 5.4.0)
- NLog.Extensions.Logging (>= 5.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 1.1.0 - Release Notes
New Features and Improvements:
Upload from Console and Other Apps:
The upload method now supports file uploads directly from console applications, desktop apps, or anywhere byte array or stream or base64 string can be provided β no longer just limited to API scenarios with IFormFile.
Upsert Functionality:
When uploading a file with the same file name and client ID, the package now updates the existing record instead of inserting a duplicate. This upsert behavior applies to both MongoDB and SQL Server metadata stores, ensuring data consistency.
Postman Friendly:
The NuGet package supports easy testing via Postman. For Swagger UI usage, please make sure to use appropriate DTOs to pass parameters correctly.
Previous Behavior:
Uploading was limited to API endpoints accepting IFormFile.
New uploads always inserted metadata, even if a file with the same name and client ID existed, resulting in duplicates.
Benefits:
More flexible integration across different app types.
Avoids duplicate metadata entries and keeps uploads clean and consistent.
Simplifies API testing and debugging with Postman.