Req.file在VM上未定义,在localhost和Heroku上运行良好

时间:2019-06-14 07:43:02

标签: javascript node.js file-upload multer-s3

我有一个代码可以将图像上传到S3。该代码在我的本地计算机和Heroku上正常工作,但是当我在VM(centos)上部署它时,它将停止工作。根据错误,未定义req.file。我在这里拔头发。我究竟做错了什么?任何建议,将不胜感激。

这是我编写的代码


const _s3ImageUpload = (bucketName, acl, moduleName = "common") => {
    const s3 = new aws.S3()
    return multer({
        storage: multerS3({
            s3: s3,
            bucket: bucketName,
            acl: acl,
            contentType: multerS3.AUTO_CONTENT_TYPE,
            "Content-Disposition": "inline;",
            metadata: function(req, file, cb) {
                cb(null, { fieldName: file.fieldname, originalName: file.originalname })
            },
            key: function(req, file, cb) {
                cb(null, `${file.mimetype.split("/")[1]}`)
                cb(null, `${moduleName}/${imageUUID()}.${file.mimetype.split("/")[1]}`)
            }
        }),
        limits: { fileSize: 50000000 }
    })
}

const uploadImage = async (req) => {
    const upload = _s3ImageUpload("api-dev", "public-read", "event", req)
    const singleUpload = upload.single("image")
    return new Promise((resolve, reject) => {
        singleUpload(req, {}, (err) => {
            if (err)
                //logger.error(err);return;
                //reject({message: "Image upload failed on S3", status: 500})
                reject(new ApiError({ message: "Image upload failed on S3", status: HttpStatus.NOT_FOUND }))

            logger.info("Here file value is ", req.file)
            const imageKey = req.file.key
            const imageUrl = s3BaseUrl + imageKey
            try {
                resolve({ imageKey, imageUrl })
            } catch (err) {
                reject({ message: "Error occurred while updating user profile", status: 500 })
            }
        })
    })
}

这是VM上的错误屏幕截图

enter image description here

0 个答案:

没有答案