Images not getting Stored in S3 bucket

20,026

I think the new version did some change. For the latest version, what you should do is:

var upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'XXXX',
        dirname: '/profilepics',
        secretAccessKey:'YYYY',
        accessKeyId:'TYYYYYY',
        region: 'us-west-2',
        key: function (req, file, cb) {
            console.log(file);
            cb(null, file.originalname); //use Date.now() for unique file keys
        }
    })
});

And when you want to access the upload array:

app.post('/upload', upload.array('upl',1), function (req, res, next)

And I believe your original code is from here:

Multer S3 Upload Example

Share:
20,026
user3930213
Author by

user3930213

Updated on May 12, 2020

Comments

  • user3930213
    user3930213 about 4 years

    I am using s3 multer module to directly upload a image file which is uploaded by user into my bucket. The app shows that update successful but i cannot see my files in bucket. The page after uploading the files respond with Upload successful. Below is the app.js file and index.html which I am using,this was forked from github repo people say it works but mine isnt working ,please tell me the error? I am running this on my localhost.

    Code:

    aws = require('aws-sdk'),
    var express = require('express'),
        bodyParser = require('body-parser'),
        multer = require('multer'),
        s3 = require('multer-s3');
    
    
    aws.config.update({
        secretAccessKey:'XXXXX',
        accessKeyId:'YYYYY',
        region: 'us-west-2'});
    
    var app = express();
    
    app.use(bodyParser.json());
    
    var upload = multer({
        storage: s3({
            dirname: '/profilepics',
            bucket: 'XXXX',
            secretAccessKey:'YYYY',
            accessKeyId:'TYYYYYY',
            region: 'us-west-2',
            filename: function (req, file, cb) {
                cb(null, "1234"); //use Date.now() for unique file keys
            }
        })
    });
    
    //open in browser to see upload form
    app.get('/', function (req, res) {
        res.sendFile(__dirname + '/index.html');
    });
    
    //use by upload form
    app.post('/upload', upload.array('upl'), function (req, res, next) {
        res.send("Uploaded!");
    });
    
    app.listen(4000, function () {
        console.log('Example app listening on port 3000!');
    });
    

    Index.html:

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    Hey! Lets try uploading to s3 directly :)
    
    <form method="post" enctype="multipart/form-data" action="/upload">
        <p>
            <input type="text" name="title" placeholder="optional title"/>
        </p>
    
        <p>
            <input type="file" name="upl"/>
        </p>
    
        <p>
            <input type="submit"/>
        </p>
    </form>
    </body>
    </html>
    
    • RandomEli
      RandomEli about 7 years
      You could do it directly with S3 SDK function: s3.putObject({ Bucket: dstBucket, Key: your key, Body: data, ContentType: 'JPG'}, next_cb);
    • RandomEli
      RandomEli about 7 years
      You can accept my answer if it helps you
  • user3930213
    user3930213 about 7 years
    it gives error that storage needs accesskey parameter
  • RandomEli
    RandomEli about 7 years
    @user3930213 Then add it in the json, check if your access key matches with your S3 setting.