file-upload-helper
1.2.0
See the version list below for details.
dotnet add package file-upload-helper --version 1.2.0
NuGet\Install-Package file-upload-helper -Version 1.2.0
<PackageReference Include="file-upload-helper" Version="1.2.0" />
paket add file-upload-helper --version 1.2.0
#r "nuget: file-upload-helper, 1.2.0"
// Install file-upload-helper as a Cake Addin #addin nuget:?package=file-upload-helper&version=1.2.0 // Install file-upload-helper as a Cake Tool #tool nuget:?package=file-upload-helper&version=1.2.0
<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>
<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.
- Currently, it supports 2 cloud storage providers: Google Firebase Storage and Azure Blob Storage.
- It also offers the ability to write your files on the server's
wwwroot
directory.
<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:
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.0.103
</span> - Package Manager: <span style="color:#a71d5d;">
Install-Package file-upload-helper -Version 1.0.103
</span>
- .NET CLI: <span style="color:#a71d5d;">
That's it! Your markdown file is now beautifully styled with colors. Let me know if there's anything else I can assist you with!
Interface Methods (IUploadFileStrategy)
public interface IUploadFileStrategy
{
bool Remove(string path, string filename);
string Upload(string path, IFormFile file);
Task<string> UploadAsync(string path, IFormFile file, CancellationToken cancellationToken = default);
}
- Upload() : <span style="color: blue;">uploads</span> your file synchronously using any of your chosen strategy and takes in <span style="color: purple;">containerName/filepath</span> and <span style="color: green;">IFormFile</span>.
- 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>.
- Remove() : <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 three strategies to choose from namely
- <span style="color: blue;">LocalFileUploadHelper</span>
- <span style="color: purple;">AzureStorageFileUploadHelper</span>
- <span style="color: green;">FirebaseStorageFileUploadHelper</span>
with future plans to support <span style="color: orange;">AWS S3 Bucket</span> storage in the next versions
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));
}
user.ImageUrl = await _fileStrategy.UploadAsync("Images",image);;
<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 IFileUpload<Accomodation> _fileUpLoad;
private readonly BlobServiceClient client;
public AccomodationRepo(BlobServiceClient client)
{
web = host;
_fileUpLoad = new AccomodationUpload(client);
db = context;
this.client = client;
}
CoverPhoto = await _fileUpLoad.UploadAsync(Params.CoverImage),
<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; }
}
CategoryImageUrl = await _uploadHelper.UploadAsync("CategoryPictures",category.CategoryImageUrl, cancellationToken)
<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));
}
return pictures;
}
<span style="color: #4285F4; font-size: 24px;">That's it, thank you for the read...</span>
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. |
-
net6.0
- Azure.Storage.Blobs (>= 12.13.1)
- Firebase.Auth (>= 1.0.0)
- FirebaseStorage.net (>= 1.0.3)
- Microsoft.AspNetCore.Hosting (>= 2.2.7)
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
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 | 137 | 1/30/2024 |
1.3.6 | 158 | 1/2/2024 |
1.3.5 | 124 | 1/2/2024 |
1.3.4.1 | 113 | 1/1/2024 |
1.3.4 | 113 | 1/1/2024 |
1.3.3 | 117 | 12/29/2023 |
1.3.2 | 111 | 12/29/2023 |
1.3.1 | 118 | 12/29/2023 |
1.3.0 | 125 | 12/29/2023 |
1.2.101 | 188 | 6/22/2023 |
1.2.1 | 129 | 6/22/2023 |
1.2.0 | 151 | 6/20/2023 |
1.0.103 | 156 | 6/20/2023 |
1.0.102 | 141 | 6/20/2023 |
1.0.101 | 150 | 6/20/2023 |
1.0.10 | 167 | 6/20/2023 |
1.0.8 | 345 | 10/13/2022 |