@tweedegolf/sab-adapter-backblaze-b2
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Backblaze B2 Storage Adapter

An adapter that provides an abstraction layer over the API of the Backblaze B2 cloud storage service.

This adapter is one of the adapters that is meant to be used as a plugin of the Storage Abstraction package. However it can be used standalone as well, see below.

The API of the adapter abstracts away the differences between the API's of cloud storage services. The API only supports the basic, most commonly used cloud service operations such as creating a bucket, storing files and so on.

It is also possible to access all the specific functionality of the cloud service API through the service client of the adapter, see here.

If you are new to the Storage Abstraction library you may want to read this first.

import { Storage, StorageType } from "@tweedegolf/storage-abstraction";

const configuration = {
  type: StorageType.B2,
  applicationKey: "key",
  applicationKeyId: "keyId",
};

const storage = new Storage(configuration);

const result = await storage.listBuckets();

console.log(result);

The Storage class is cloud service agnostic and doesn't know anything about the adapter it uses and adapters are completely interchangeable. It only expects the adapter to have implemented all methods of the IAdapter interface, see the API.

When you create a Storage instance it checks the mandatory type key in the configuration object and then loads the appropriate adapter module automatically from your node_modules folder using require(). For more information please read this.

Configuration

The configuration object that you pass to the Storage constructor is forwarded to the constructor of the adapter.

The Storage constructor is only interested in the type key of the configuration object, all other keys are necessary for configuring the adapter.

The Storage constructor expects the configuration to be of type StorageAdapterConfig.

The adapter expects the configuration to be of type AdapterConfig or a type that extends this type.

export interface AdapterConfig {
  bucketName?: string;
  [id: string]: any; // any mandatory or optional key
}

export interface StorageAdapterConfig extends AdapterConfig {
  type: string;
}

The type of the configuration object for this adapter:

export interface AdapterConfigB2 extends AdapterConfig {
  applicationKey: string;
  applicationKeyId: string;
}

Examples

Examples with configuration object:

const s = new Storage({
  type: StorageType.B2,
  applicationKeyId: "your-key-id",
  applicationKey: "your-key",
});

const s = new Storage({
  type: StorageType.B2,
  applicationKeyId: "your-key-id",
  applicationKey: "your-key",
  bucketName: "the-buck",
});

Same examples with configuration url:

const s = new Storage("b2://your-key-id:your-key");

const s = new Storage("b2://your-key-id:your-key@the-buck");

For more information about configuration urls please read this.

Standalone

You can also use the adapter standalone, without the need to create a Storage instance:

import { AdapterBackblazeB2 } from "@tweedegolf/sab-adapter-backblaze-b2";

const a = new AdapterBackblazeB2({
  applicationKey: "key",
  applicationKeyId: "keyId",
});
const r = await a.listBuckets();
console.log(r);

API

For a complete description of the Adapter API see this part documentation of the Storage Abstraction package readme.

/@tweedegolf/sab-adapter-backblaze-b2/

    Package Sidebar

    Install

    npm i @tweedegolf/sab-adapter-backblaze-b2

    Weekly Downloads

    67

    Version

    1.0.5

    License

    MIT

    Unpacked Size

    98.1 kB

    Total Files

    27

    Last publish

    Collaborators

    • daniel2g
    • marlonb