Skip to main content

Connect AWS CLI, S3cmd, and AWS JavaScript SDK

AWS CLI, S3cmd, and the AWS JavaScript SDK all use the same credentials to connect to CDB Object Storage: an access key, a secret key, and an endpoint URL. The setup format differs by tool — select a tab for the specific steps.

AWS CLI integrates with CDB Object Storage for bucket management using commands from the Amazon documentation.

Installation

  1. 1

    Install AWS CLI

    Follow the installation guide to install the latest version.

  2. 2

    Verify installation

    Run the following command to confirm the installation succeeded:

    aws --version
    

    The output shows the installed version and operating system:

    aws-cli/2.7.24 Python/3.8.8 Windows/10 exe/AMD64 prompt/off
    

    If the terminal cannot find the aws command, consult the troubleshooting guide.

Configuration

  1. 1

    Launch the configuration wizard

    Run aws configure.

  2. 2

    Enter storage credentials

    Provide the following values:

    • Access Key: the access key from the storage Details dialog.
    • Secret Key: the secret key from the storage Details dialog.
    • Default region name: the region name for the storage location — s-ed1 for Luxembourg-2.
    • Leave all other parameters at their default values.
  3. 3

    Verify the connection

    Run the following command, replacing the endpoint with the actual value from the service URLs reference:

    aws s3 ls --endpoint-url=https://s-ed1.cloud.gcore.lu
    

    Correct credentials produce no errors and list any existing buckets. An empty output with exit code 0 is expected for new storage with no buckets.

S3cmd is a free command-line tool for uploading, retrieving, and managing objects in S3-compatible storage. The full command reference is in the S3cmd documentation.

Installation

  1. 1

    Download S3cmd

    Go to the Download page in the S3cmd documentation.

  2. 2

    Install the package

    Download and install the package for the operating system.

Configuration

Two methods are available: the interactive wizard and direct command-line input for automated environments.

The code examples use AWS SDK for JavaScript v2 (aws-sdk), which is in maintenance mode but remains functional. The migration guide covers the path to SDK v3 (@aws-sdk/client-s3), which AWS recommends for new projects.

SDK setup

Add the AWS SDK script to the HTML page:

<html>
    <head>
        <script src="https://sdk.amazonaws.com/js/aws-sdk-2.742.0.min.js"></script>
        <script src="./js/index2.js"></script>
    </head>
    <body>
        <h1>List of files</h1>
        <ul id="list">
        </ul>
    </body>
</html>

Storage operations

Credentials and bucket name

Open the configuration file (./js/index2.js) and specify the storage credentials and bucket name:

var s3BucketName = "test";
var host = "https://s-ed1.cloud.gcore.lu";
var access_key = "1234";
var secret_key = "5678";

AWS.config.accessKeyId = access_key;
AWS.config.secretAccessKey = secret_key;
AWS.config.endpoint = host;

var s3 = new AWS.S3({
    sslEnabled: true
});

Where:

  • test is the bucket name.
  • https://s-ed1.cloud.gcore.lu is the storage endpoint.
  • 1234 is the access key from the Customer Portal.
  • 5678 is the secret key from the Customer Portal.

CORS policy

A wildcard CORS policy allows cross-origin GET, HEAD, PUT, POST, and DELETE requests from all sources:

This configuration is permissive and may be insecure for production environments.
<CORSConfiguration>
<CORSRule>
    <ID>Allow everything</ID>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
    <MaxAgeSeconds>30</MaxAgeSeconds>
</CORSRule>
</CORSConfiguration>

Apply the policy to the bucket:

s3cmd setcors cors.xml s3://<bucket_name>

Add objects to a bucket

The following example adds two objects (test_file1 and test_file2) to the s3test bucket:

var params1 = {
   Bucket: "s3test", Key: "test_file1",
   Body: "test"
};
var params2 = {
   Bucket: "s3test", Key: "test_file2",
   Body: "test"
};
var request = s3.putObject(params1);
request.send(function (err, data) {
   if (err) console.log("Error:", err.code, err.message);
   else console.log(data);
});
var request = s3.putObject(params2);
request.send(function (err, data) {
   if (err) console.log("Error:", err.code, err.message);
   else console.log(data);
});

List objects in a bucket

The following example retrieves all objects in the test_2 bucket:

params = {
    Bucket: "test_2"
};
s3.listObjects(params, function(err, data) {
    if (err) return;
    data.Contents.map(function(info) {
        var ul = document.getElementById("list");
        var li = document.createElement("li");
        li.innerText = info.Key + " " + info.LastModified;
        ul.append(li);
    });
});