How to Download File to Amazon AWS s3 Bucket in Node JS?

Nov 14, 2022

.

Admin

How to Download File to Amazon AWS s3 Bucket in Node JS?

Hello Friends,

This post will give you an example of downloading the file to an amazon AWS s3 bucket in node js. In this article, we will learn how to download an amazon AWS bucket file. We will look at examples of nodejs and how do I download a file to disk from an AWS s3 bucket. this example will help you how to download an array of files from AWS s3 using AWS SDK in nodejs.

Download files to amazon AWS S3 bucket using Node js + express; this tutorial will teach you how to download files to amazon s3 bucket using node js + express + AWS-s3.

Step 1: Create Node Express js App


Execute the following command on the terminal to create the node js app:

mkdir my-app

cd my-app

npm init -y

Step 2: Install express, aws-s3 dependencies

Execute the following command on the terminal to install express, AWS-s3 dependencies:

npm install express aws-sdk --save

Express — Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

AWS-SDK — AWS s3 npm is used to upload or delete an image from the s3 bucket with the help of some keys.

Step 3: Create Server.js File

Create server.js file; so visit your app root directory and create a new file name server.js.

Then follow the below steps:

->Import Installed Packages

->Create Route for Download File to AWS S3 using Node.js

Import Installed Packages

Import the above-installed dependencies package in the server.js file:

var aws = require('aws-sdk')

var express = require('express')

Create Route for Download File to AWS S3 using Node.js

The following node js rest API route will be a download file to the amazon s3 bucket:

app.get('/download-file', function(req, res, next){

// download the file via aws s3 here

var fileKey = req.query['fileKey'];

console.log('Trying to download file', fileKey);

AWS.config.update(

{

accessKeyId: "....",

secretAccessKey: "...",

region: 'ap-southeast-1'

}

);

var s3 = new AWS.S3();

var options = {

Bucket : '/bucket-url',

Key : fileKey,

};

res.attachment(fileKey);

var fileStream = s3.getObject(options).createReadStream();

fileStream.pipe(res);

});

Open your server js file and add the following code into it:

var express = require('express');

var app = express();

var AWS = require('aws-sdk');

var fs = require('fs');

app.get('/download-file', function(req, res, next){

// download the file via aws s3 here

var fileKey = req.query['fileKey'];

console.log('Trying to download file', fileKey);

AWS.config.update(

{

accessKeyId: "....",

secretAccessKey: "...",

region: 'ap-southeast-1'

}

);

var s3 = new AWS.S3();

var options = {

Bucket : '/bucket-url',

Key : fileKey,

};

res.attachment(fileKey);

var fileStream = s3.getObject(options).createReadStream();

fileStream.pipe(res);

});

app.listen(3000, function () {

console.log('express is online');

})

Step 4: Start Node Express Js App Server

Execute the following command on the terminal to start the node express js server:

//run the below command

npm start

I hope it can help you...

#Node JS