Skip to content

Repository files navigation

Backblaze Agent for .NET Core

NuGet VersionNuGet Downloads

The Backblaze Agent (client) for .NET Core is an implementation of the Backblaze B2 Cloud Storage API. Backblaze B2 Cloud Storage provides the cheapest cloud storage available on the internet. Backblaze B2 Cloud Storage is ¼ of the price of other storage providers. Give it a try as the first 10 GB of storage is free.

Features

  • Full support for Backblaze B2 Cloud Storage API v2 including files, accounts, keys and buckets.
  • Built targeting .NET Standard 2.0 which means Backblaze Agent will work on Windows, Mac and Linux systems.
  • Seamlessly intergrates with .NET Core Dependency Injection and HttpClientFactory to implement resilient requests.
  • Simple in-memory response cache using MemoryCache.
  • Large file support with low memory allocation and IProgress status.
  • Native support of task based programming model (async/await).

For feature requests and bug reports, please open an issue on GitHub.

Installation via .NET CLI

To install Backblaze.Agent run the following command:

> dotnet add package Backblaze.Agent

Getting Started

Work in Progress! Whilst we encourage users to play with the samples and test programs, this project has not yet reached a stable state.

You will need an key_id and an application_key to configure Backblaze Agent. You can obtain these from the Backblaze B2 Cloud Storage portal. See the Sample Project for an example of how to use this packages.

Adding Backblaze Agent to Services

publicvoidConfigureServices(IServiceCollectionservices){services.AddBackblazeAgent(options =>{options.KeyId="[key_id]";options.ApplicationKey="[application_key]";});}

To get a list of backblaze buckets simply inject IStorageClient into your class and call the async client.

publicclassIndexModel:PageModel{privatereadonlyIStorageClient_storage;publicIndexModel(IStorageClientstorage){_storage=storage;}[BindProperty]publicIEnumerable<BucketItem>Buckets{get;privateset;}publicasyncTask<IActionResult>OnGetAsync(){Buckets=await_storage.Buckets.GetAsync();if(Buckets==null){returnNotFound();}returnPage();}}

Initializing Backblaze Client without Dependency Injection

Install the following packages:

> dotnet add package Backblaze.Client
> dotnet add package Microsoft.Extensions.Caching.Memory
> dotnet add package Microsoft.Extensions.Logging.Debug

Sample Code:

classProgram{privatestaticIStorageClientClient;staticasyncTaskMain(string[]args){try{varoptions=newClientOptions();varloggerFactory=LoggerFactory.Create(builder =>{builder.AddFilter("Bytewizer.Backblaze",LogLevel.Trace).AddDebug();});varcache=newMemoryCache(newMemoryCacheOptions());Client=newBackblazeClient(options,loggerFactory,cache);awaitClient.ConnectAsync("[key_id]","[application_key]");varbuckets=awaitClient.Buckets.GetAsync();foreach(varbucketinbuckets)Console.WriteLine($"Bucket Name: {bucket.BucketName} - Type: {bucket.BucketType}");}catch(Exceptionex){Console.Error.WriteLine(ex.Message);}}}

Initializing Backblaze Client without Dependency Injection, Logging or Caching

Install the following package:

> dotnet add package Backblaze.Client

Sample Code:

classProgram{privatestaticIStorageClientClient;staticasyncTaskMain(string[]args){try{Client=newBackblazeClient();Client.Connect("[key_id]","[application_key]");varbuckets=awaitClient.Buckets.GetAsync();foreach(varbucketinbuckets){Console.WriteLine($"Bucket Name: {bucket.BucketName} - Type: {bucket.BucketType}");}}catch(Exceptionex){Console.Error.WriteLine(ex.Message);}}}

Basic Usage

Upload File Stream

foreach(varfilePathinDirectory.GetFiles(@"c:\my\directory")){using(varstream=File.OpenRead(filePath)){varresults=awaitClient.UploadAsync("[BucketId]",newFileInfo(filePath).Name,stream);}}

Download File Stream

varfiles=newstring[]{@"c:\my\directory\file1.txt","file2.bat"};foreach(varfileNameinfiles){using(varstream=File.Create(fileName)){varresults=awaitClient.DownloadAsync("[BucketName]",fileName,stream);}}

Microsoft Logging Integration

Install the Microsoft.Extensions.Logging packages:

> dotnet add package Microsoft.Extensions.Logging.Debug

Tracing to the Debug window can be enabled with the following code:

services.AddLogging(builder =>{builder.AddDebug();builder.AddFilter("Bytewizer.Backblaze",LogLevel.Trace);});

Agent Options

services.AddBackblazeAgent(options =>{options.KeyId="[key_id]";options.ApplicationKey="[application_key]";});

The following table describes the Agent Options available:

Option NameDefaultDescription
KeyId---Required - The key identifier used to authenticate.
ApplicationKey---Required - The secret part of the key used to authenticate.
HandlerLifetime600The time in seconds that the message handler instance can be reused.
Timeout600The time in seconds to wait before the client request times out.
RetryCount5The number of times the client will retry failed requests before timing out.
RequestMaxParallel10The maximum number of parallel request connections established.
DownloadMaxParallel5The maximum number of parallel download connections established.
DownloadCutoffSize100MBDownload cutoff size for switching to chunked parts in bytes.
DownloadPartSize100MBDownload part size of chunked parts in bytes.
UploadMaxParallel3The maximum number of parallel upload connections established.
UploadCutoffSize100MBUpload cutoff size for switching to chunked parts in bytes.
UploadPartSize100MBUpload part size of chunked parts in bytes.
AutoSetPartSizefalseUse the recommended part size returned by the Backblaze service.
ChecksumDisabledfalseThis is for testing use only and not recomended for production environments.
TestMode---This is for testing use only and not recomended for production environments.

Test Mode Options

services.AddBackblazeAgent(options =>{// This is for testing use only and not recomended for production environments.options.TestMode="fail_some_uploads";});

The following test mode options are available to verify that your code correctly handles error conditions.

Option StringDescription
fail_some_uploadsRandom uploads fail or become rejected by the service.
expire_some_account_authorization_tokensRandom account authorization tokens expire.
force_cap_exceededCap exceeded conditions are forced.

Integration Tests

You will need an key_id and an application_key to configure Backblaze Test Agent settings.json file.

Disclaimer

All source, documentation, instructions and products of this project are provided as-is without warranty. No liability is accepted for any damages, data loss or costs incurred by its use.

Branches

master - This is the branch containing the latest release - no contributions should be made directly to this branch.

develop - This is the development branch to which contributions should be proposed by contributors as pull requests. This development branch will periodically be merged to the master branch, and be released to NuGet Gallery.

Contributions

Contributions to this project are always welcome. Please consider forking this project on GitHub and sending a pull request to get your improvements added to the original project.

About

Backblaze.Agent is a high-performance .NET Core implementation of the Backblaze B2 Cloud Storage API.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages