如何在 multer s3 中获取 req.body 参数

How to get req.body parameters in multer s3

var upload = multer({
storage: multerS3({
    s3: s3,
    bucket: 'bucket',
    metadata: function(req, file, cb) {
        cb(null, {
            fieldName: file.fieldname
        });
    },
    key: function(req, file, cb) {
        console.log('req.body', req.params.id); //not getting
        console.log('req.body', req.body);
        //Not getting param here that passed in api 
        //Need to save file on s3 at specific location i.e /foldername/filename
        //But the folder name not getting from API
        cb(null, file.originalname)
    }
})  }).array('userFile', 1);

以上是multer s3代码

app.post('/saveData', function(req, res, next) {
upload(req, res, function(err) {
    console.log('err' + err);
    var status = '';
    var result = '';
    var link = '';
    if (err) {
        status = false;
    } else {
        status = true;
    }
    result = {
        "status": status,
        "link": link
    }
});
res.send(result);   });

以上代码调用了多个上传功能。我正在解析 API 中的数据(来自 Angular2,设置 -Content-Type": "multipart/form-data)作为 formdata

let formData: FormData = new FormData();
formData.append('userFile', file);
formData.append('fileName', fileName);

我需要 API 中的 req.body 数据,例如文件夹名称和其他,这样我就可以将文件放到 S3 上的特定位置。密钥中需要 req.body 数据:function(req, file, cb) { of multerS3.

阅读 dropzone.js 的源代码,我发现问题是 S3 期望该文件是表单数据的最后一个参数

如果你的文件输入是表单的最后一个输入,你也应该尝试在创建 FormData() 的新实例时传递表单标签的引用

<form
    id="myForm"
    className="col-md-10 col-md-offset-1"
>

所以像这样传递 id:new FormData(document.getElementById(myForm)

那么你应该在你的关键回调函数 req.body 中找到你的表单的输入值

这似乎是multer的一个错误,但正如这里所说,您可以在发送时重新排序参数。

https://github.com/expressjs/multer/issues/322#issuecomment-191642374