我正试图用我的nodejs后端将一个图像上传到s3,但我无法让它工作。
这就是我得到的错误:
InvalidParameterType: Expected params.Key to be a string
2021-07-25T15:07:10.575922+00:00 app[web.1]: at ParamValidator.fail (/app/node_modules/aws-sdk/lib/param_validator.js:50:37)
2021-07-25T15:07:10.575923+00:00 app[web.1]: at ParamValidator.validateType (/app/node_modules/aws-sdk/lib/param_validator.js:232:10)
2021-07-25T15:07:10.575923+00:00 app[web.1]: at ParamValidator.validateString (/app/node_modules/aws-sdk/lib/param_validator.js:154:32)
2021-07-25T15:07:10.575923+00:00 app[web.1]: at ParamValidator.validateScalar (/app/node_modules/aws-sdk/lib/param_validator.js:130:21)
2021-07-25T15:07:10.575924+00:00 app[web.1]: at ParamValidator.validateMember (/app/node_modules/aws-sdk/lib/param_validator.js:94:21)
2021-07-25T15:07:10.575924+00:00 app[web.1]: at ParamValidator.validateStructure (/app/node_modules/aws-sdk/lib/param_validator.js:75:14)
2021-07-25T15:07:10.575925+00:00 app[web.1]: at ParamValidator.validateMember (/app/node_modules/aws-sdk/lib/param_validator.js:88:21)
2021-07-25T15:07:10.575925+00:00 app[web.1]: at ParamValidator.validate (/app/node_modules/aws-sdk/lib/param_validator.js:34:10)
2021-07-25T15:07:10.575925+00:00 app[web.1]: at Request.VALIDATE_PARAMETERS (/app/node_modules/aws-sdk/lib/event_listeners.js:132:42)
2021-07-25T15:07:10.575926+00:00 app[web.1]: at Request.callListeners (/app/node_modules/aws-sdk/lib/sequential_executor.js:106:20) {
2021-07-25T15:07:10.575926+00:00 app[web.1]: code: 'InvalidParameterType',
2021-07-25T15:07:10.575927+00:00 app[web.1]: time: 2021-07-25T15:07:10.571Z
2021-07-25T15:07:10.575927+00:00 app[web.1]: }
这是我的uploadfile()函数:
const bucketName = process.env.AWS_BUCKET_NAME;
const region = process.env.AWS_BUCKET_REGION;
const accessKeyId = process.env.AWS_ACCESS_KEY;
const secretAccessKey = process.env.AWS_SECRET_KEY;
const s3 = new S3({
region,
accessKeyId,
secretAccessKey,
});
// uploads a file to s3
function uploadFile(file) {
const fileStream = fs.createReadStream(file.path);
const uploadParams = {
Bucket: bucketName,
Key: file.filename,
Body: fileStream,
};
return s3.upload(uploadParams).promise();
}
exports.uploadFile = uploadFile;
它使用multer中间件获取文件,该中间件工作正常,我可以使用console.log()查看该文件。这是控制器:
const uploadAvatar = async (req, res) => {
try {
const id = req.user;
const file = req.file;
const result = await uploadFile(file);
await unlinkFile(file.path);
await knex('users').where('id', id).update({
avatar_url: result.Key,
});
return res.status(200).send({ msg: 'Photo has been uploaded.' });
} catch (err) {
return res.status(500).send({ msg: err.message });
}
};
//route
router.post('/upload-avatar', auth, upload.single('image'), customerController.uploadAvatar);
我不明白我做错了什么,我试着把钥匙改成“测试”,但没有成功。我也找不到相关的问题。
任何帮助都将不胜感激。
暂无答案!
目前还没有任何答案,快来回答吧!