AWS SDK for JavaScript
The AWS SDK for JavaScript
Getting started
- Create a service account.
- Assign the service account the roles required for your project. For more information about roles, see the Identity and Access Management documentation.
- Create a static access key.
Note
A service account is only allowed to view a list of buckets in the folder it was created in.
A service account can perform actions with objects in buckets that are created in folders different from the service account folder. To enable this, assign the service account roles for the appropriate folder or its bucket.
Installation
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 the credentials 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 settings and copy the following information to it:[default] region=ru-central1 endpoint_url=https://storage.yandexcloud.net
Note
Some apps designed to work with Amazon S3 do not allow you to specify the region; this is why Object Storage may also accept the
us-east-1
value.
To access Object Storage, use the https://storage.yandexcloud.net
endpoint.
Code snippets
You can find an example of bucket creation code in the javascriptv3/example_code/nodegetstarted/src
directory of the repository.
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"; // Installing Object Storage region const REGION = "ru-central1"; // Installing Object Storage endpoint const ENDPOINT = "https://storage.yandexcloud.net"; // 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 AWS SDK clients and commands to work with Node.js import { PutObjectCommand, CreateBucketCommand } from "@aws-sdk/client-s3"; import { s3Client } from "./libs/s3Client.js"; // Installing parameters const params = { Bucket: "<bucket_name>", // Bucket name, e.g., 'sample-bucket-101'. Key: "<bucket_name>", // Object name, e.g., 'sample_upload.txt'. Body: "<object_contents>", // Object contents, e.g., '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 unit testing. } catch (err) { console.log("Error", err); } // Creating an object and uploading it into a 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 unit testing. } catch (err) { console.log("Error", err); } }; run();
-
Under
const params
, specify 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