Skip to content

Instantly share code, notes, and snippets.

@ppshein
Last active May 15, 2019 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppshein/cec8cc1bfbc8f4c8bf1900691e6a1b62 to your computer and use it in GitHub Desktop.
Save ppshein/cec8cc1bfbc8f4c8bf1900691e6a1b62 to your computer and use it in GitHub Desktop.
Upload files to AWS-S3 with NodeJS
var aws = require('aws-sdk')
var express = require('express')
var multer = require('multer')
var multerS3 = require('multer-s3')
var app = express()
var s3 = new aws.S3({
accessKeyId: '',
secretAccessKey: '',
region: 'ap-southeast-1'
})
var upload = multer({
storage: multerS3({
s3: s3,
bucket: 'ayaplus-kyc-files',
metadata: function (req, file, cb) {
cb(null, {
fieldName: file.fieldname
});
},
key: function (req, file, cb) {
cb(null, Date.now().toString())
}
})
})
app.post('/upload', upload.array('photos', 3), function(req, res, next) {
res.send('Successfully uploaded ' + req.files.originalname + ' files!')
})
app.get('/image/:imageId', function (req, res, next) {
var params = { Bucket: 'ayaplus-kyc-files', Key: req.params.imageId };
s3.getObject(params, function(err, data) {
res.writeHead(200, {'Content-Type': 'image/jpeg'});
res.write(data.Body, 'binary');
res.end(null, 'binary');
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment