Files Download from AWS S3 using Node.js and the AWS SDK

Downloading files from Amazon Simple Storage Service (AWS S3) using Node.js is a common task for developers working with cloud storage. In this comprehensive guide, we’ll explore step-by-step instructions and code examples to effortlessly download files from AWS S3 using Node.js.


Before you start, make sure you have the following:

  1. AWS Account: Ensure that you have an AWS account with access to S3 buckets.
  2. Node.js and npm: Install the latest version of Node.js and npm on your machine.

Step 1: Set Up AWS SDK

Start by installing the AWS SDK for Node.js using npm:

npm install aws-sdk

Step 2: Configure AWS SDK

Create an AWS SDK configuration file with your AWS credentials. You can do this by creating a file named credentials in the ~/.aws/ directory on your machine. Add your AWS access key ID and secret access key:

aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

Step 3: Write Node.js Code to Download Files

Now, you can write Node.js code to download files from an S3 bucket. Create a file (e.g., downloadFromS3.js) and use the following code:

const AWS = require('aws-sdk');
const fs = require('fs');

// Configure AWS SDK with your region
AWS.config.update({ region: 'your-aws-region' });

// Create an S3 object
const s3 = new AWS.S3();

// Specify the S3 bucket and object key
const bucketName = 'your-s3-bucket-name';
const objectKey = 'path/to/your/file.txt';
const localFilePath = 'local/file.txt';

// Create a writable stream to save the downloaded file locally
const fileStream = fs.createWriteStream(localFilePath);

// Set up the parameters for the S3 getObject operation
const getObjectParams = {
  Bucket: bucketName,
  Key: objectKey,

// Perform the S3 getObject operation and pipe the data to the writable stream
  .on('error', (err) => {
    console.error('Error downloading file:', err);
  .on('close', () => {
    console.log('File downloaded successfully!');

Replace the placeholder values with your AWS region, S3 bucket name, object key, and the local file path where you want to save the downloaded file.

Step 4: Run the Node.js Script

Save the changes to your Node.js script and run it using the following command:

node downloadFromS3.js

This script will download the specified file from the AWS S3 bucket and save it locally.

You’ve successfully learned how to download files from AWS S3 using Node.js. This comprehensive guide covered setting up the AWS SDK, configuring AWS credentials, and writing Node.js code to perform the S3 getObject operation.

Integrate this knowledge into your applications to seamlessly download files from AWS S3 and enhance your cloud storage capabilities.

Related Posts