AWS SDK for JavaScript With Wasabi
    • 12 Aug 2024
    • 5 Minutes to read
    • PDF

    AWS SDK for JavaScript With Wasabi

    • PDF

    Article summary

    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 an 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. For rther examples, refer to the AWS documentation.  

    This example discusses the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, use the appropriate Wasabi service URL as described in Service URLs for Wasabi's Storage Regions.

    Send all IAM requests to iam.wasabisys.com.

    Setting 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";

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

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

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

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

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

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