AWS S3 Bucket Files Download using Multer and Express in NodeJS

By the end of this tutorial, you will be able to download files from the AWS S3 bucket using Express in NodeJS.

How to use Amazon AWS S3 Bucket to Download Files?

By the end of this tutorial, you will be able to download files from the AWS S3 bucket using Express in NodeJS.

Download AWS S3 Bucket Files in NodeJS Express Server

To make it short, you can follow the steps given below to download as per the title of the tutorial. Step 1: Create a NodeJS App Step 2: Installation of express, aws-s3, and multer NPM packages or dependencies Step 3: Create a server.js file and import the installed packages from Step 2. Step 4: Creating a controller for the route file Step 5: Creating a route to download files (REST API) Step 6: Start Node Express Js App Server

AWS-S3-Bucket-Multer-NodeJS-Express-Server Step 1: Create a NodeJS App

Run the following command on your terminal for a new dir, enter it and create a new app.
mkdir rabins-node-s3
cd rabins-node-s3
npm init -y

Step 2: Installation of express, aws-s3, and multer NPM packages or dependencies

Run the following command on your terminal to install express and aws-s3 dependencies. Express is a web application framework for NodeJS and AWS SDK is an NPM package that helps handle images in S3 buckets using some key values.
npm install express aws-sdk --save

Step 3: Create a server.js file and import the installed packages from Step 2.

Run the following command on the terminal to create a new server.js file. Make sure you are the app directory while executing the command.
nano server.js
Once you have created the server.js file you can add code as shown in the block below.
var express = require('express');
var app = express();

Step 4: Create a controller for the download route file.

Create a controller file say download.controller.js and add the following code block.
var express = require('express');
var AWS = require('aws-sdk');
var fs = require('fs');

const downloadFile = async (req, res, next) => {
     var fileKey = req.query['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);
};

module.exports = {
   downloadFile
};

Step 5: Creating a route to download files (REST API)

Create a route file, say download.routes.js and import the controller and add the following code block that works as a REST API to download the file from AWS S3.
// make sure to check the file location
const downloadController = require("download.controller");
module.exports = function (app) {
  app.use(function (req, res, next) {
    res.header(
      "Access-Control-Allow-Headers",
      "x-access-token, Origin, Content-Type, Accept"
    );
    next();
  });
  app.get("/api/v1/products/", downloadController.downloadFile);


    // setting the request query to download file via AWS S3 bucket
    var fileKey = req.query['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 to it:
var express = require('express');
var app = express();

require("download.routes")(app);

// You can specify port in your Node Environment or it will fire-up on 8080
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

Step 4 — Start Node Express Js App Server

Now, run the following command on the terminal to fire-up your express server.
npm run start

Conclusion

Thus, we can download files from the Amazon S3 bucket using Node JS Express and aws-s3 dependencies.