-
-
Save ojalao/2ea684697c0dd923fc15ff6f1a5b411e 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 hidden or 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}`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment