cl2j.FileStorage
1.0.8
dotnet add package cl2j.FileStorage --version 1.0.8
NuGet\Install-Package cl2j.FileStorage -Version 1.0.8
<PackageReference Include="cl2j.FileStorage" Version="1.0.8" />
<PackageVersion Include="cl2j.FileStorage" Version="1.0.8" />
<PackageReference Include="cl2j.FileStorage" />
paket add cl2j.FileStorage --version 1.0.8
#r "nuget: cl2j.FileStorage, 1.0.8"
#:package cl2j.FileStorage@1.0.8
#addin nuget:?package=cl2j.FileStorage&version=1.0.8
#tool nuget:?package=cl2j.FileStorage&version=1.0.8
cl2j.FileStorage
is a multi-providers .NET library written in C# that abstract file operations like read, write, delete and more. It's an open and extensible framework based on interfaces and Dependency Injection.
Providers supported:
FileStorageProviderDisk
provider for Local file systemFileStorageProviderAzureBlobStorage
provider for Azure Blob Storage
Getting started
The setup is simple.
Add the nuget package to your project:
Install-Package cl2j.FileStorage
- Add the following lines in the appsettings.json:
"cl2j": {
"FileStorage": {
"Storages": {
"Data": {
"Type": "Disk",
"Path": "c:\folder>"
}
}
}
}
In the previous example, Data is the name of the FileStorage. Type is the type of the provider, Disk indicate that you want to use a provider that will use the File.IO operations. Path is the local path, i.e. c:\dev, where the file operations will be done.
- Configures the services by calling AddFileStorage() and then UseFileStorageDisk()
- Get the IFileProvider configured and perform file operations
IFileStorageProvider works with Streams. The library offer utilities, through extensions methods, to simplify text manipulations.
Web Application
public void ConfigureServices(IServiceCollection services)
{
...
//Bootstrap the FileStorage to be available from DependencyInjection.
//This will allow accessing IFileStorageProviderFactory instance
services.AddFileStorage();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
//Add the Disk FileStorage provider
app.ApplicationServices.UseFileStorageDisk();
}
Console Application
var services = new ServiceCollection();
...
//Bootstrap the FileStorage to be available from DependencyInjection.
//This will allow accessing IFileStorageProviderFactory instance
services.AddFileStorage();
//Add the Disk FileStorage provider
var serviceProvider = services.BuildServiceProvider();
serviceProvider.UseFileStorageDisk();
Sample code
Here's a class that execute common file operations.
The class receive, by Dependency Injection, the IFileStorageProviderFactory. It request the IFileStorageProvider, represented by the name Data, and execute file operations.
internal class FileOperationSample
{
private IFileStorageProvider fileStorageProvider;
public FileOperationExecutor(IFileStorageProviderFactory fileStorageFactory)
{
//Retreive the FileStorage provider
fileStorageProvider = fileStorageFactory.Get("Data");
}
public async Task ExecuteAsync()
{
//Create a file with the specified text
await fileStorageProvider.WriteTextAsync("file1.txt", "First part of the text");
//Read the file content
var text = await fileStorageProvider.ReadTextAsync("file1.txt");
//Append text to the existing file
await fileStorageProvider.AppendTextAsync("file1.txt", "Second part of the text");
//Delete the file
await fileStorageProvider.DeleteAsync("file1.txt");
}
}
Operations
Get the File Storage Provider
To perform file operations, you need to get the IFileStorageProviderFactory and request the provider by passing it's name like:
var fileStorageProviderFactory = serviceProvider.GetRequiredService<IFileStorageProviderFactory>();
var fileStorageProvider = fileStorageProviderFactory.Get("Data");
Here's the operations available from the IFileStorageProvider
interface:
public interface IFileStorageProvider
{
// Write content
Task WriteAsync(string name, Stream stream);
// Read content
Task<bool> ReadAsync(string name, Stream stream);
// Append content
Task AppendAsync(string name, Stream stream);
// Validate if a file exists
Task<bool> ExistsAsync(string name);
// List files of directory
Task<IEnumerable<string>> ListAsync(string path);
// Get file information
Task<FileStoreFileInfo> GetInfoAsync(string name);
// Delete file
Task DeleteAsync(string name);
}
Azure Blob Storage
To use a Azure Blob Storage in youf project, add the following package:
Install-Package cl2j.FileStorage.Provider.AzureBlobStorage
In the application configuration, call UseFileStorageAzureBlobStorage()
like this:
serviceProvider.UseFileStorageAzureBlobStorage();
Add an entry in the appsetttings.json as follow:
"cl2j": {
"FileStorage": {
"Storages": {
"Azure": {
"Type": "AzureBlobStorage",
"ConnectionString": "<ConnectionStringToYourStorage>",
"Container": "<NameOfYourContainer>"
}
}
}
}
Feedback & Community
We look forward to hearing your comments. Feel free to submit your opinion, any problems you have encountered as well as ideas for improvements, we would love to hear it.
If you have a technical question or issue, please either:
- Submit an issue
- Ask a question on StackOverflow
- Contact us directly
Roadmap
We expect to add Amazon S3
and Google Cloud Storage
in the coming months.
We will also like to add:
FTP
FileStorageProvider- Copy and move utilities to simplify the operations
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.0
- cl2j.Tooling (>= 1.0.8)
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.2)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on cl2j.FileStorage:
Package | Downloads |
---|---|
cl2j.FileStorage.Provider.AzureBlobStorage
cl2j.FileStorage is a multi-providers library that abstract file operations like read, write, delete and more. It's an open and extensible framework based on interfaces and Dependency Injection. Azure Blob Storage implementation |
|
cl2j.Logging
Package Description |
|
cl2j.DataStore
Multi-providers CRUD Repository abstraction .NET library written in C# |
GitHub repositories
This package is not used by any popular GitHub repositories.