3

我尝试将文件从 NodeJS 上传到我的 S3 存储桶。下一个代码工作正常。

./routes/lessons.js

 const upload = require('../models/model.lessons');

 router.post('/upload9', upload.array('file', 1), (req, res) => {
        console.log('Uploaded');
        res.send({image: req.file});
    });

./models/model.lessons.js

    const upload = multer({
    storage: multerS3({
        acl: 'public-read',
        s3,
        bucket: config.get('AWS.bucket_audios'),
        key: function (req, file, cb) {
            req.file = Date.now() + file.originalname;
            cb(null, Date.now() + file.originalname);
        }
    })
})

我的问题是我想在上传前后做一些额外的事情。当我尝试在 model.lessons.js 代码中移动 upload.array 时,出现错误。我究竟做错了什么 ?

./routes/lessons.js

router.post('/upload', async function (req, res, next) {
try {
    let result = await Lessons.upload2(req, res);
    console.log('uploaded');
    await res.send(result);
} catch (err) {
    console.log(err);
    res.status(400).json(err);
}

});

./models/model.lessons.js

   upload2: async function (req, res) {

    // some code 

    var result = await multer({
        storage: multerS3({
            acl: 'public-read',
            s3,
            bucket: config.get('AWS.bucket_audios'),
            key: function (req, file, cb) {
                req.file = Date.now() + file.originalname;
                cb(null, Date.now() + file.originalname);
            }
        })
    }).array('file', 1);

    // some code 

    return true;

},
4

0 回答 0