Skip to content

Instantly share code, notes, and snippets.

@nyx-code
Last active November 25, 2022 20:50
Show Gist options
  • Save nyx-code/c1c07dc7830cd596c7a1b48d0dd5d15f to your computer and use it in GitHub Desktop.
Save nyx-code/c1c07dc7830cd596c7a1b48d0dd5d15f to your computer and use it in GitHub Desktop.
This NodeJS API which will upload files onto the AWS S3 Bucket. Video -> https://youtu.be/TtuCCfren_I
require('dotenv/config')
const express = require('express')
const multer = require('multer')
const AWS = require('aws-sdk')
const uuid = require('uuid/v4')
const app = express()
const port = 3000
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ID,
secretAccessKey: process.env.AWS_SECRET
})
const storage = multer.memoryStorage({
destination: function(req, file, callback) {
callback(null, '')
}
})
const upload = multer({storage}).single('image')
app.post('/upload',upload,(req, res) => {
let myFile = req.file.originalname.split(".")
const fileType = myFile[myFile.length - 1]
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `${uuid()}.${fileType}`,
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 at ${port}`)
})
@shah21
Copy link

shah21 commented Mar 12, 2021

Error : expected 0 arguments, but got 1.ts(2554) when usgin multer.memmoryStorage({....}) why

@HappyDevCompany
Copy link

thanks bro for sharing beautifull and simple code example 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment