Uploading image to amazon s3 using multer-s3 nodejs

I am trying to upload an image to amazon s3 using multer-s3, but I am getting this error:

TypeError: Expected opts.s3 to be object
node_modules/multer-s3/index.js:69:20

  • Cropping and resizing images on the fly with node.js
  • get uploaded file name/path in node.js
  • Why is my S3 upload not uploading correctly?
  • Formidable doesnt form.parse() when uploading a file in Nodejs
  • How do I upload multiple files directly to Amazon S3?
  • Access denied on aws lambda function when getObject from S3 bucket
  • This is my server code:

    var upload = multer({
        storage: s3({
            dirname: '/',
            bucket: 'bucket',
            secretAccessKey: 'key',
            accessKeyId: 'key',
            region: 'us-west-2',
            filename: function (req, file, cb) {
                cb(null, file.originalname); 
            }
        })
    });
    
    app.post('/upload', upload.array('file'), function (req, res, next) {
        res.send("Uploaded!");
    });
    

    Why I am getting this error?

  • Refactor: Upload multiple images - NodeJS & Amazon S3
  • How can I convert uploaded file on-the-fly and stream it directly to amazon S3 within sails
  • Node download archives from s3 - Error: incorrect header check in gunzip
  • how to secure s3 files with trusted signed users using cloudfront aws-sign?
  • How to change upload path when use formidable with express in node.js
  • AngularJs Image upload to S3
  • 2 Solutions collect form web for “Uploading image to amazon s3 using multer-s3 nodejs”

    s3 needs to be an object to be passed. According to the docs, the object needs to be like this:

    var upload = multer({
      storage: multerS3({
        s3: s3,
        bucket: 'some-bucket',
        metadata: function (req, file, cb) {
          cb(null, {fieldName: file.fieldname});
        },
        key: function (req, file, cb) {
          cb(null, Date.now().toString())
        }
      })
    })
    

    MulterS3 Docs

    Complete and working Node Cheat | Upload to s3 using multer-s3 available.

    Code:

    var express = require('express'),
        aws = require('aws-sdk'),
        bodyParser = require('body-parser'),
        multer = require('multer'),
        multerS3 = require('multer-s3');
    
    aws.config.update({
        secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        accessKeyId: 'XXXXXXXXXXXXXXX',
        region: 'us-east-1'
    });
    
    var app = express(),
        s3 = new aws.S3();
    
    app.use(bodyParser.json());
    
    var upload = multer({
        storage: multerS3({
            s3: s3,
            bucket: 'bucket-name',
            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
    app.get('/', function (req, res) {
        res.sendFile(__dirname + '/index.html');//index.html is inside node-cheat
    });
    
    //use by upload form
    app.post('/upload', upload.array('upl',1), function (req, res, next) {
        res.send("Uploaded!");
    });
    
    app.listen(3000, function () {
        console.log('Example app listening on port 3000!');
    });
    

    For complete repo:

    Clone node-cheat express_multer_s3, run node app followed by npm install express body-parser aws-sdk multer multer-s3.

    Happy Helping!

    Node.js is the Best Javascript runtime in the world.