【问题标题】:Resizing images from AWS S3 with Node.js and Sharp使用 Node.js 和 Sharp 从 AWS S3 调整图像大小
【发布时间】:2022-02-02 23:20:14
【问题描述】:

我正在尝试在我的 API 上创建一个端点,我可以在其中按需调整来自 S3 的图像大小。我已经让它工作了,但是返回调整大小的图像大约需要 7 秒,而在不调整大小的情况下返回图像大约需要 800 毫秒。我是处理图像的新手,并决定使用Sharp包,因为它看起来很简单。我想知道是否有人可以告诉我我做错了什么?或者有什么方法可以加快速度?还是尖锐只是慢?

我的代码是:

const getCommand = new GetObjectCommand({
    Bucket: 'mybucket',
    Key: 'myfile'
});
const getResponse = await s3Client.send(getCommand);

// Set response headers
response.set('Content-Length', getResponse.ContentLength);
response.set('Content-Type', getResponse.ContentType);
response.statusCode = 200;

// Stream response
getResponse.Body.pipe(response);
// GetResponse.Body.pipe(sharp().resize(40, 40)).pipe(response)

提前致谢!

【问题讨论】:

  • 您处理多大的图像?夏普非常快。
  • @Jax-p 问题中的速度来自 106kb png 文件的图像。

标签: javascript node.js amazon-s3 image-processing sharp


【解决方案1】:

尝试删除 Content-Length 标头:

response.set('Content-Length', getResponse.ContentLength);

当我测试您的代码时,这提高了性能。

检查您是否观察到相同的改进。

【讨论】:

  • 太棒了,这成功了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 2014-06-29
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多