使用 multer-s3 nodejs 将图像上传到亚马逊 s3 [英] Uploading image to amazon s3 using multer-s3 nodejs

查看:36
本文介绍了使用 multer-s3 nodejs 将图像上传到亚马逊 s3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 multer-s3 将图像上传到 amazon 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?

推荐答案

完成 和工作 节点作弊 |使用可用的 multer-s3 上传到 s3.

Complete and working Node Cheat | Upload to s3 using multer-s3 available.

代码:

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

对于完整的回购:

克隆节点作弊 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 nodejs 将图像上传到亚马逊 s3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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