Node.js RestAPIs Download File from Amazon S3 | using Express, AWS-SDK

node-js-restapis-download-file-from-amazon-s3-using-express-aws-sdk-feature-image-new

In the tutorial, we show how to build a Node.js/Express RestAPIs to Download Files from Amazon S3 using AWS-SDK.

Previous post:
Node.js RestAPIs upload file to Amazon S3

Related posts:
Node.js/Express – PostgreSQL example – Upload File/Download File – Multer + Sequelize CRUD
NodeJS/Express – Bootstrap Image example
Mongoose Many-to-Many related models with NodeJS/Express, MongoDB

Related pages:

Technologies

  • Node.js
  • Express
  • AWS-SDK
  • Amazon S3

Overview

Demo

Goal

We create a Node.js project as below ->

nodejs-express-restapis-download-file-from-amazon-s3-using-aws-sdk-project-structure

Expose a RestAPI: '/api/files/:filename'

File on Amazon S3 ->

nodejs-express-restapis-download-file-from-amazon-s3-using-aws-sdk-file-on-s3-amazon

Download File ->

nodejs-express-restapis-download-file-from-amazon-s3-using-aws-sdk-get-file-successfully

Error ->

nodejs-express-restapis-download-file-from-amazon-s3-using-aws-sdk-get-file-wrong-key-fail

Practice

Setting up Node.js project

Create a folder NodejsDownloadFileAmazonS3, then init Node.js project ->

>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install ` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (nodejsdownloadfileamazons3)
version: (1.0.0)
description: Nodejs Express RestAPIs downloads file from Amazon S3
entry point: (index.js) server.js
test command:
git repository:
keywords: Nodejs, Express, RestAPI, DownloadFile, Amazon S3, AWS-SDK
author: ozenero.com
license: (ISC)
About to write to D:\gkz\article\NodejsDownloadFileAmazonS3\package.json:

{
  "name": "nodejsdownloadfileamazons3",
  "version": "1.0.0",
  "description": "Nodejs Express RestAPIs downloads file from Amazon S3",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Nodejs",
    "Express",
    "RestAPI",
    "DownloadFile",
    "Amazon",
    "S3",
    "AWS-SDK"
  ],
  "author": "ozenero.com",
  "license": "ISC"
}


Is this ok? (yes) yes

Install Express, AWS-SDK ->

npm install express aws-sdk --save

-> package.json file:

{
  "name": "nodejsdownloadfileamazons3",
  "version": "1.0.0",
  "description": "Nodejs Express RestAPIs downloads file from Amazon S3",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Nodejs",
    "Express",
    "RestAPI",
    "DownloadFile",
    "Amazon",
    "S3",
    "AWS-SDK"
  ],
  "author": "ozenero.com",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.302.0",
    "express": "^4.16.3"
  }
}

AWS S3 Config

– Create a file ./app/config/s3.env.js ->

const env = {
	AWS_ACCESS_KEY: 'AKIAIGGOGCH2UD7H63JA', // change to yours
	AWS_SECRET_ACCESS_KEY: 'GaGHmB7ajGsBOd6w626l6wilE+es3zqN9oT9C9Ka', // change to yours
	REGION : 'us-east-2', // change to yours
	Bucket: 'ozenero-s3-bucket' // change to yours
};
 
module.exports = env;

– Configure S3 Client in a file ./app/config/s3.config.js ->

const AWS = require('aws-sdk');
const env = require('./s3.env.js');
 
const s3Client = new AWS.S3({
    accessKeyId: env.AWS_ACCESS_KEY,
    secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
	region : env.REGION
});
 
const downloadParams = {
         Bucket: env.Bucket, 
         Key: '', // pass key
};

const s3 = {};
s3.s3Client = s3Client;
s3.downloadParams = downloadParams;
 
module.exports = s3;

Express Router

– Create router in file ./app/routers/s3.router.js ->

let express = require('express');
let router = express.Router();
 
const awsWorker = require('../controllers/s3.controller.js');
 
router.get('/api/files/:filename', awsWorker.doDownload);
 
module.exports = router;

Download Controller

– Implement S3 Downloader in a controller file ./app/controllers/s3.controller.js ->

var stream = require('stream');
 
const s3 = require('../config/s3.config.js');
 
exports.doDownload = (req, res) => {
	const s3Client = s3.s3Client;
	const params = s3.downloadParams;
	
	params.Key = req.params.filename;

	s3Client.getObject(params)
		.createReadStream()
			.on('error', function(err){
				res.status(500).json({error:"Error -> " + err});
		}).pipe(res);
}

Server.js

server.js file ->

const express = require('express');
const app = express();
 
let router = require('./app/routers/s3.router.js');
app.use('/', router);
 
// Create a Server
const server = app.listen(8080, function () {
 
  let host = server.address().address
  let port = server.address().port
 
  console.log("App listening at http://%s:%s", host, port); 
})

SourceCode

Use npm install to install all dependencies before run the Node.js server with cmd npm start.

NodejsDownloadFileAmazonS3

0 0 votes
Article Rating
Subscribe
Notify of
guest
28 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments