如何将 GraphicsMagick 与节点一起使用以将新图像文件发送到 AWS S3?

How to use GraphicsMagick with node to send a new image file to AWS S3?

我正在尝试将图像上传到 S3,但我一直发现 phone 图像旋转了图像,我了解到这是由 EXIF 数据引起的。我发现了这个名为 graphicsmagick 的库,它声称能够去除 EXIF 数据,我还决定将图像大小减小到 500px 宽和任何高度结果。 我无法弄清楚的问题是如何在更改文件后获取文件。似乎所有 graphicsmagick 示例都显示将图像写入磁盘上的文件,但我想抓取文件数据并将其上传到 AWS S3。

到目前为止我有:

let file_extension = file.name.split('.').pop();//grab the file extension of for saving in the db
        let key = `${user_id}/${UUID()}.${file_extension}`; //create a unique key to save in S3 based on users id
        let params = {Bucket: S3_name, Key: key, Body: file.data};

        //resize image
        let new_image = gm(file.data)
            .resize(500)
            .noProfile()
            .write() <-- this is as far as I got.

        //upload
        let result = new Promise(resolve=>{
            s3.upload(params, function(err, result){
                if (err) {
                    throw new Error('Could not upload photo');
                }else {
                    resolve(result);
                }
            })
        });

        result = await result;

来自 gm 文档:

Image output

write - writes the processed image data to the specified filename

stream - provides a ReadableStream with the processed image data

toBuffer - returns the image as a Buffer instead of a stream

因此在您的情况下,您可以使用:

而不是 .write()
.toBuffer('png',function (err, buffer) {
  if (err) return throw err;
  console.log('done!');
})

现在你得到了 buffer 可以用作 Body 上传到 S3 逻辑