Reputation: 37
I have a problem uploading images to S3 using multer and multer-s3 npm with node.js and express.
I have read the documentation of multer and multer-s3 and followed the tutorials, and searched on stackoverflow and other websites to solve my issue but no success.
This is my client side code:
<form method="post" enctype="multipart/form-data" action="/test">
<p>
<input type="text" name="title" placeholder="optional title"/>
</p>
<p>
<input type="file" name="upl"/>
</p>
<p>
<input type="submit"/>
</p>
</form>
And here is my server side code:
var express = require('express'),
router = express.Router(),
aws = require('aws-sdk'),
multer = require('multer'),
multerS3 = require('multer-s3'),
s3 = new aws.S3()
aws.config = ({
secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
accessKeyId: 'XXXXXXXXXXXXXX'
});
var upload = multer({
storage: multerS3({
s3: s3,
bucket: 'styleboxphotosbianor',
key: function (req, file, cb) {
console.log(file);
cb(null, file.originalname); //use Date.now() for unique file keys
}
})
});
//open in browser to see upload form
router.get('/', function (req, res) {
res.render('multer');
});
//use by upload form
router.post('/', upload.array('upl',1), function (req, res, next) {
res.send("Uploaded!");
});
module.exports = router;
And I got this error
TypeError: this.s3.upload is not a function
at S3Storage.<anonymous> (/Users/magintosh/bianor/node_modules/multer-s3/index.js:150:26)
So i need your help my friends. Thank you a lot for being here for us!
Upvotes: 0
Views: 3680
Reputation: 495
You must create "s3" variable only after setting up your "aws" module. And also setting up the "aws" package should be with "aws.config.update"
var express = require('express'),
router = express.Router(),
aws = require('aws-sdk'),
multer = require('multer'),
multerS3 = require('multer-s3');
aws.config.update({
secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
accessKeyId: 'XXXXXXXXXXXXXX'
});
s3 = new aws.S3();
*I assume that you replace the value for "secretAccessKey" and "accessKeyId" with actual key from AWS and you do have AWS account (some tutorial miss to mention that)
Upvotes: 3