Skip to content

simplify9/CloudFiles

Repository files navigation

Banner

Package Version
SimplyWorks.HttpExtensions Nuget

License Contributions welcome

Introduction

CloudFiles is a minimalist library that abstracts the Amazon S3 SDK. It has the core needed from a file-uploading library without the hassle of going through mountains of documentation.

CloudFiles has extensions to integrate it into the ASP dotnet core dependency injection. It covers multiple ways to upload data, including opening writable streams or simply uploading a file's data from IFormFile or similar.

Installation

There are two NuGet packages for Cloudfiles, one for being the actual service, installed with:

dotnet add package Simplyworks.CloudFiles

While the other is used to integrate it into the dependency injection, with:

dotnet add package Simplyworks.CloudFiles.Extensions

Getting Started

To register Cloudfiles, use the service collection extension method. Add Cloudfiles to your startup file and pass the configuration in one of these two ways:

  1. Configure your details in the AppSettings.json file and then call .AddCloudFiles() in your Startup file. Here's how:
 "CloudFiles": {
    "AccessKeyId": "",
    "SecretAccessKey": "",
    "BucketName": "",
    "ServiceUrl": ""
  }, 
  1. Use the AddCloudFiles function in your Startup file and specify your parameters like so:
.AddCloudFiles( config =>
         config.AccessKeyId = ""
         config.SecretAccessKey = "";
         config.ServiceUrl = "";
         config.BucketName = "";
 ) 

Then simply add the ICoudFilesService interface (from PrimitiveTypes) in the constructor of a controller for it to be injected, then use the functions provided!

Examples

Reading from Cloud bucket example:

We initialize this function with its corresponding primitive type, and it then reads a file from the bucket and writes it onto the local disk.

async public Task TestOpenReadAcync()
{
    var cloudFiles = server.Host.Services.GetService<ICloudFilesService>();

    using var stream = await cloudFiles.OpenReadAsync("test/TestWriteAcync.txt");
    using var diskFile = File.OpenWrite(@"c:\temp\sample.txt");
    await stream.CopyToAsync(diskFile);
}

CloudFiles used in an ASP Controller endpoint:

[HttpPost]
[Route("{**directory}")]
public async Task<IActionResult> UploadBlobToCloud([FromRoute]string directory, [FromForm]IFormFile file)
{
        string directoryPath = directory.EndsWith('/') ? directory : directory + '/';
        if(_contentTypeProvider.TryGetContentType(file.FileName, out string mimeType))
        {
                var blob = await cloudFilesService.WriteAsync(file.OpenReadStream(), new PrimitiveTypes.WriteFileSettings
                {
                        ContentType = mimeType,
                        Key = directoryPath +  file.FileName,
                        CloseInputStream = false,
                        Public = true,
                        Metadata = new Dictionary<string, string>()
                });
                return Ok(blob.Location);
        } throw new Exception("Invalid form file");
}

Getting support 👷

If you encounter any bugs, don't hesitate to submit an issue. We'll get back to you promptly!

About

Abstraction of Amazon S3 SDK for the needed file operations, utilizing streams, and Asp.Net Core dependency injection

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages