Skip to content

Instantly share code, notes, and snippets.

@SachinSMahajan
Created Oct 16, 2021
Embed
What would you like to do?
Upload files on AWS-S3 with nodejs
require('dotenv/config')
const express = require('express');
const multer = require('multer')
const AWS = require('aws-sdk')
const app = express();
const PORT = 3000;
const storage = multer.memoryStorage()
const upload = multer({ storage: storage }).single('file')
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_AccessKey_Id,
secretAccessKey: process.env.AWS_Secret_Key
})
app.post('/upload', upload, (req, res) => {
let fileName = req.file.originalname;
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: fileName,
Body: req.file.buffer
}
s3.upload(params, (error, data) => {
if (error) {
res.status(500).send(error)
}
res.status(200).send(data)
})
})
app.listen(PORT, () => {
console.log(`Server is up and running on Port ${PORT}`);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment