代码之家  ›  专栏  ›  技术社区  ›  KAT

如何使用带节点的graphicsmagick将新的图像文件发送到AWS S3?

  •  0
  • KAT  · 技术社区  · 6 年前

    我试图上传一个图像到S3,但我一直发现手机图像有旋转的图像,我知道这是由于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;
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Basim Hennawi    6 年前

    gm 文档:

    图像输出

    -将处理的图像数据写入指定的文件名

    流动 -为可读流提供处理的图像数据

    缓冲区 -将图像作为缓冲区而不是流返回

    所以在你的情况下,而不是 .write() 你可以使用:

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

    现在你得到 buffer 可用于 Body 上传到S3逻辑