In this section, we introduce File storage to build a file management system.
This service will resolves Storage according to its identity (it is storage’s web endpoint by default).
It is a proxy that contains backend storage endpoints so we can access storage using multiple endpoints and protocols.
It provides methods to access the endpoint depending on its protocol. By default, we have implemented FTP, Samba and LocalDisk access providers.
It manages the upload process by storing the upload process to IUploadRepo to continue it and can load-balance between servers.
After a successful upload, it will try to add the uploaded file to IFileRepo for storage.
This middleware handles requests, attemps to resolve Storage by requesting enpoint and operates it.
To use storage services, please call the AddStorage() on IServiceCollection instance. By default, we have implemented in-memory repositories to store upload processes.
// Program.cs
using Juice.Storage.Extensions;
using Juice.Storage.Local;
...
var builder = WebApplication.CreateBuilder(args);
// add storage services
builder.Services.AddStorage();
// use in-memory repos
builder.Services.AddInMemoryUploadManager(builder.Configuration.GetSection("Juice:Storage"));
// maintain timed-out uploads
builder.Services.AddInMemoryStorageMaintainServices(builder.Configuration.GetSection("Juice:Storage"),
new string[] { "/storage", "/storage1" },
options =>
{
options.CleanupAfter = TimeSpan.FromMinutes(5);
options.Interval = TimeSpan.FromMinutes(1);
});
// access FTP, Samba, LocalDisk enpoints
builder.Services.AddLocalStorageProviders();
...
var app = builder.Build();
...
app.UseStorage(options =>
{
options.Endpoints = new string[] { "/storage", "/storage1" };
options.SupportDownloadByPath = true;
});
app.Run();
{
"Juice:Storage": {
"Storages": [
{
"WebBasePath": "/storage",
"Endpoints": [
{
"Protocol": "LocalDisk",
"Uri": "C:\\Workspace\\Storage"
}
]
},
{
"WebBasePath": "/storage1",
"Endpoints": [
{
"Protocol": "Smb",
"BasePath": "\\\\ip-or-name",
"Uri": "\\\\ip-or-name\\path\\to\\storage",
//"Identity": "demo",
//"Password": "demo"
}
]
}
]
}
}
In these options:
We can configure StorageMaintainOptions using configuration section or configure action (see the code block above).
We can configure StorageMiddlewareOptions directly using configure action.
We can configure UploadOptions using configuration section or configure action.
NOTE
If you have added app.UseAuthorization() then the storage authorization policies need to be configured.
// Program.cs
using Juice.Storage.Authorization;
...
builder.Services.AddAuthorization(options =>
{
options.AddPolicy(StoragePolicies.CreateFile, policy =>
{
// policy requirements
});
options.AddPolicy(StoragePolicies.DownloadFile, policy =>
{
// policy requirements
});
});
The sample source code is available on github.
The library can be accessed via Nuget and npmjs: