Giter VIP home page Giter VIP logo

b2.net's Introduction

B2.NET

License

B2.NET is a C# client for the Backblaze B2 Cloud Storage service.

B2.NET is still in Beta, so use it in production at your own risk.

B2 Documentation

Features

  • Full implementation of the B2 REST API
  • Experimental support for file FriendlyURL's
  • UFT-8 and Url Encoding support
  • Full Async support
  • Full test coverage
  • Targets .NET 4.5 and .NET Standard 1.5
  • NOTE: There is currently no plans to support the new Key's API's

nuget

nuget package

Install-Package B2Net

Install

Stick the B2Net.dll in your project.

Guide

Usage

// the B2Client will default to the bucketId provided here
// for all subsequent calls if you set PersistBucket to true.
var options = new B2Options() {
	AccountId = "YOUR ACCOUNT ID",
	ApplicationKey = "YOUR APPLICATION KEY",
	BucketId = "OPTIONAL BUCKET ID",
	PersistBucket = true/false
};
var client = new B2Client(Options);

Authorize

There are two ways to Authorize a B2Client. The old way is slightly more verbose and there is no clear benefit to using it over the static method. The options object returned from the Authorize method will contain the authorizationToken necessary for subsequent calls to the B2 API. This will automatically be handled by the library when necessary. You do not have to keep this object around. The options object requires AccountID and ApplicationKey to authorize.

// New, cleaner way to Authorize using the static `Authorize` method.
var client = new B2Client(B2Client.Authorize(options));
// OR
var client = new B2Client(B2Client.Authorize("ACCOUNTID", "APPLICATIONKEY"));
// OR
var client = new B2Client("ACCOUNTID", "APPLICATIONKEY", "REQUESTTIMEOUT");

The old way:

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var options = await client.Authorize();

Buckets

List Buckets

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var bucketList = await client.Buckets.GetList();
// [
//   { BucketId: "",
//     BucketName: "",
//     BucketType: "",
//     BucketInfo: Dictionary<string,string> }
// ]

Create a Bucket

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var bucket = await client.Buckets.Create("BUCKETNAME", "OPTIONAL_BUCKET_TYPE");
// { BucketId: "",
//   BucketName: "",
//   BucketType: "" }

Create a Bucket with options

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var bucket = await client.Buckets.Create("BUCKETNAME", new B2BucketOptions() {
	CacheControl = 300,
	LifecycleRules = new System.Collections.Generic.List<B2BucketLifecycleRule>() {
		new B2BucketLifecycleRule() {
			DaysFromHidingToDeleting = 30,
			DaysFromUploadingToHiding = null,
			FileNamePrefix = ""
		}
	}
});
// { BucketId: "",
//   BucketName: "",
//   BucketType: "",
//   BucketInfo: Dictionary<string,string>,
//   LifecycleRules: List<B2BucketLifecycleRule> }

Update a Bucket

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var bucket = await client.Buckets.Update("BUCKETID", "BUCKETYPE");
// { BucketId: "",
//   BucketName: "",
//   BucketType: "" }

Update a Bucket with options

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var bucket = await client.Buckets.Update("BUCKETID", new B2BucketOptions() {
	CacheControl = 300,
	LifecycleRules = new System.Collections.Generic.List<B2BucketLifecycleRule>() {
		new B2BucketLifecycleRule() {
			DaysFromHidingToDeleting = 30,
			DaysFromUploadingToHiding = null,
			FileNamePrefix = ""
		}
	}
});
// { BucketId: "",
//   BucketName: "",
//   BucketType: "",
//   BucketInfo: Dictionary<string,string>,
//   LifecycleRules: List<B2BucketLifecycleRule> }
Bucket Types
allPrivate
allPublic

Delete a Bucket

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var bucket = await client.Buckets.Delete("BUCKETID");
// { BucketId: "",
//   BucketName: "",
//   BucketType: "" }

Files

Get a list of files

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var fileList = await client.Files.GetList("BUCKETID", "FILENAME");
// Using optional prefix and delimiter
var fileList = await client.Files.GetList("BUCKETID", "FILENAME", prefix: "PREFIX", delimiter: "DELIMITER");
// {
//   NextFileName: "",
//   [
//     { FileId: "",
//       FileName: "",
//       ContentLength: "", 
//       ContentSHA1: "", 
//       ContentType: "" }
//   ]
// }

Upload a file

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var uploadUrl = await client.Files.GetUploadUrl("BUCKETID");
var file = await client.Files.Upload("FILEDATABYTES", "FILENAME", uploadUrl, "AUTORETRY", "BUCKETID", "FILEINFOATTRS");
// { FileId: "",
//   FileName: "",
//   ContentLength: "", 
//   ContentSHA1: "", 
//   ContentType: "",
//   FileInfo: Dictionary<string,string> }

Download a file by id

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var file = await client.Files.DownloadById("FILEID");
// { FileId: "",
//   FileName: "",
//   ContentLength: "", 
//   ContentSHA1: "", 
//   ContentType: "",
//   FileData: byte[],
//   FileInfo: Dictionary<string,string> }

Download a file by name

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var file = await client.Files.DownloadName("FILENAME", "BUCKETNAME");
// { FileId: "",
//   FileName: "",
//   ContentLength: "", 
//   ContentSHA1: "", 
//   ContentType: "",
//   FileData: byte[],
//   FileInfo: Dictionary<string,string> }

Get versions for a file

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var file = await client.Files.GetVersions("FILENAME", "FILEID");
// {
//   NextFileName: "",
//   NextFileId: "",
//   [
//     { FileId: "",
//       FileName: "",
//       Action: "", 
//       Size: "", 
//       UploadTimestamp: "" }
//   ]
// }

Delete a file version

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var file = await client.Files.Delete("FILEID", "FILENAME");
// { FileId: "",
//   FileName: ""}

Hide a file version

var client = new B2Client("ACCOUNTID", "APPLICATIONKEY");
var file = await client.Files.Hide("FILEID", "BUCKETID");
// { FileId: "",
//   FileName: "",
//   Action: "",
//   Size: "",
//   UploadTimestamp: ""}

Get info for a file

var client = new B2Client(options);
await client.Authorize();
var file = client.Files.GetInfo("FILEID").Result;
// { FileId: "",
//   FileName: "",
//   ContentSHA1: "",
//   BucketId: "",
//   ContentLength: "",
//   ContentType: "",
//   FileInfo: Dictionary<string,string> }

Large File API

See the Large File tests for usage details.

Errors

Certain errors returned by B2 point to a temporary error with the upload or download of a file. If one of these errors are encountered during an upload or download then the B2Exception that is returned will have the ShouldRetryRequest flag marked true. This is an indication that you should retry the request if you are so inclined.

Release Notes

  • 0.5.2 Implemented Interfaces for easier testing (from @mattdewn) and fixed tests
  • 0.5.0 Large file management apis (version mix up as well)
  • 0.4.9 static Authorize
  • 0.4.6 Configureable HttpClient timeout
  • 0.4.5 File prefix and delimiter support
  • 0.4.0 Added Large File support
  • 0.2.5 Support for .NET 4.5 and .NET Standard 1.5 and FriendlyURL's for files
  • 0.1.92 Fixed Lifecycle Rules null issue.
  • 0.1.9 Added Lifecycle Rules and Cache Control to bucket creation and updating.
  • 0.1.81 Switch targeting to netstandard1.3 and updated B2 endpoint
  • 0.1.7 Merged changes for file name encoding and maxFileCount from tomvoros
  • 0.1.6 Started URL Encoding file names.
  • 0.1.5 File info attributes support.
  • 0.1.0 Initial Alpha release.

Running the tests

From the src directory run

dotnet pack -o ..\tests\

From the tests directory run

dotnet restore
dotnet test

b2.net's People

Contributors

coryrwest avatar mattdwen avatar slieser avatar tomvoros avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.