How to Upload File to Amazon AWS s3 in Node JS?

07-Nov-2022

.

Admin

How to Upload File to Amazon AWS s3 in Node JS?

Hello Friends,

I will show you an example of how to upload files to amazon AWS s3 in node js. I want to share with you how to upload files to s3 from node.js. In this article, we will implement an uploading file to AWS s3 using nodejs. We will use uploading files to amazon s3 in node.js. So, let's follow a few step to create an example of file upload AWS s3 using nodejs in rest-full API

This uploading files to AWS S3 with Node.js tutorial will create file upload to amazon s3 bucket using node js + express + REST API.

Step 1: Create Node Express js App


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

var aws = require('aws-sdk')

var express = require('express')

var multer = require('multer')

var multerS3 = require('multer-s3')

Configure AWS-S3 Package with Multer

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

var s3 = new aws.S3({

accessKeyId: " ",

secretAccessKey: " ",

Bucket: " "

})

var 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 Multiple File to AWS S3 with Node.js REST API Route

The following node js rest API route will upload multiple files to the amazon s3 bucket:

//Uploading single File to aws s3 bucket

app.post('/upload', upload.single('photos'), function (req, res, next) {

res.send({

data: req.files,

msg: 'Successfully uploaded ' + req.files + ' files!'

})

})

Open your server js file and add the following code into it:

var aws = require('aws-sdk')

var express = require('express')

var multer = require('multer')

var multerS3 = require('multer-s3')

var app = express()

var s3 = new aws.S3({

accessKeyId: " ",

secretAccessKey: " ",

Bucket: " "

})

var 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())

}

})

})

//Uploading single File to aws s3 bucket

app.post('/upload', upload.single('photos'), function (req, res, next) {

res.send({

data: req.files,

msg: 'Successfully uploaded ' + req.files + ' files!'

})

})

//Uploading Multiple Files to aws s3 bucket

app.post('/upload', upload.array('photos', 3), function (req, res, next) {

res.send({

data: req.files,

msg: 'Successfully uploaded ' + req.files.length + ' files!'

})

})

app.listen(4000, function () {

console.log('express is online');

})

Step 4: Start Node Express Js App Server

Execute the following command on the terminal to start the 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:

I hope it can help you...

#Node JS