Last active
November 25, 2022 20:50
-
-
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
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 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}`) | |
}) |
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
Error : expected 0 arguments, but got 1.ts(2554) when usgin multer.memmoryStorage({....}) why