file-upload-helper 1.3.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package file-upload-helper --version 1.3.4                
NuGet\Install-Package file-upload-helper -Version 1.3.4                
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="file-upload-helper" Version="1.3.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add file-upload-helper --version 1.3.4                
#r "nuget: file-upload-helper, 1.3.4"                
#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.
// Install file-upload-helper as a Cake Addin
#addin nuget:?package=file-upload-helper&version=1.3.4

// Install file-upload-helper as a Cake Tool
#tool nuget:?package=file-upload-helper&version=1.3.4                

<span style="color:#337ab7;">FILE-UPLOAD-HELPER</span>

<span style="color:#337ab7;">Hi there! Welcome to the <span style="color:#ff7f50;">file-upload-helper</span> docs</span>

Added Features

Support for dependency injection AWSS3 - AddFileWithAWSS3trategy Azure - AddFileWithAZureBlobStrategy Firebase - AddFileWithAFirebaseStorageStrategy FileSystem - AddFileWithLocalFileSystemStrategy

<span style="color:#ff7f50;">File-upload-helper</span> is a tool designed to help .Net developers speed up their development by offering functionalities like:

  • Uploading files to different servers and cloud providers for storage.
  • Supported cloud storage providers: AWS S3, Google Firebase Storage and Azure Blob Storage.
  • It also offers the ability to write your files on the server's wwwroot directory/filesystem.

<span style="color:#337ab7;">Usage</span>

The library provides a convenient interface for interacting with the code and allows you to switch between different providers easily. To use the library, follow these steps:

  1. Add the nuget package to your project through nuget.org.

    Alternatively, you can use the following commands to reference the package to your project:

    • .NET CLI: <span style="color:#a71d5d;">dotnet add package file-upload-helper --version 1.3.4</span>
    • Package Manager: <span style="color:#a71d5d;">Install-Package file-upload-helper -Version 1.3.4</span>

Interface Methods (IUploadHelperStrategy)

public interface IUploadHelperStrategy
{
Task<bool> Remove(string path, string filename);
Task<string> UploadAsync( IFormFile file,string path ="", CancellationToken cancellationToken = default);
}
  • UploadAsync() : <span style="color: blue;">uploads</span> your file asynchronously using any of your chosen strategy and takes in <span style="color: purple;">containerName/filepath</span>, <span style="color: green;">IFormFile</span>, and a <span style="color: orange;">cancellationToken</span>.
  • RemoveAsync() : <span style="color: blue;">removes</span> the file based on the <span style="color: purple;">filename</span>. Takes in the <span style="color: purple;">containerName/filepath</span> and <span style="color: purple;">filename</span>.

The Concrete Implementation of the above interface constructor looks like below to support the swapping of multiple strategies

 public UploadFileStrategy(IUploadHelper uploadHelper)
        {
            _uploadHelper = uploadHelper;
        }

There are 4 strategies to choose from namely

  • <span style="color: blue;">LocalFileUploadHelper</span>
  • <span style="color: purple;">AzureStorageFileUploadHelper</span>
  • <span style="color: green;">FirebaseStorageFileUploadHelper</span>
  • <span style="color: green;">AWSS3FileUploadStrategy</span>

How to Use the Library With the different Strategies

  1. <span style="color: blue;">LocalFileUploadHelper</span>

To use the <span style="color: blue;">LocalFileUploadHelper</span> strategy, you need to inject the necessary dependencies.

 private readonly IUploadFileStrategy _fileStrategy;

    public UpdateUserImageRepository(IWebHostEnvironment hostEnvironment)
    {
        _hostEnvironment = hostEnvironment;
        _fileStrategy = new UploadFileStrategy(new LocalFileUploadHelper(hostEnvironment));
    }
await _fileStrategy.UploadAsync("Images",image, cancellationToken); //returns filename

<span style="color: #4285F4; font-weight: bold;">After injecting the dependencies, you only need one line to upload your file.</span>

<span style="color: #4285F4; font-weight: bold;">2) AzureStorageFileUploadHelper:</span>
<span style="color: #4285F4; font-weight: bold;">Need to inject the necessary dependencies.</span>

In Program.cs, add the following code:

 services.AddScoped(_ => {
                return new BlobServiceClient(BlobConfig.BlobConString);
            });

Custom Object

 public class BlobConfig
    {
        public  static string BlobConString { get { return "DefaultEndpointsProtocol=https;AccountName=yourAzureBlobStorageAccountName;AccountKey=yourApiKey"; } }
        public static string ContainerName { get { return "yourAzureBlobContainerName"; } }
    }
private readonly IUploadFileStrategy _fileStrategy;
public AccomodationRepo(BlobServiceClient client)
  {
         _fileUpLoad = new UploadFileStrategy(new AzureStorageFileUploadHelper(client));
  }

await _fileUpLoad.UploadAsync("Products",Params.CoverImage, cancellationToken); //returns filename

<span style="color: #4285F4; font-weight: bold;">This is how convenient all the methods are just a one liner</span>

<span style="color: #4285F4; font-weight: bold;">2) FirebaseStorageFileUploadHelper:</span>
<span style="color: #4285F4; font-weight: bold;">Need to inject the necessary dependencies.</span>

private readonly IUploadFileStrategy _uploadHelper;

    public ProductCategoryService(IConfiguration configuration)
    {
        _uploadHelper = new UploadFileStrategy(new FirebaseStorageFileUploadHelper(new FirebaseStorageConfiguration
        {
            ApiKey = configuration["Firebase:ApiKey"],
            Bucket = configuration["Firebase:Bucket"],
            AuthEmail = configuration["Firebase:AuthEmail"],
            AuthPassword = configuration["Firebase:AuthPassword"]
        }));
    }
public class FirebaseStorageConfiguration
{
    public string ApiKey { get; set; }
    public string Bucket { get; set; }
    public string AuthEmail { get; set; }
    public string AuthPassword { get; set; }
}
 await _uploadHelper.UploadAsync("CategoryPictures",category.CategoryImageUrl, cancellationToken) //returns downloadlink

<span style="color: #4285F4; font-weight: bold;">The above strategies offer one-liners and a few configs, and that's it. You have access to all the providers.</span> <span style="color: #4285F4; font-weight: bold;">Below is an example of how to upload multiple images:</span>

private static async Task<IEnumerable<string>> AddProductPictures(IFormFileCollection collection)
    {
        var pictures = new List<string>();
        foreach (var image in collection)
        {
            pictures.Add(await _fileStrategy.UploadAsync("ProductPictures", image, cancellationToken));
        }
        return pictures;
    }

<span style="color: #4285F4; font-size: 24px;">That's it, thank you for the read...</span>

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.6.1 135 1/30/2024
1.3.6 156 1/2/2024
1.3.5 122 1/2/2024
1.3.4.1 111 1/1/2024
1.3.4 111 1/1/2024
1.3.3 115 12/29/2023
1.3.2 107 12/29/2023
1.3.1 115 12/29/2023
1.3.0 121 12/29/2023
1.2.101 186 6/22/2023
1.2.1 127 6/22/2023
1.2.0 149 6/20/2023
1.0.103 154 6/20/2023
1.0.102 139 6/20/2023
1.0.101 148 6/20/2023
1.0.10 165 6/20/2023
1.0.8 340 10/13/2022