NodeJS Upload File to S3 bucket using Node JS + Express

In this tutorial, you will learn how to upload the file in S3 bucket with the help of NodeJS and ExpressJS

To upload file in S3 we will create API, to achive this we will need following packages

  • Express
  • Multer
  • aws-s3

We will follow given steps to upload single or multiple file to amazon s3 bucket using node js + express + rest api. So let get started with the NodeJS Upload File to S3 bucket using Node JS + Express tutorial

  1. Create Node Express js App
  2. Install express, aws-s3, Multer dependencies
  3. Create Server.js File
    • Import Installed Packages
    • Configure AWS-S3 Package with Multer
    • Create Uploading Single File to AWS S3 with Node.js REST API Route
    • Create Uploading MultipleFile to AWS S3 with Node.js REST API Route
  4. Start Node Express Js App Server
  5. Uploading Files to AWS S3 Bucket with Node.js REST API

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, Multer dependencies

Execute the following command on terminal to install express, aws-s3 and multer dependencies:

npm install express multer aws-sdk body-parser --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.

body-parser — Express body-parser is an npm library used to process data sent through an HTTP request body. It exposes four express middlewares for parsing text, JSON, url-encoded and raw data set through an HTTP request body.

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
  • Configure AWS-S3 Package with Multer
  • Create Uploading Single File to AWS S3 with Node.js REST API Route
  • Create Uploading Single File to AWS S3 with Node.js REST API Route

Import Installed Packages

Import above installed dependencies package in server.js file:

const aws = require('aws-sdk');
const express = require('express');
const multer = require('multer');
const multerS3 = require('multer-s3');
const app = express();

app.listen(4000, function () {console.log('express is online');})

Configure AWS-S3 Package with Multer

Then configure aws-s3 package with multer as shown below:

const s3 = new aws.S3({ accessKeyId: " ", secretAccessKey: " ", Bucket: " " });
const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: "", 
    metadata: function (req, file, cb) {
      cb(null, { fieldName: file.fieldname });
    }, 
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

Create Uploading Single File to AWS S3 with Node.js REST API Route

The following node js rest api route will be upload single file to amazon s3 bucket:

app.post('/upload', upload.single('photos'), function (req, res, next) {
  res.send({data: req.files,msg: 'Successfully uploaded ' + req.files + ' files!'})
});

Create Uploading Multiple File to AWS S3 with Node.js REST API Route

The below API route will be able to upload multiple file to amazon S3 bucke

app.post('/multiple-upload', upload.array('photos', 4), function (req, res, next) {
  res.send({data: req.files,msg: 'Successfully uploaded ' + req.files + ' files!'})
});

Everything is ready now, Let’s merge all the code together in a file. To do so open the server.js file and put all the code together

const aws = require('aws-sdk');
const express = require('express');
const multer = require('multer');
const multerS3 = require('multer-s3');
const app = express();

const s3 = new aws.S3({ accessKeyId: " ", secretAccessKey: " ", Bucket: " " });
const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: "", 
    metadata: function (req, file, cb) {
      cb(null, { fieldName: file.fieldname });
    }, 
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
});

app.post('/upload', upload.single('photos'), function (req, res, next) {
  res.send({data: req.files,msg: 'Successfully uploaded ' + req.files + ' files!'})
});

app.post('/multiple-upload', upload.array('photos', 4), function (req, res, next) {
  res.send({data: req.files,msg: 'Successfully uploaded ' + req.files + ' files!'})
});

app.listen(4000, 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

Step 5 – Uploading Files to AWS S3 Bucket with Node.js REST API

To upload single or multiple files to aws s3 bucket using node js rest API; So open postman for sending HTTP multipart/form-data requests: as shown below picture:

Conclusion

To upload files to AWS S3 using Node js + express + rest API; Through this tutorial, you will learn how to upload files to the amazon s3 bucket using node js + rest api+ express with the express, aws-s3, multer package.

Recommended Node JS Tutorials

Comments are closed.