This is an interface into the BackBlaze B2 API with a series of helper functions that will allow you to shortcut some calls.
npm install node-backblaze-b2
const backblaze = require("node-backblaze-b2");
let b2 = new backblaze({ accountId: "string", applicationKey: "string" });
Must be done first before anything else. This will return an Authorization token. The token is also stored within the application so you don't need to explicitly do anything with it going forward.
b2.authorize((err, data) => {});
Return a list of buckets associated with the account. You can optionally pass in a string which will only return that specific bucket's object.
List all buckets
b2.listBuckets((err, data) => {});
List individual bucket
b2.listBuckets("my-bucket", (err, data) => {});
Creates a new bucket and returns the associated bucket object.
let input = {
bucketName: "string",
bucketType: "allPublic" // Also accepts allPrivate.
};
b2.createBucket(input, (err, data) => {});
Returns an array with file names that exist in a bucket. This has a maximum of 1000 items returned, and if nextFileName exist within the response, you will need to re-request with the startFileName filter to get the next set of data.
let input = {
bucketId: "string",
startFileName: "string", // Optional
maxFileCount: int, // Optional
depth: int // Folder depth in which to scan
};
b2.listFileNames(input, (err, data) => {});
Returns an array with file versions that exist in a bucket. This has a maximum of 1000 items returned, and if nextFileName/nextFileId exist within the response, you will need to re-request with the startFileName/startFileId filter to get the next set of data.
let input = {
bucketId: "string",
startFileName: "string", // Optional
startFileId: "string", // Optional
maxFileCount: int // Optional
strict: bool, // Optional. When used with startFileName, it will only return results with the exact filename.
};
b2.listFileVersions(input, (err, data) => {});
Return an object of file details. It needs either fileId, or fileName to be passed. If fileName is passed in, it will automatically find the fileId of the latest version of the file, as returned in b2.listFileVersions.
let input = { fileId: "string" }; // Get file info by ID (1 API call)
let input = { fileName: "string", bucketId: "string" }; // Get file info by Name and Bucket ID (2 API calls)
let input = { fileName: "string", bucketName: "string" }; // Get file info by Name and Bucket Name (3 API calls)
b2.getFileInfo(input, (err, data) => {});
Delete a file. This requires both the fileName and fileId to be passed into it. It will only delete the fileId specified, so if there are multiple versions of the file, you'll need to delete each one.
let input = { fileId: "string", fileName: "string" };
b2.deleteFile(input, (err) => {});
Return an upload url object. Use this is you plan to upload a file manually, if you use the b2.uploadFile function, then this is called on your behalf.
let input = {
bucketId: "string"
};
b2.getUploadUrl(input, (err, data) => {});
Upload a local file to B2, and return the file objects. This will perform all necessary helper data on your behalf. The ability to add header data will come at a future date.
let input = {
bucketId: "string",
file: "/path/to/file.jpg",
fileName: "filename/on/b2.jpg",
contentType: "image/jpeg", // Optional, mime type to use.
retryAttempts: 3 // Optional, how many attempts at an upload. This compensates for the B2 503 on upload.
};
b2.uploadFile(input, (err, data) => {});
Retrieve an authorization token that will allow you to download a file directly from B2.
let input = {
bucketId: "string",
fileNamePrefix: "/path", // This would allow /path.jpg, /path/file.jpg, etc.
duration: int // Seconds that this token should remain valid.
};
b2.getAuthToken(input, (err, data) => {});
Retrieve a file from B2 and save it to local disk. If the file already exists, it will attempt to truncate the file and write it again. Returns the finished http.ClientRequest object.
let input = {
bucketName: "string",
fileName: "filename/on/b2.jpg",
file: "/path/to/file.jpg",
verify: false // Optional, Verify sha1 matches the one sent by b2.
};
b2.downloadFile(input, (err, data) => {});
Retrieve an https.ClientRequest object for minipulation. This allows to modify and object retrieved from B2 without needing to write it to disk first.
let input = {
bucketName: "string",
fileName: "string"
};
var stream = b2.getFileStream(input, (resp) => {
// Retrieve data in chunks
resp.on("data", (chunk) => {
console.log(chunk); // Buffer.
});
// Write file to disk.
resp.pipe(fs.createWriteStream("./myData.ext", { flags: "w+" }));
});
// Error downloading object.
stream.on("error", (err) => {
console.log(err);
});
If any API calls return an error, the callback will return a modified Error Object. The following data is an example of an unable to authenticate error. All errors follow this standard form, with the exception of err.status = 0 represents a local application error, or inability to contact B2 API.
let err = {
api: { // This was left in here to allow BackBlaze to update their error response object in the future.
code: 'bad_auth_token',
message: 'Invalid authorization token',
status: 401
},
code: 'bad_auth_token',
status: 401,
message: 'Invalid authorization token',
requestObject: { [http.clientRequest() object] }
};
Please report bugs and feature requests to Github.