Images not getting Stored in S3 bucket

user3930213 picture user3930213 · May 11, 2017 · Viewed 9.2k times · Source

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>

Answer

RandomEli picture RandomEli · May 11, 2017

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