使用Multer-S3 Node.js将图像上传到Amazon S3
我正在尝试使用将图片上传到亚马逊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!");
});
为什么我收到此错误?
回答:
并正常工作的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_s3,node app
然后运行npm install express
body-parser aws-sdk multer multer-s3。
快乐的帮助!
以上是 使用Multer-S3 Node.js将图像上传到Amazon S3 的全部内容, 来源链接: utcz.com/qa/399867.html