@tweedegolf/sab-adapter-backblaze-b2 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -0
- package/dist/AbstractAdapter.d.ts +33 -0
- package/dist/AbstractAdapter.js +74 -0
- package/dist/AbstractAdapter.js.map +1 -0
- package/dist/AdapterBackblazeB2.d.ts +35 -0
- package/dist/AdapterBackblazeB2.js +498 -0
- package/dist/AdapterBackblazeB2.js.map +1 -0
- package/dist/index/AdapterBackblazeB2.d.ts +5 -0
- package/dist/index/AdapterBackblazeB2.js +10 -0
- package/dist/index/AdapterBackblazeB2.js.map +1 -0
- package/dist/types/adapter_backblaze_b2.d.ts +71 -0
- package/dist/types/adapter_backblaze_b2.js +3 -0
- package/dist/types/adapter_backblaze_b2.js.map +1 -0
- package/dist/types/add_file_params.d.ts +40 -0
- package/dist/types/add_file_params.js +3 -0
- package/dist/types/add_file_params.js.map +1 -0
- package/dist/types/general.d.ts +179 -0
- package/dist/types/general.js +14 -0
- package/dist/types/general.js.map +1 -0
- package/dist/types/result.d.ts +41 -0
- package/dist/types/result.js +3 -0
- package/dist/types/result.js.map +1 -0
- package/dist/util.d.ts +98 -0
- package/dist/util.js +223 -0
- package/dist/util.js.map +1 -0
- package/package.json +30 -0
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Backblaze B2 Storage Adapter
|
|
2
|
+
|
|
3
|
+
This adapter is a peer dependency of the [storage abstraction package](https://www.npmjs.com/package/@tweedegolf/storage-abstraction). It provides an abstraction layer over the API of the Backblaze B2 cloud storage service.
|
|
4
|
+
|
|
5
|
+
If you are new to the Storage Abstraction library you may want to read [this](https://github.com/tweedegolf/storage-abstraction/blob/master/README.md#how-it-works) first.
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { Storage, StorageType } from "@tweedegolf/storage-abstraction";
|
|
9
|
+
|
|
10
|
+
const configuration = {
|
|
11
|
+
type: StorageType.B2,
|
|
12
|
+
applicationKey: "key",
|
|
13
|
+
applicationKeyId: "keyId",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const storage = new Storage(configuration);
|
|
17
|
+
|
|
18
|
+
const result = await storage.listBuckets();
|
|
19
|
+
|
|
20
|
+
console.log(result);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
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](https://github.com/tweedegolf/storage-abstraction/blob/master/README.md#adapter-api).
|
|
24
|
+
|
|
25
|
+
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](https://github.com/tweedegolf/storage-abstraction/blob/master/README.md#register-your-adapter).
|
|
26
|
+
|
|
27
|
+
## Configuration
|
|
28
|
+
|
|
29
|
+
The configuration object that you pass to the Storage constructor is forwarded to the constructor of the adapter.
|
|
30
|
+
|
|
31
|
+
The Storage constructor is only interested in the `type` key of the configuration object, all other keys are necessary for configuring the adapter.
|
|
32
|
+
|
|
33
|
+
The Storage constructor expects the configuration to be of type `StorageAdapterConfig`.
|
|
34
|
+
|
|
35
|
+
The adapter expects the configuration to be of type `AdapterConfig` or a type that extends this type.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
export interface AdapterConfig {
|
|
39
|
+
bucketName?: string;
|
|
40
|
+
[id: string]: any; // any mandatory or optional key
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface StorageAdapterConfig extends AdapterConfig {
|
|
44
|
+
type: string;
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The type of the configuration object for this adapter:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
export interface AdapterConfigB2 extends AdapterConfig {
|
|
52
|
+
applicationKey: string;
|
|
53
|
+
applicationKeyId: string;
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Examples
|
|
58
|
+
|
|
59
|
+
Example with configuration object:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const s = new Storage({
|
|
63
|
+
type: StorageType.B2,
|
|
64
|
+
applicationKey: "key",
|
|
65
|
+
applicationKeyId: "keyId",
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Example with configuration url:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const s = new Storage("b2://applicationKeyId=keyId&applicationKey=key");
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
For more information about configuration urls please read [this](https://github.com/tweedegolf/storage-abstraction/blob/master/README.md#configuration-url).
|
|
76
|
+
|
|
77
|
+
## Standalone
|
|
78
|
+
|
|
79
|
+
You can also use the adapter standalone, without the need to create a Storage instance:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { AdapterBackblazeB2 } from "@tweedegolf/sab-adapter-backblaze-b2";
|
|
83
|
+
|
|
84
|
+
const a = new AdapterBackblazeB2({
|
|
85
|
+
applicationKey: "key",
|
|
86
|
+
applicationKeyId: "keyId",
|
|
87
|
+
});
|
|
88
|
+
const r = await a.listBuckets();
|
|
89
|
+
console.log(r);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## API
|
|
93
|
+
|
|
94
|
+
For a complete description of the Adapter API see [this part](https://github.com/tweedegolf/storage-abstraction/blob/master/README.md#adapter-api) documentation of the Storage Abstraction package readme.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AdapterConfig, IAdapter, Options, StreamOptions } from "./types/general";
|
|
2
|
+
import { FileBufferParams, FilePathParams, FileStreamParams } from "./types/add_file_params";
|
|
3
|
+
import { ResultObject, ResultObjectBoolean, ResultObjectBuckets, ResultObjectFiles, ResultObjectNumber, ResultObjectStream } from "./types/result";
|
|
4
|
+
export declare abstract class AbstractAdapter implements IAdapter {
|
|
5
|
+
protected _type: string;
|
|
6
|
+
protected _config: AdapterConfig | null;
|
|
7
|
+
protected _configError: string | null;
|
|
8
|
+
protected _client: any;
|
|
9
|
+
constructor(config: string | AdapterConfig);
|
|
10
|
+
get type(): string;
|
|
11
|
+
getType(): string;
|
|
12
|
+
get config(): AdapterConfig;
|
|
13
|
+
getConfig(): AdapterConfig;
|
|
14
|
+
get configError(): string;
|
|
15
|
+
getConfigError(): string;
|
|
16
|
+
get serviceClient(): any;
|
|
17
|
+
getServiceClient(): any;
|
|
18
|
+
addFileFromPath(params: FilePathParams): Promise<ResultObject>;
|
|
19
|
+
addFileFromBuffer(params: FileBufferParams): Promise<ResultObject>;
|
|
20
|
+
addFileFromStream(params: FileStreamParams): Promise<ResultObject>;
|
|
21
|
+
abstract addFile(paramObject: FilePathParams | FileBufferParams | FileStreamParams): Promise<ResultObject>;
|
|
22
|
+
abstract createBucket(name: string, options?: Options): Promise<ResultObject>;
|
|
23
|
+
abstract clearBucket(name: string): Promise<ResultObject>;
|
|
24
|
+
abstract deleteBucket(name: string): Promise<ResultObject>;
|
|
25
|
+
abstract listBuckets(): Promise<ResultObjectBuckets>;
|
|
26
|
+
abstract getFileAsStream(bucketName: string, fileName: string, options?: StreamOptions): Promise<ResultObjectStream>;
|
|
27
|
+
abstract getFileAsURL(bucketName: string, fileName: string, options?: Options): Promise<ResultObject>;
|
|
28
|
+
abstract removeFile(bucketName: string, fileName: string, allVersions?: boolean): Promise<ResultObject>;
|
|
29
|
+
abstract listFiles(bucketName: string, maxFiles: number): Promise<ResultObjectFiles>;
|
|
30
|
+
abstract sizeOf(bucketName: string, fileName: string): Promise<ResultObjectNumber>;
|
|
31
|
+
abstract fileExists(bucketName: string, fileName: string): Promise<ResultObjectBoolean>;
|
|
32
|
+
abstract bucketExists(name: string): Promise<ResultObjectBoolean>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AbstractAdapter = void 0;
|
|
13
|
+
const util_1 = require("./util");
|
|
14
|
+
class AbstractAdapter {
|
|
15
|
+
constructor(config) {
|
|
16
|
+
this._type = "abstract-adapter";
|
|
17
|
+
this._configError = null;
|
|
18
|
+
this._client = null; // eslint-disable-line
|
|
19
|
+
if (typeof config === "string") {
|
|
20
|
+
const p = config.indexOf("://");
|
|
21
|
+
if (p !== -1) {
|
|
22
|
+
// strip the type, we don't need it anymore at this point
|
|
23
|
+
config = config.substring(p);
|
|
24
|
+
}
|
|
25
|
+
this._config = (0, util_1.parseQueryString)(config);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this._config = Object.assign({}, config);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
get type() {
|
|
32
|
+
return this._type;
|
|
33
|
+
}
|
|
34
|
+
getType() {
|
|
35
|
+
return this.type;
|
|
36
|
+
}
|
|
37
|
+
get config() {
|
|
38
|
+
return this._config;
|
|
39
|
+
}
|
|
40
|
+
getConfig() {
|
|
41
|
+
return this.config;
|
|
42
|
+
}
|
|
43
|
+
get configError() {
|
|
44
|
+
return this._configError;
|
|
45
|
+
}
|
|
46
|
+
getConfigError() {
|
|
47
|
+
return this.configError;
|
|
48
|
+
}
|
|
49
|
+
// eslint-disable-next-line
|
|
50
|
+
get serviceClient() {
|
|
51
|
+
return this._client;
|
|
52
|
+
}
|
|
53
|
+
// eslint-disable-next-line
|
|
54
|
+
getServiceClient() {
|
|
55
|
+
return this._client;
|
|
56
|
+
}
|
|
57
|
+
addFileFromPath(params) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
return yield this.addFile(params);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
addFileFromBuffer(params) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
return yield this.addFile(params);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
addFileFromStream(params) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
return yield this.addFile(params);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.AbstractAdapter = AbstractAdapter;
|
|
74
|
+
//# sourceMappingURL=AbstractAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractAdapter.js","sourceRoot":"","sources":["../../src/AbstractAdapter.ts"],"names":[],"mappings":";;;;;;;;;;;;AAUA,iCAA0C;AAE1C,MAAsB,eAAe;IAMnC,YAAY,MAA8B;QALhC,UAAK,GAAG,kBAAkB,CAAC;QAE3B,iBAAY,GAAkB,IAAI,CAAC;QACnC,YAAO,GAAQ,IAAI,CAAC,CAAC,sBAAsB;QAGnD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC/B,MAAM,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YAChC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;gBACb,yDAAyD;gBACzD,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;YAC/B,CAAC;YACD,IAAI,CAAC,OAAO,GAAG,IAAA,uBAAgB,EAAC,MAAM,CAAC,CAAC;QAC1C,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,OAAO,qBAAQ,MAAM,CAAE,CAAC;QAC/B,CAAC;IACH,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED,2BAA2B;IAC3B,IAAI,aAAa;QACf,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,2BAA2B;IAC3B,gBAAgB;QACd,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAEK,eAAe,CAAC,MAAsB;;YAC1C,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACpC,CAAC;KAAA;IAEK,iBAAiB,CAAC,MAAwB;;YAC9C,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACpC,CAAC;KAAA;IAEK,iBAAiB,CAAC,MAAwB;;YAC9C,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACpC,CAAC;KAAA;CAiDF;AAhHD,0CAgHC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import B2 from "backblaze-b2";
|
|
2
|
+
import { AbstractAdapter } from "./AbstractAdapter";
|
|
3
|
+
import { Options, StreamOptions, StorageType } from "./types/general";
|
|
4
|
+
import { FileBufferParams, FilePathParams, FileStreamParams } from "./types/add_file_params";
|
|
5
|
+
import { ResultObject, ResultObjectBoolean, ResultObjectBuckets, ResultObjectFiles, ResultObjectNumber, ResultObjectStream } from "./types/result";
|
|
6
|
+
import { AdapterConfigBackblazeB2 } from "./types/adapter_backblaze_b2";
|
|
7
|
+
export declare class AdapterBackblazeB2 extends AbstractAdapter {
|
|
8
|
+
protected _type: StorageType;
|
|
9
|
+
protected _config: AdapterConfigBackblazeB2;
|
|
10
|
+
protected _configError: string | null;
|
|
11
|
+
protected _client: B2;
|
|
12
|
+
private authorized;
|
|
13
|
+
private versioning;
|
|
14
|
+
constructor(config: string | AdapterConfigBackblazeB2);
|
|
15
|
+
private authorize;
|
|
16
|
+
private getBuckets;
|
|
17
|
+
private getBucket;
|
|
18
|
+
private getUploadUrl;
|
|
19
|
+
private getFiles;
|
|
20
|
+
private getFile;
|
|
21
|
+
get config(): AdapterConfigBackblazeB2;
|
|
22
|
+
get serviceClient(): B2;
|
|
23
|
+
addFile(params: FilePathParams | FileBufferParams | FileStreamParams): Promise<ResultObject>;
|
|
24
|
+
getFileAsStream(bucketName: string, fileName: string, options?: StreamOptions): Promise<ResultObjectStream>;
|
|
25
|
+
getFileAsURL(bucketName: string, fileName: string): Promise<ResultObject>;
|
|
26
|
+
removeFile(bucketName: string, fileName: string): Promise<ResultObject>;
|
|
27
|
+
createBucket(name: string, options?: Options): Promise<ResultObject>;
|
|
28
|
+
clearBucket(name: string): Promise<ResultObject>;
|
|
29
|
+
deleteBucket(name: string): Promise<ResultObject>;
|
|
30
|
+
listBuckets(): Promise<ResultObjectBuckets>;
|
|
31
|
+
listFiles(bucketName: string, numFiles?: number): Promise<ResultObjectFiles>;
|
|
32
|
+
sizeOf(bucketName: string, fileName: string): Promise<ResultObjectNumber>;
|
|
33
|
+
bucketExists(bucketName: string): Promise<ResultObjectBoolean>;
|
|
34
|
+
fileExists(bucketName: string, fileName: string): Promise<ResultObjectBoolean>;
|
|
35
|
+
}
|