未在S3存储桶中存储的图像

时间:2017-05-11 20:01:04

标签: node.js amazon-web-services amazon-s3 multer-s3

我正在使用s3 multer模块直接上传用户上传到我的存储桶中的图像文件。该应用程序显示更新成功但我无法在桶中看到我的文件。上传文件后的页面响应上传成功。下面是我正在使用的app.js文件和index.html,这是从github repo分叉的人说它有效但我的工作不起作用,请告诉我错误?我在我的localhost上运行它。

代码:

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>

1 个答案:

答案 0 :(得分:1)

我认为新版本做了一些改变。对于最新版本,您应该做的是:

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
        }
    })
});

当您想要访问上传数组时:

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

我相信您的原始代码来自此处:

Multer S3 Upload Example