0

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!

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

3 Comments

@Rajan .. how do i implement ffmpeg to convert a file and thn upload to S3?
@Somename I've never done that yet but the following question seems like a good starting point stackoverflow.com/questions/28286798/… stackoverflow.com/questions/43227600/…
I've posted the second question with no luck so far. Could you please try and tell me ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.