How to use the AWS SDK for JavaScript with B2 How to use the AWS SDK for JavaScript with B2

How to use the AWS SDK for JavaScript with B2

Pat Patterson Pat Patterson


B2 can easily be configured for use with the AWS SDK for JavaScript thanks to the S3 Compatible API. Below is an example of a sample.js that has been configured to set credentials, create a bucket, and upload a file:

var AWS = require('aws-sdk');
var uuid = require('node-uuid');

// Load credentials from a local JSON file

// Create an S3 client
// You must copy the endpoint from your B2 bucket details
// and set the region to match.
var s3 = new AWS.S3({
endpoint: '',
region: 'us-west-004'

// Create a bucket and upload something into it
var bucketName = 'node-sdk-sample-' + uuid.v4();
var keyName = 'hello_world.txt';

s3.createBucket({Bucket: bucketName}, function() {
var params = {Bucket: bucketName, Key: keyName, Body: 'Hello World!'};
s3.putObject(params, function(err, data) {
if (err)
console.log("Successfully uploaded data to " + bucketName + "/" + keyName);

Content of config.json:

"accessKeyId": "your_b2_keyid",
"secretAccessKey": "your_appKey"


Alternatively, you can add credentials to ~/.aws/credentials as a separate profile:

aws_access_key_id = your_b2_keyId
aws_secret_access_key = your_b2_appKey

In this case, remove the following line in the script above:



Then run the script as follows.

b2@vm:~/aws-nodejs-sample$ AWS_PROFILE=b2 node sample.js
Successfully uploaded data to node-sdk-sample-38cb5413-29c2-46f5-ab2d-4ac4d553f929/hello_world.txt

The S3 Compatible API for Backblaze B2 Cloud Storage allows 1000’s of integrations to work with B2 natively. If you’re new to the S3 Compatible API, please see our Getting Started Guide. If you have any trouble using AWS SDK for JavaScript with B2, please let us know by emailing us at