cl2j.FileStorage 1.0.8

dotnet add package cl2j.FileStorage --version 1.0.8
                    
NuGet\Install-Package cl2j.FileStorage -Version 1.0.8
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="cl2j.FileStorage" Version="1.0.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="cl2j.FileStorage" Version="1.0.8" />
                    
Directory.Packages.props
<PackageReference Include="cl2j.FileStorage" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add cl2j.FileStorage --version 1.0.8
                    
#r "nuget: cl2j.FileStorage, 1.0.8"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package cl2j.FileStorage@1.0.8
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=cl2j.FileStorage&version=1.0.8
                    
Install as a Cake Addin
#tool nuget:?package=cl2j.FileStorage&version=1.0.8
                    
Install as a Cake Tool

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 system
  • FileStorageProviderAzureBlobStorage provider for Azure Blob Storage

Getting started

The setup is simple.

Add the nuget package to your project:

Install-Package cl2j.FileStorage
  1. 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.

  1. Configures the services by calling AddFileStorage() and then UseFileStorageDisk()
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.0.8 352 11/18/2023
1.0.7 458 11/5/2023
1.0.6 189 11/5/2023
1.0.3 646 11/19/2021
1.0.2 454 11/17/2021
1.0.1 391 11/16/2021
1.0.0 388 11/16/2021