Image for post
Image for post
Image for post
Image for post

Creating a S3 Uploading Service with Node.js and AWS-SDK

AWS Simple Storage Service, or better known as S3, is an object storage service that offers high durability, availability, and performance. Being one of the most frequently used service in AWS, its support and service level agreement offers extremely high value for tech companies who have needs for storage solutions.

  1. Create a S3 Bucket
  2. Create a user with the right permission policy attached to it
npm -i aws-sdk multer multer-s3 express body-parser dotenv
AWS_ACCESS_KEY_ID=Your Key ID HereAWS_SECRET_ACCESS_KEY=Your Key Secret HereAWS_REGION=The Region of Your Object StorageS3_BUCKET=Your Bucket Name
const path = require(‘path’);const configVar = require(‘dotenv’).config({ path: path.join(__dirname, ‘/.env’) });const aws = require(‘aws-sdk’);aws.config.update({// Your SECRET ACCESS KEY from AWS should go here,// Never share it!// Setup Env Variable, e.g: process.env.SECRET_ACCESS_KEYsecretAccessKey: configVar.parsed.AWS_SECRET_ACCESS_KEY,// Not working key, Your ACCESS KEY ID from AWS should go here,// Never share it!// Setup Env Variable, e.g: process.env.ACCESS_KEY_IDaccessKeyId: configVar.parsed.AWS_ACCESS_KEY_ID,region: configVar.parsed.AWS_REGION // region of your bucket});const s3 = new aws.S3();module.exports={aws,s3,s3bucket:configVar.parsed.S3_BUCKET}
const {s3,s3bucket} = require(‘../config/config’)const express = require(‘express’)const router = express.Router();const multer = require(‘multer’);const multerS3 = require(‘multer-s3’);router.get(‘/’, function (req, res) {res.sendFile(__dirname + ‘/index.html’);})router.post(‘/upload’,function (req, res) {const multerUpload = multer({storage: multerS3({s3: s3,key: function (req, file, cb) {cb(null, Date.now().toString())}})}).array(‘file’, 1);multerUpload(req, res, function (err) {if (err) {res.send(‘multerUpload Error:’ + err);}else{res.send(“Success”)}})})module.exports = router
<!DOCTYPE html><html><head lang=”en”><meta charset=”UTF-8"><title></title></head><body>You can upload the file to s3 directly here<form method=”post” enctype=”multipart/form-data” action=”/s3upload/upload”><p><input type=”file” name=”file”/></p><p><input type=”submit”/></p></form></body></html>
const express = require(‘express’),bodyParser = require(‘body-parser’);const {aws,s3} = require(‘./config/config’);const s3upload = require(‘./s3-upload/s3-multer’);const app = express();app.use(bodyParser.json());app.use(‘/s3upload’,s3upload)app.listen(3000, function () {console.log(‘app listening on port 3000!’);});

Written by

Software consulting company that focuses on emerging technology such as AI, Blockchain, Cloud Computing, and Data Engineering, MERN Stack, and Fintech

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store