Node.js RestAPIs upload file to Amazon S3 | using Express, Multer, AWS-SDK

node-js-restapis-upload-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 Upload Files to Amazon S3 using Multer middleware and AWS-SDK.

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
  • Multer
  • AWS-SDK
  • Amazon S3

Overview

Demo

Goal

We create a Node.js project as below ->

nodejs-express-restapis-multer-upload-files-to-aws-amazon-s3-project-structure

Expose a RestAPI: '/api/file/upload'

Upload File ->

nodejs-express-restapis-multer-upload-files-to-aws-amazon-s3-upload-file-succesffully

Amazon S3 ->

nodejs-express-restapis-multer-upload-files-to-aws-amazon-s3-uploaded-file-on-s3

Practice

Setting up Node.js project

Create a folder NodejsUploadFileAmazonS3, 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: (nodejsuploadfileamazons3)
version: (1.0.0)
description: Node.js Express RestAPIs upload file to Amazon S3 using Multer, AWS-SDK
entry point: (index.js) server.js
test command:
git repository:
keywords: Nodejs, Express, Amazon S3, RestAPI, Upload File, Multer, Aws SDK
author: ozenero.com
license: (ISC)
About to write to D:\gkz\article\NodejsUploadFileAmazonS3\package.json:

{
  "name": "nodejsuploadfileamazons3",
  "version": "1.0.0",
  "description": "Node.js Express RestAPIs upload file to Amazon S3 using Multer, AWS-SDK",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Nodejs",
    "Express",
    "Amazon",
    "S3",
    "RestAPI",
    "Upload",
    "File",
    "Multer",
    "Aws",
    "SDK"
  ],
  "author": "ozenero.com",
  "license": "ISC"
}


Is this ok? (yes) yes

Install Express, Multer, AWS-SDK ->


npm install express multer aws-sdk --save

-> package.json file:


{
  "name": "nodejsuploadfileamazons3",
  "version": "1.0.0",
  "description": "Node.js Express RestAPIs upload file to Amazon S3 using Multer, AWS-SDK",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Nodejs",
    "Express",
    "Amazon",
    "S3",
    "RestAPI",
    "Upload",
    "File",
    "Multer",
    "Aws",
    "SDK"
  ],
  "author": "ozenero.com",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.302.0",
    "express": "^4.16.3",
    "multer": "^1.3.1"
  }
}

Multer Config

– Create ./app/config/multer.config.js file:


const multer = require('multer');
 
var storage = multer.memoryStorage()
var upload = multer({storage: storage});
 
module.exports = upload;

AWS S3 Config

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


const env = {
	AWS_ACCESS_KEY: 'AKIAJMIPBAVVXRVB4AWA', // change to yours
	AWS_SECRET_ACCESS_KEY: 'lu3aELpRhETjw9xC+Qs5VhCpqkP+6VPoEL4khwxL', // 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 uploadParams = {
         Bucket: env.Bucket, 
         Key: '', // pass key
         Body: null, // pass file body
};

const s3 = {};
s3.s3Client = s3Client;
s3.uploadParams = uploadParams;

module.exports = s3;

Express Routers

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


let express = require('express');
let router = express.Router();
let upload = require('../config/multer.config.js');
 
const awsWorker = require('../controllers/aws.controller.js');
 
router.post('/api/file/upload', upload.single("file"), awsWorker.doUpload);
 
module.exports = router;

Upload Controller

– Implement S3 Uploader in a controller file ./app/controllers/aws.controller.js ->


var stream = require('stream');
 
const s3 = require('../config/s3.config.js');
 
exports.doUpload = (req, res) => {
	const s3Client = s3.s3Client;
	const params = s3.uploadParams;
	
	params.Key = req.file.originalname;
	params.Body = req.file.buffer;
		
	s3Client.upload(params, (err, data) => {
		if (err) {
			res.status(500).json({error:"Error -> " + err});
		}
		res.json({message: 'File uploaded successfully! -> keyname = ' + req.file.originalname});
	});
}

Server.js

server.js file ->


const express = require('express');
const app = express();
 
let router = require('./app/routers/upload.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.

NodejsUploadFileAmazonS3

30 thoughts on “Node.js RestAPIs upload file to Amazon S3 | using Express, Multer, AWS-SDK”

  1. Hi ,
    I done same thing, in my windows machine ,
    but i got a error like “cannot read property ‘originalname’ of defined.
    can you please help me on this.

  2. Perfect, It is working for single file upload but I need to upload multiple files. And I approached this method upload.array(“file”, 3) but it is giving error Cannot read property ‘original name’ of undefined would you please help me to figure out this.

  3. Obwohl Das macht das Spiel weniger instabilen als No-Limit oder Pot-Limit
    Aktivitäten, Darüber hinaus ist es Kosten in viele von Gewinnung
    Finger – was zu mehr massiven Töpfe.

  4. Thank you for another excellent article. Exactly where else could anybody get that kind of data in this kind of a perfect way of writing? I have a presentation subsequent week, and I am to the appear for such information and facts.

  5. *When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!

  6. 800535 456743It can be difficult to write about this topic. I think you did an exceptional job though! Thanks for this! 261623

  7. 513992 746224Somebody necessarily support to make seriously articles I might state. That could be the very initial time I frequented your internet page and to this point? I surprised with the research you produced to make this actual put up remarkable. Fantastic task! 329093

  8. 810500 285884Hi there for your individual broad critique, then once again particularly passionate the recent Zune, and moreover intend this specific, not to mention the beneficial feedbacks other sorts of every person has posted, will determine if is it doesnt answer you are searching for. 323510

  9. 424852 241337Aw, this was a genuinely nice post. In concept I wish to put in writing like this in addition ?taking time and actual effort to make an excellent article?nevertheless what can I say?I procrastinate alot and by no indicates seem to get something done. 891541

  10. 175555 260280Id need to consult you here. Which isnt some thing Which i do! I enjoy reading a post that can make folks feel. Also, appreciate your allowing me to comment! 977821

  11. 73410 140156Hi there, just became aware of your weblog by means of Google, and found that its truly informative. Im gonna watch out for brussels. I will appreciate in the event you continue this in future. Lots of people will be benefited from your writing. Cheers! 98724

  12. 881944 890605Does your web site have a contact page? Im having trouble locating it but, Id like to send you an e-mail. Ive got some suggestions for your weblog you may be interested in hearing. Either way, excellent weblog and I look forward to seeing it develop more than time. 993490

Leave a Reply

Your email address will not be published.