Working with S3 and Cloud Storage in JavaScript (Node.js) — @aws-sdk/client-s3 Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

@aws-sdk/client-s3 Library for Working with S3 and Cloud Storage in Node.js



Cloud storage has become an integral part of modern web applications. It allows storing and transferring files of any size, ensuring high availability and scalability. In the JavaScript (Node.js) ecosystem, one of the most popular libraries for working with S3-compatible storage is @aws-sdk/client-s3 — the official third-generation AWS SDK.



This library provides a full set of methods for interacting with Amazon S3, as well as with any S3-compatible object storage (e.g., MinIO, DigitalOcean Spaces, Yandex Object Storage, VK Cloud). In this article, we will cover installation, configuration, and basic operations: uploading, downloading, deleting, and listing objects.



Installation and Import



To get started, install the package via npm:



npm install @aws-sdk/client-s3


After installation, import the necessary classes into your project:



const { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand, ListObjectsV2Command } = require('@aws-sdk/client-s3');


Configuring the S3 Client



To connect to S3, you need to create a client instance, specifying the region, access keys, and, if necessary, the endpoint (for third-party storage).



const s3Client = new S3Client({  region: 'ru-central1', // your storage region  endpoint: 'https://storage.yandexcloud.net', // for Yandex Object Storage  credentials: {    accessKeyId: 'YOUR_ACCESS_KEY',    secretAccessKey: 'YOUR_SECRET_KEY'  }});


If you are using Amazon S3 directly, you do not need to specify the endpoint — the SDK will determine it automatically based on the region. For local testing with MinIO, specify endpoint: 'http://localhost:9000' and forcePathStyle: true.



Basic S3 Operations



1. Uploading a File (PutObject)



To upload a file to a bucket, use the PutObjectCommand. Data can be passed as a string, buffer, or stream.



const uploadFile = async (bucketName, key, body) => {  try {    const command = new PutObjectCommand({      Bucket: bucketName,      Key: key,      Body: body,      ContentType: 'text/plain' // optional    });    const response = await s3Client.send(command);    console.log('File uploaded:', response);  } catch (err) {    console.error('Upload error:', err);  }};

// Usage exampleuploadFile('my-bucket', 'hello.txt', 'Hello, world!');


2. Downloading a File (GetObject)



To retrieve an object from storage, use GetObjectCommand. The data is returned as a stream, which can be converted to a string or buffer.



const downloadFile = async (bucketName, key) => {  try {    const command = new GetObjectCommand({      Bucket: bucketName,      Key: key    });    const response = await s3Client.send(command);    const str = await response.Body.transformToString('utf-8');    console.log('File content:', str);    return str;  } catch (err) {    console.error('Download error:', err);  }};

// Usage exampledownloadFile('my-bucket', 'hello.txt');


3. Deleting a File (DeleteObject)



Deleting an object is performed using the DeleteObjectCommand.



const deleteFile = async (bucketName, key) => {  try {    const command = new DeleteObjectCommand({      Bucket: bucketName,      Key: key    });    await s3Client.send(command);    console.log('File deleted');  } catch (err) {    console.error('Delete error:', err);  }};

deleteFile('my-bucket', 'hello.txt');


4. Listing Objects (ListObjectsV2)



To list all objects in a bucket, use

Recommendations