How do I use AWS SDK for JavaScript (v3) with Wasabi?

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

The user will need to install the node package based on the Operating System from the link here.

Verification of node installation:

Command: node -v

 

Users can now use a separate package for each service known as Modularized packages. Run the below command to install AWS SDK for JavaScript:

npm install @aws-sdk/client-service

 

Examples to install IAM, S3 and credentials-provider module in AWS SDK for JavaScript (v3) are below and visit for others here:

npm install @aws-sdk/client-iam
npm install @aws-sdk/client-s3
npm install @aws-sdk/credential-provider-ini

Output Screenshots of the installations:

 

 

Output from package.json after upgrading to AWS SDK for JavaScript (v3):

{
"dependencies": {
"@aws-sdk/client-iam": "^3.395.0",
"@aws-sdk/client-s3": "^3.397.0",
"@aws-sdk/credential-provider-ini": "^3.395.0"
}
}
 

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 an existing object to the Bucket

6. Read an object from the Bucket

7. Delete the object from the Bucket

8. Delete the Bucket (Bucket must be empty)

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

Please send all IAM requests to iam.wasabisys.com

 

1. How to set the credentials.

// Load the SDK
const { S3Client } = require("@aws-sdk/client-s3");

const { IAMClient } = require("@aws-sdk/client-iam");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

// Connection
// This is how you can use the .aws credentials file to fetch the credentials
const credentials = fromIni({ profile: "wasabi" });
const iam = new IAMClient({ credentials }); // for IAM
const s3 = new S3Client({ credentials }); // for S3

// Set the AWS region. us-east-1 is the default for IAM calls.
const region = "us-east-1";
const iam = new IAMClient({ region: region }); // for IAM
const s3 = new S3Client({ region: region }); // for S3

 

2. Connect to IAM and S3 endpoints

IAM:

// Load the SDK
const { IAMClient } = require("@aws-sdk/client-iam");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

// Connection
// This is how you can use the .aws credentials file to fetch the credentials, set region, and custom endpoint for IAM
const credentials = fromIni({ profile: "wasabi" });
const region = "us-east-1"; // us-east-1 is the default for IAM calls
const endpoint = "https://iam.wasabisys.com";

// Create an IAM client
const iam = new IAMClient({ credentials, region, endpoint });

S3:

// Load the SDK
const { S3Client } = require("@aws-sdk/client-s3");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

// Connection
// This is how you can use the .aws credentials file to fetch the credentials, set region, and custom endpoint for S3
const credentials = fromIni({ profile: "wasabi" });
const region = "us-east-1"; // replace with your S3 bucket region
const endpoint = "https://s3.us-east-1.wasabisys.com"; // replace with your S3 bucket service URL

// Create an S3 client
const s3 = new S3Client({ credentials, region, endpoint });

 

3. Create a user using IAM

const { IAMClient, CreateUserCommand } = require("@aws-sdk/client-iam");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

// Connection
// This is how you can use the .aws credentials file to fetch the credentials, set region, and custom endpoint for IAM
const credentials = fromIni({ profile: "wasabi" });
const region = "us-east-1"; // us-east-1 is the default for IAM calls
const endpoint = "https://iam.wasabisys.com";

// Create an IAM client
const iam = new IAMClient({ credentials, region, endpoint });

const params = {
UserName: "NewUser" //replace with your IAM username here
};

const createUserCommand = new CreateUserCommand(params);

iam.send(createUserCommand)
.then((result) => {
console.log("User created:", result.User);
})
.catch((error) => {
console.error("An error occurred:", error);
});

 

 

4. Create a Bucket

const { S3Client, CreateBucketCommand } = require("@aws-sdk/client-s3");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

// Connection
// This is how you can use the .aws credentials file to fetch the credentials, set region, and custom endpoint for IAM
const credentials = fromIni({ profile: "default" });
const region = "us-east-1"; // us-east-1 is the default for IAM calls
const endpoint = "https://s3.us-east-1.wasabisys.com";

// Set up the client with the custom endpoint
const s3 = new S3Client({ credentials, region, endpoint });
const BUCKET_NAME = "custom-bucket-name-here"; // Replace with your bucket name

const createBucket = async () => {
try {
const params= {
Bucket: BUCKET_NAME,
};

const data = await s3.send(new CreateBucketCommand(params));
console.log("Success, bucket deleted!", data);
} catch (err) {
console.error("Error", err);
}
};

createBucket();

 

5. Upload an existing object to the Bucket

const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const { fromIni } = require("@aws-sdk/credential-provider-ini");
const fs = require('fs');

// Connection
// This is how you can use the .aws credentials file to fetch the credentials, set region, and custom endpoint for S3
const credentials = fromIni({ profile: "wasabi" });
const region = "us-east-1"; // replace with your S3 bucket region
const endpoint = "https://s3.us-east-1.wasabisys.com"; // replace with your S3 bucket service URL

// Set up the client with the custom endpoint
const s3 = new S3Client({ credentials, region, endpoint });

const BUCKET_NAME = "custom-bucket-name-here"; // Replace with your bucket name
const OBJECT_KEY = "file.png"; // Replace with your object key
const FILE_PATH = "path_to_file.png"; // Replace with the path to your file

const uploadObject = async () => {
try {
const fileStream=fs.createReadStream(FILE_PATH);
const params= {
Bucket: BUCKET_NAME,
Key: OBJECT_KEY,
Body: fileStream,
};

// Upload the object
const data = await s3.send(new PutObjectCommand(params));
console.log("Success, object uploaded", data);
} catch (err) {
console.error("Error", err);
}
};

uploadObject();

 

6. Read an object from the Bucket

const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

// Connection
// This is how you can use the .aws credentials file to fetch the credentials, set region, and custom endpoint for S3
const credentials = fromIni({ profile: "wasabi" });
const region = "us-east-1"; // replace with your S3 bucket region
const endpoint = "https://s3.us-east-1.wasabisys.com"; // replace with your S3 bucket service URL

// Set up the client with the custom endpoint
const s3 = new S3Client({ credentials, region, endpoint });

const BUCKET_NAME = "custom-bucket-name-here"; // Replace with your bucket name
const OBJECT_KEY = "custom-object-key-name-here"; // Replace with your object key

// Read the object
const readObject = async () => {
try {
const params= {
Bucket: BUCKET_NAME,
Key: OBJECT_KEY
};

const data = await s3.send(new GetObjectCommand(params));
const body = await streamToString(data.Body);

console.log("Success, object retrieved:", body);
} catch (err) {
console.error("Error", err);
}
};

// Function to convert a stream to string
const streamToString = (stream) => {
return new Promise((resolve, reject) => {
const chunks= [];
stream.on('data', (chunk) =>chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () =>resolve(Buffer.concat(chunks).toString('utf8')));
});
};

readObject();

 

7. Delete the object from the Bucket

const { S3Client, DeleteObjectCommand } = require("@aws-sdk/client-s3");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

// Connection
// This is how you can use the .aws credentials file to fetch the credentials, set region, and custom endpoint for S3
const credentials = fromIni({ profile: "wasabi" });
const region = "us-east-1"; // replace with your S3 bucket region
const endpoint = "https://s3.us-east-1.wasabisys.com"; // replace with your S3 bucket service URL

// Set up the client with the custom endpoint
const s3 = new S3Client({ credentials, region, endpoint });
const BUCKET_NAME = "custom-bucket-name-here"; // Replace with your bucket name
const OBJECT_KEY = "custom-object-key-name-here.txt"; // Replace with your object key

const deleteObject = async () => {
try {
const params= {
Bucket: BUCKET_NAME,
Key: OBJECT_KEY
};

const data = await s3.send(new DeleteObjectCommand(params));
console.log("Success, object deleted", data);
} catch (err) {
console.error("Error", err);
}
};

deleteObject();

 

8. Delete the Bucket (Bucket must be empty)

const { S3Client, DeleteBucketCommand } = require("@aws-sdk/client-s3");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

// Connection
// This is how you can use the .aws credentials file to fetch the credentials, set region, and custom endpoint for S3
const credentials = fromIni({ profile: "wasabi" });
const region = "us-east-1"; // replace with your S3 bucket region
const endpoint = "https://s3.us-east-1.wasabisys.com"; // replace with your S3 bucket service URL

// Set up the client with the custom endpoint
const s3 = new S3Client({ credentials, region, endpoint });
const BUCKET_NAME = "custom-bucket-name-here"; // Replace with your bucket name

const deleteBucket = async () => {
try {
const params= {
Bucket: BUCKET_NAME,
};

const data = await s3.send(new DeleteBucketCommand(params));
console.log("Success, bucket deleted!", data);
} catch (err) {
console.error("Error", err);
}
};

deleteBucket();

 

Have more questions? Submit a request