[英]AWS S3 Image uploaded is corrupted
when uploadingimages, If i use the data thats in my req.file.buffer
which is an array of numbers.. the buffer.当上传图像时,如果我使用我的req.file.buffer
的数据,它是一个数字数组......缓冲区。 It uploads correctly the image to aws s3.它将图像正确上传到 aws s3。
But i need to resize theimage before... so im trying to use jimp, like so:但是我需要在之前调整图像大小...所以我尝试使用 jimp,如下所示:
const photo = await jimp.read(req.file.buffer)
await photo.cover(300, 300);
And then pass it to aws settings:然后将其传递给 aws 设置:
const s3 = new AWS.S3()
const params = {
Bucket: 'jamsession-images',
Key: req.body.photo,
// here in body is a buffer just like the one in req.file.buffer
Body: photo.bitmap.data
};
s3.upload(params, function (err, data) {
if (err) {
console.log(err);
}
console.log('****************** success');
});
But if i do this.. it uploads the image to aws s3.. but the image is corrupted但是如果我这样做..它会将图像上传到aws s3..但图像已损坏
What im i doing here?我在这里做什么? i think aws s3 needs in the budy a buffer... and i think after jimp finished scaling the image.. that new buffer would work.. but it doesnt.. any ideas?我认为 aws s3 在伙伴中需要一个缓冲区......我认为在 jimp 完成缩放图像后.. 那个新的缓冲区可以工作......但它没有......有什么想法吗?
Full code:完整代码:
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}`
// now we resize
const photo = await jimp.read(req.file.buffer)
await photo.cover(300, 300);
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,
// this line seems to be the issue..
// even though photo.bitmap.data its also a buffer
Body: photo.bitmap.data
};
s3.upload(params, function (err, data) {
if (err) {
console.log('%%%%%%%%%%%%%%% error in callback');
console.log(err);
}
console.log('****************** success');
console.log(data);
});
// await photo.write(`./public/uploads/${req.body.photo}`);
// once we have written the photo to our filesystem, keep going!
next()
};
I had have this problem too, to get the correct buffer of the result image we have to use Jimp's getBuffer function.我也有这个问题,为了获得结果图像的正确缓冲区,我们必须使用 Jimp 的 getBuffer 函数。
image.getBuffer(mime, cb);
Supported MIME types支持的 MIME 类型
Jimp.MIME_PNG; // "image/png"
Jimp.MIME_JPEG; // "image/jpeg"
Jimp.MIME_BMP; // "image/bmp"
But with Jimp.AUTO can have the mime type of the original image and use it.但是使用 Jimp.AUTO 可以拥有原始图像的 mime 类型并使用它。
You can read more of getBuffer function in https://www.npmjs.com/package/jimp您可以在https://www.npmjs.com/package/jimp 中阅读更多 getBuffer 函数
photo.getBuffer(Jimp.AUTO, function(error, result){
const params = {
Bucket: 'jamsession-images',
Key: req.body.photo,
// correct buffer
Body: result
};
s3.upload(...);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.