1

我正在使用 s3 multer 模块将用户上传的图像文件直接上传到我的存储桶中。该应用程序显示更新成功,但我在存储桶中看不到我的文件。上传文件后的页面响应上传成功。下面是我正在使用的 app.js 文件和 index.html,这是从 github repo 派生的,人们说它可以工作,但我的不工作,请告诉我错误?我在我的本地主机上运行它。

代码:

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

索引.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>
4

1 回答 1

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 上传示例

于 2017-05-11T21:03:42.127 回答