JavaScript SDK v3

In this section we will show you the steps needed to install and use the AWS SDK for JavaScript v3.

The AWS SDK is modulized by clients and commands. To send a request, you only need to import the S3Client and the commands you need.

Installation

To install this package, simply type “add”, or install @aws-sdk/client-s3 using your preferred package manager:

1npm install @aws-sdk/client-s3
2yarn add @aws-sdk/client-s3
3pnpm add @aws-sdk/client-s3

Usage

To send a request:

  • Initiate client with configuration (e.g. credentials).

  • Initiate command with input parameters.

  • Call send operation on client with command object as input.

Tebi Object storage is globally distributed. This means that it does not require to specify separate regions, but a single global region instead. Physical location of your files is controlled by a user-defined Storage Class.

 1import { S3Client } from "@aws-sdk/client-s3";
 2import { ListBucketsCommand, PutObjectCommand } from "@aws-sdk/client-s3";
 3
 4const credentials = {
 5        accessKeyId: "<PUT_YOUR_KEY>",
 6        secretAccessKey: "<PUT_YOUR_SECRET>"
 7};
 8
 9// Create an S3 service client object.
10const s3Client = new S3Client({
11        endpoint: "https://s3.tebi.io",
12        credentials: credentials,
13        region: "global"
14});

Now that you have an S3 Client resource, you can send requests to the service. The following code uses ListBucketsCommand to get and print out all bucket names:

15// List buckets
16const buckets_data = await s3Client.send(
17        new ListBucketsCommand({})
18);
19console.log(buckets_data);

You can also upload and download binary data. For example, the following uploads a new file to S3, assuming that the bucket my-bucket already exists:

20// Upload a file
21const upload_data = await s3Client.send(
22        new PutObjectCommand({
23                Bucket: "my-bucket",
24                Key: "node-js.txt",
25                Body: "some data"
26        })
27);
28console.log(upload_data);

To generate a presigned URL, you must install and use @aws-sdk/s3-request-presigner package:

Install it with your favorite package manager:

1npm install @aws-sdk/s3-request-presigner
2yarn add @aws-sdk/s3-request-presigner
3pnpm add @aws-sdk/s3-request-presigner

This code will generate a signed URL that will replace a Content-Disposition server response header as an example to force the browser to use a different file name. Use s3Client variable from the first code example. Refer to the full @aws-sdk/s3-request-presigner module documentation for more examples and GetObject API function for a full list of supported headers.

 1import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
 2import { GetObjectCommand } from "@aws-sdk/client-s3";
 3
 4// Generate a presigned URL
 5const get_command = new GetObjectCommand({
 6        Bucket: "test",
 7        Key: "node-js.txt",
 8        ResponseContentDisposition: 'attachment; filename="YOUR_FILENAME.txt"'
 9});
10const url = await getSignedUrl(s3Client, get_command, { expiresIn: 3600 });
11console.log(url);

For more examples, please refer to the official AWS SDK v3 documentation examples. Full AWS SDK v3 S3 reference is available here.