How to use AWS S3 in NodeJS

AWS S3 is one of the most popular cloud storage services. It is a great place to store your data and make it available to your users. It is also a great place to store your backups. This guide will show you how to interact with AWS S3 in NodeJS.

What will we learn?

today we will learn how to

  • Upload a file to S3
  • Upload a file to S3 with the progress
  • Download a file from S3
  • Download a file from S3 and store it locally.
  • Delete a file from S3
  • List all files in a bucket

Pre-requisites

To follow along, you should have a basic understanding of NodeJS and AWS. You should also have an AWS account and have created an S3 bucket.

Let's get started!

Setup and Initialization

First, install the dependency.

yarn add @aws-sdk/client-s3

Then initialize the aws client.

import { S3Client } from '@aws-sdk/client-s3';

const s3 = new S3Client({
  region: 'us-east-1',
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  },
});

We will use this client for all of our subsequent methods.

Upload files

Then you can use the client to upload files

import { PutObjectCommand } from '@aws-sdk/client-s3';

const params = {
  Bucket: 'my-bucket',
  Key: 'my-key',
  Body: 'my-body',
};

const command = new PutObjectCommand(params);

const data = await s3.send(command);

Uploading files with progress

To upload files with the progress, you can use the upload command. But to do that, we need another library, @aws-sdk/lib-storage. If provides an Upload class that we can use to upload files.

yarn add @aws-sdk/lib-storage

Then we can use the Upload class to upload files with progress.

import { Upload } from '@aws-sdk/lib-storage';

const params = {
  Bucket: 'my-bucket',
  Key: 'my-key',
  Body: 'my-body',
};

const upload = new Upload({
  client: s3,
  params,
});

upload.on('httpUploadProgress', (progress) => {
  console.log(progress);
});

const data = await upload.done();

Downloading files

To download files, you can use the getObject command.

import { GetObjectCommand } from '@aws-sdk/client-s3';

const params = {
  Bucket: 'my-bucket',
  Key: 'my-key',
};

const command = new GetObjectCommand(params);

const data = await s3.send(command);

Downloading files and saving them locally

To download files and save them locally, you can use the getObject command.

import { GetObjectCommand } from '@aws-sdk/client-s3';
import { createWriteStream } from 'fs';

const params = {
  Bucket: 'my-bucket',
  Key: 'my-key',
};

const command = new GetObjectCommand(params);

const data = await s3.send(command);

const file = fs.createWriteStream('my-file.txt');

data.Body.pipe(file);

Downloading files with progress

To download files with the progress, you can use the download command.

import { Download } from '@aws-sdk/lib-storage';

const params = {
  Bucket: 'my-bucket',
  Key: 'my-key',
};

const download = new Download({
  client: s3,
  params,
});

const data = await download.done();

Deleting files

To delete files, you can use the deleteObject command.

import { DeleteObjectCommand } from '@aws-sdk/client-s3';

const params = {
  Bucket: 'my-bucket',
  Key: 'my-key',
};

const command = new DeleteObjectCommand(params);

const data = await s3.send(command);

Listing files

To list files, you can use the listObjects command.

import { ListObjectsCommand } from '@aws-sdk/client-s3';

const params = {
  Bucket: 'my-bucket',
};

const command = new ListObjectsCommand(params);

const data = await s3.send(command);

that's it for today. Hope you learned something new!

Profile Image

Who I am

Hi, I amMohammad Faisal, A full-stack software engineer @Cruise , working remotely from a small but beautiful country named Bangladesh.

I am most experienced inReactJS,NodeJS andAWS

Buy Me a Coffee Widget