一尘不染

使用Multer-S3 Node.js将图像上传到Amazon S3

node.js

我正在尝试使用将图片上传到亚马逊s3 multer-s3,但出现此错误:

TypeError:预期opts.s3为对象node_modules / multer-s3 / index.js:69:20

这是我的服务器代码:

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

为什么我收到此错误?


阅读 364

收藏
2020-07-07

共1个答案

一尘不染

完成 并正常工作的Node Cheat |
使用multer-s3上传到s3

码:

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

对于完整的回购

克隆node-cheat express_multer_s3node app然后运行npm install express body-parser aws-sdk multer multer-s3

快乐的帮助!

2020-07-07