Upload files on AWS-S3 with nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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