Created
March 2, 2022 02:37
-
-
Save hengkiardo/3114b81990332bfb958982b7a409a462 to your computer and use it in GitHub Desktop.
Uploading files to Amazon S3 in ExpressJS with Multer
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
'use strict'; | |
const express = require('express'); | |
const app = express(); | |
const multer = require('multer'); | |
const multerS3 = require('multer-s3'); | |
const AWS = require('aws-sdk'); | |
const s3 = new AWS.S3({ | |
accessKeyId: 'access-key-id', | |
secretAccessKey: 'secret-access-key' | |
}); | |
const uploadS3 = multer({ | |
storage: multers3({ | |
s3: s3, | |
acl: 'public-read', | |
bucket: 'bucket-name', | |
metadata: (req, file, cb) => { | |
cb(null, {fieldName: file.fieldname}) | |
}, | |
key: (req, file, cb) => { | |
cb(null, Date.now().toString() + '-' + file.originalname) | |
} | |
}) | |
}); | |
app.post('/upload', uploadS3.single('file'),(req, res) => { | |
console.log(req.file); | |
}); | |
app.listen(process.env.PORT || 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment