使用Multer-S3 Node.js将图像上传到Amazon S3 [英] Uploading image to amazon s3 using multer-s3 nodejs

查看:421
本文介绍了使用Multer-S3 Node.js将图像上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to upload an image to amazon s3 using multer-s3, but I am getting this error:

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

TypeError: Expected opts.s3 to be object 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!");
});

为什么我会收到此错误?

Why I am getting this error?

推荐答案

完成,然后运行代码:

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.

Clone node-cheat express_multer_s3, run node app followed by npm install express body-parser aws-sdk multer multer-s3.

祝您工作愉快!

这篇关于使用Multer-S3 Node.js将图像上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆