Node JS Upload File to Amazon AWS s3

Node JS Upload File to Amazon AWS s3

Uploading files to amazon AWS S3 using Node js + express; This tutorial will show you how to upload file to amazon s3 bucket using node js + express with express, aws-s3, multer package.

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

Upload File to Amazon s3 bucket using Node JS + Express

Follow the below-given steps to upload single or multiple file to amazon s3 bucket using node js + express + rest api:

  • Step 1 – Create Node Express js App
  • Step 2 – Install express, aws-s3, Multer dependencies
  • Step 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
  • Step 4 – Start Node Express Js App Server
  • Step 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:

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 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:

//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!'
   })
})

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

The following node js rest api route will be upload multiple file to 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 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

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *