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 ->
Expose a RestAPI: '/api/file/upload'
Upload File ->
Amazon 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
.