Created
July 8, 2016 02:38
-
-
Save aconfee/5348d4f68fd60dda2b34d368c5e0ab1c to your computer and use it in GitHub Desktop.
multer-s3 issue
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
var AWS = require('aws-sdk'); | |
AWS.config.region = 'us-west-2'; | |
AWS.config.update({ | |
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | |
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY | |
}); | |
var s3 = new AWS.S3({params: {Bucket: process.env.S3_BUCKET_NAME}}); | |
var multer = require('multer'); | |
var multerS3 = require('multer-s3'); | |
var upload = multer({ | |
storage: multerS3({ | |
s3: s3, | |
bucket: process.env.S3_BUCKET_NAME, | |
metadata: function (req, file, cb) { | |
console.log("File: " + file.key); // These output undefined. | |
console.log("File: " + file.location); | |
cb(null, {fieldName: file.fieldname}); | |
}, | |
key: function (req, file, cb) { | |
console.log(file.key); | |
console.log(file.location); | |
cb(null, Date.now().toString()); | |
} | |
}) | |
}); | |
router.post('/upload', upload.any(), function(req, res){ | |
console.log("successfully uploaded"); | |
res.status(200); | |
console.log("filepath " + req.files[0].filename); | |
res.json({ filepath: '/images/' + req.files[0].filename }); | |
}); | |
/**** | |
The console output here is: | |
file: undefined | |
file: undefined | |
successfully uploaded | |
filepath undefined | |
POST /api/upload 200 1507.201 ms - 32 | |
GET /images/undefined 304 1.151 ms - - | |
******/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment