How to use AWS S3 in NodeJS

Mohammad Faisal - May 30 '23 - - Dev Community

To read more articles like this, visit my blog

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
Enter fullscreen mode Exit fullscreen mode

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,
  },
});
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

Get in touch with me via LinkedIn or my Personal Website.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .