npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-backblaze-b2

v1.0.7-c

Published

Interface for BackBlaze B2 object storage.

Downloads

23

Readme

Interface to BackBlaze B2

This is an interface into the BackBlaze B2 API with a series of helper functions that will allow you to shortcut some calls.

Install

npm install node-backblaze-b2

Setup

const backblaze = require("node-backblaze-b2");
let b2 = new backblaze({ accountId: "string", applicationKey: "string" });

Authorize (b2_authorize_account)

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) => {});

List Buckets (b2_list_buckets)

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) => {});

Create Bucket (b2_create_bucket)

Creates a new bucket and returns the associated bucket object.

let input = {
  bucketName: "string",
  bucketType: "allPublic" // Also accepts allPrivate.
};
b2.createBucket(input, (err, data) => {});

List File Names in Bucket (b2_list_file_names)

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) => {});

List File Versions in Bucket (b2_list_file_versions)

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) => {});

Get File Info (b2_get_file_info)

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 File (b2_delete_file_version)

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) => {});

Get Upload URL (b2_get_upload_url)

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 file to B2 (b2_upload_file)

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) => {});

Get Authorization Token (b2_get_download_authorization)

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) => {});

Save File Locally (b2_download_file_by_name)

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 File as http(s).ClientRequest Object (b2_download_file_by_name)

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);
});

Error Object

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.