How do I use AWS SDK for JavaScript with Wasabi?

AWS SDK for JavaScript v2 has been certified for use with Wasabi. Wasabi uses this SDK to help power the Wasabi Management Console.

To use the Javascript SDK execute the following steps:

1) Install the AWS SDK for Javascript using npm

2) Configure additional AWS CLI profile for Wasabi account using the Wasabi keys (optional)

In this example, we have set the profile name as "wasabi" in the "~/.aws/credentials" file. To help our customers use this SDK with Wasabi, we have provided examples for both IAM and S3. This example shows: 

  1. How to set the credentials.
  2. Connect to IAM and S3 endpoints
  3. Create a user using IAM
  4. Create a Bucket
  5. Upload and Object to the Bucket
  6. Read an Object from the Bucket
  7. Delete the Object from the Bucket

Other examples can be referred from the AWS documentation.   

Note that this example discusses the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described in this article

 

1. How to set the credentials.

// Load the SDK
const AWS = require('aws-sdk');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = new AWS.SharedIniFileCredentials({profile: 'wasabi'});
AWS.config.credentials = credentials;

// This is a configuration to directly use a profile from aws credentials file.
// AWS.config.credentials.accessKeyId = ""
// AWS.config.credentials.secretAccessKey = ""

// Set the AWS region. us-east-1 is the default for IAM calls.
AWS.config.region = "us-east-1";

 

2. Connect to IAM and S3 endpoints

IAM:

// Load the SDK
const AWS = require('aws-sdk');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = new AWS.SharedIniFileCredentials({profile: 'wasabi'});
AWS.config.credentials = credentials;

// This is a configuration to directly use a profile from aws credentials file.
// AWS.config.credentials.accessKeyId = ""
// AWS.config.credentials.secretAccessKey = ""

// Set the AWS region. us-east-1 is default for IAM calls.
AWS.config.region = "us-east-1";

// Set an endpoint. For IAM us-east-1 or s3.wasabisys.com is what you need.
const ep = new AWS.Endpoint('iam.wasabisys.com');

// Create an IAM client
const iam = new AWS.S3({endpoint: ep});

 

S3:

// Load the SDK
const AWS = require('aws-sdk');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = new AWS.SharedIniFileCredentials({profile: 'wasabi'});
AWS.config.credentials = credentials;

// This is a configuration to directly use a profile from aws credentials file.
// AWS.config.credentials.accessKeyId = ""
// AWS.config.credentials.secretAccessKey = ""

// Set the AWS region.
AWS.config.region = "us-east-1";

// Set an endpoint. 

const ep = new AWS.Endpoint('s3.wasabisys.com');

// Create an S3 client
const s3 = new AWS.S3({endpoint: ep});

 

3. Create a user using IAM

// Load the SDK
const AWS = require('aws-sdk');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = new AWS.SharedIniFileCredentials({profile: 'wasabi'});
AWS.config.credentials = credentials;

// This is a configuration to directly use a profile from aws credentials file.
// AWS.config.credentials.accessKeyId = ""
// AWS.config.credentials.secretAccessKey = ""

// Set the AWS region. us-east-1 is default for IAM calls.
AWS.config.region = "us-east-1";

// Set an endpoint. For IAM us-east-1 or s3.wasabisys.com is what you need.
const ep = new AWS.Endpoint('iam.wasabisys.com');

// Create an IAM client
const iam = new AWS.IAM({endpoint:ep});

// here is an example of creating a user using IAM
const user_params = {
    UserName: "BOB"
};

iam.createUser(user_params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
});

 

4. Create a Bucket

// Load the SDK
const AWS = require('aws-sdk');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = new AWS.SharedIniFileCredentials({profile: 'wasabi'});
AWS.config.credentials = credentials;

// This is a configuration to directly use a profile from aws credentials file.
// AWS.config.credentials.accessKeyId = ""
// AWS.config.credentials.secretAccessKey = ""

// Set the AWS region. us-east-1 is default for IAM calls.
AWS.config.region = "us-east-1";

// Set an endpoint.
const ep = new AWS.Endpoint('s3.wasabisys.com');

// Create an S3 client
const s3 = new AWS.S3({endpoint: ep});

// The following example creates a bucket.
// set the parameters
const bucket_params = {
    Bucket: "bucket-name"
};

// create a bucket with the above parameters.
s3.createBucket(bucket_params, function (err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log(data);           // successful response
});

 

5. Upload an object to the Bucket

// Load the SDK
const AWS = require('aws-sdk');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = new AWS.SharedIniFileCredentials({profile: 'wasabi'});
AWS.config.credentials = credentials;

// This is a configuration to directly use a profile from aws credentials file.
// AWS.config.credentials.accessKeyId = ""
// AWS.config.credentials.secretAccessKey = ""

// Set the AWS region. us-east-1 is default for IAM calls.
AWS.config.region = "us-east-1";

// Set an endpoint.
const ep = new AWS.Endpoint('s3.wasabisys.com');

// Create an S3 client
const s3 = new AWS.S3({endpoint: ep});

// The following example creates an object. If the bucket is versioning enabled, S3 returns version ID in response.
// set key and bucket.
const object_upload_params = {
    Bucket: "bucket-name",
    Key: "file-name"
};

// upload object to previously created "examplebucket"
s3.putObject(object_upload_params, function (err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log(data);           // successful response
});

 

6. Read an object from the Bucket

// Load the SDK
const AWS = require('aws-sdk');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = new AWS.SharedIniFileCredentials({profile: 'wasabi'});
AWS.config.credentials = credentials;

// This is a configuration to directly use a profile from aws credentials file.
// AWS.config.credentials.accessKeyId = ""
// AWS.config.credentials.secretAccessKey = ""

// Set the AWS region. us-east-1 is default for IAM calls.
AWS.config.region = "us-east-1";

// Set an endpoint.
const ep = new AWS.Endpoint('s3.wasabisys.com');

// Create an S3 client
const s3 = new AWS.S3({endpoint: ep});

// The following example retrieves an object for an S3 bucket.
// set the details for the bucket and key
const object_get_params = {
    Bucket: "bucket-name",
    Key: "file-name"
};

// get the object that we just uploaded.
// get the uploaded test_file
s3.getObject(object_get_params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
});

 

7. Delete the object from the Bucket

// Load the SDK
const AWS = require('aws-sdk');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = new AWS.SharedIniFileCredentials({profile: 'wasabi'});
AWS.config.credentials = credentials;

// This is a configuration to directly use a profile from aws credentials file.
// AWS.config.credentials.accessKeyId = ""
// AWS.config.credentials.secretAccessKey = ""

// Set the AWS region. us-east-1 is default for IAM calls.
AWS.config.region = "us-east-1";

// Set an endpoint.
const ep = new AWS.Endpoint('s3.wasabisys.com');

// Create an S3 client
const s3 = new AWS.S3({endpoint: ep});

// The following example deletes an object from a non-versioned bucket.
const delete_params = {
    Bucket: "bucket-name",
    Key: "file-name"
};
s3.deleteObject(delete_params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
});

 

Have more questions? Submit a request