我在用户编辑个人资料页面上将图像上传到 s3,它可以工作,但是在图像保存在 s3 后,页面在保存更改后重新加载。
找不到图像,但是,如果我刷新它的页面,那里...
任何想法可能是什么问题?
这在控制器中应用:
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.file) {
next(); // skip to the next middlewaree
return;
}
const extension = req.file.mimetype.split('/')[1]
req.body.photo = `${uuid.v4()}.${extension}`
let readyimg
const imageAws = await sharp(req.file.buffer)
.resize(800, 800)
.toBuffer()
.then( data => {
readyimg = data
})
AWS.config.update({
secretAccessKey: process.env.SECRETACCESSKEY,
accessKeyId: process.env.ACCESSKEYID,
region: 'us-east-1'
})
const s3 = new AWS.S3()
const params = {
Bucket: 'jamsession-images',
Key: req.body.photo,
Body: readyimg
};
await s3.upload(params, function (err, data) {
if (err) {
console.log('%%%%%%%%%%%%%%% error in callback');
console.log(err);
}
console.log('****************** success');
console.log(data);
});
next()
};