Here we will learn how to implement S3 bucket in our code.
The first thing that we need is to create an account in AWS service.
Follow this link in order to create S3 bucket.
First we need to install some npm:
$ npm install express
$ npm install aws-sdk
$ npm install multer-s3
$ npm install multer
//Code starts
const AWS = require(‘aws-sdk’);
const multerS3 = require(‘multer-s3’);
const multer = require(‘multer’);
//uploading image to s3 bucket
var s3 = new AWS.S3({
accessKeyId: “AWS_ACCESS_KEY_ID”, //This key you will get from AWS IAM module
secretAccessKey: “AWS_SECRET_ACCESS_KEY_ID”, //This key you will get from AWS IAM module
region: “AWS_REGION” //Region is something that you have provide while creating an account
});
var upload = multer({
storage:multerS3({
s3:s3,
bucket:’AWS_BUCKET_NAME’,
acl:’public-read’,
metadata:function (req, file, cb) {
cb(null, {fieldName:file.fieldname});
},
key:function (req, file, cb) {
if(!file.originalname.match(/\.(jpg|jpeg|png)$/i)){
returncb(newError(‘Please upload a vaild image file’))
}
cb(null,’images/profile_pic/’+file.fieldname+’-‘+Date.now().toString()+path.extname(file.originalname))
}
})
})
varprofileUpload=multer(upload).fields([{ name:’profile_pic‘, maxCount:1 }, { name:’team_logo‘, maxCount:1 }])
//Now above we have call profile_pic as name which will be our fieldName in our request parameter and also we can only pass files in form-data so make sure we are passing data in form-data.

//exporting our multer function
module.exports = profileUpload;
//Now we will learn how to call above function in our route
const express = require(‘express’); // here we are using express framework
const app = express();
//Importing file into our route file
const profileUpload = require(‘../../middleware/uploadprofileimage’);
//Calling user create route
app