AWS SDK for JavaScript
The AWS SDK for JavaScript
Getting started
- Create a service account.
- Add the service account to a group to grant it necessary permissions.
- Create a static access key.
Installing
To install the AWS SDK for JavaScript:
-
Install
Node.js. -
Clone the AWS SDK Code Examples
repository. Run the following command in the terminal window:git clone https://github.com/awsdocs/aws-doc-sdk-examples
Setup
-
Go to the
~/.aws/
directory (for macOS and Linux) orC:\Users\<username>\.aws\
(for Windows). -
Create a file named
credentials
with authentication data for Object Storage and copy the following information to it:[default] aws_access_key_id = <static_key_ID> aws_secret_access_key = <secret_key>
-
Create a file named
config
with the default region parameters and copy the following information to it:[default] region=eu-north1
Note
Some apps designed to work with Amazon S3 don't let you set the region, so Object Storage also accepts the value
us-east-1
.
To access Object Storage, use the https://storage.ai.nebius.cloud
endpoint.
Code samples
You can find a sample bucket creation code in the repository's javascriptv3/example_code/nodegetstarted/src
directory.
To connect to Object Storage and create a bucket:
-
Install dependencies as described in the
javascriptv3/example_code/nodegetstarted/package.json
file:npm install node -g cd aws-doc-sdk-examples/javascriptv3/example_code/nodegetstarted npm install
-
In the
javascriptv3/example_code/nodegetstarted/src/libs/
directory, open thes3Client.js
file with a description of the Object Storage client. -
Replace the file contents with the code below:
import { S3Client } from "@aws-sdk/client-s3"; // Setting the region Object Storage const REGION = "eu-north1"; // Setting the endpoint Object Storage const ENDPOINT = "https://storage.ai.nebius.cloud"; // Creating a client for Object Storage const s3Client = new S3Client({ region: REGION, endpoint: ENDPOINT }); export { s3Client };
-
In the
javascriptv3/example_code/nodegetstarted/src/
directory, open thesample.js
file with the code for creating a bucket and an object in it.sample.js contents// Importing clients and AWS SDK commands to work with Node.js import { PutObjectCommand, CreateBucketCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js"; // Setting parameters const params = { Bucket: "<bucket_name>", // Bucket name, such as 'sample-bucket-101'. Key: "<object_name>", // Object name, such as 'sample_upload.txt'. Body: "<object_contents>", // Object contents, such as 'Hello world!". }; const run = async () => { // Creating a bucket try { const data = await s3Client.send( new CreateBucketCommand({ Bucket: params.Bucket }) ); console.log(data); console.log("Successfully created a bucket called ", data.Location); return data; // For modular testing. } catch (err) { console.log("Error", err); } // Creating an object and uploading it to the bucket try { const results = await s3Client.send(new PutObjectCommand(params)); console.log( "Successfully created " + params.Key + " and uploaded it to " + params.Bucket + "/" + params.Key ); return results; // For modular testing. } catch (err) { console.log("Error", err); } }; run();
-
Under
const params
, set a name for the bucket and a name and contents for the bucket object. -
Run the application:
node sample.js
To learn more about using the AWS SDK for JavaScript, see the AWS documentation