Node JS Download File to Amazon AWS s3 Bucket

Node JS Download File to Amazon AWS s3 Bucket

Download files to amazon AWS S3 bucket using Node js + express; Through this tutorial, you will learn how to download file to amazon s3 bucket using node js + express + aws-s3.

How to download file from S3 bucket using node js

Follow the below-given steps to download the file to amazon s3 bucket using node js + express:

  • Step 1 – Create Node Express js App
  • Step 2 – Install express, aws-s3, Multer dependencies
  • Step 3 – Create Server.js File
    • Import Installed Packages
    • Create Route for Download File to AWS S3 using Node.js
  • Step 4 – Start Node Express Js App Server

Step 1 – Create Node Express js App

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

mkdir my-app
cd my-app
npm init -y

Step 2 – Install express, aws-s3 dependencies

Execute the following command on 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 above installed dependencies package in 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 download file to 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 terminal to start node express js server:

//run the below command

npm start

Conclusion

Download files to Amazon AWS S3 bucket using Node js + express; Through this tutorial, you have learned how to download file to amazon s3 bucket using node js + express + aws-s3.

Recommended Node JS Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *