简体   繁体   English

使用 Node.js 和 Sharp 从 AWS S3 调整图像大小

[英]Resizing images from AWS S3 with Node.js and Sharp

I am trying to create an endpoint on my API where I can resize images from S3 on-demand.我正在尝试在我的 API 上创建一个端点,我可以在其中按需调整 S3 中的图像大小。 I have got it working but it takes roughly 7s to return the resized image compared to roughly 800ms to return the image without resizing.我已经让它工作了,但是返回调整大小的图像大约需要 7 秒,而在不调整大小的情况下返回图像大约需要 800 毫秒。 I am new to manipulating images and decided to use the sharp package as it seemed quite simple to use.我是处理图像的新手,并决定使用锐利的 package,因为它看起来很容易使用。 I was wondering if anyone could tell me if I am doing something wrong?我想知道是否有人可以告诉我我做错了什么? Or a way to speed it up?或者有什么方法可以加快速度? Or is sharp just slow?还是尖锐只是慢?

My code is:我的代码是:

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)

Thanks in advance!提前致谢!

Try to remove the Content-Length header:尝试删除Content-Length header:

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

When I tested your code this improved the performance.当我测试您的代码时,这提高了性能。

Check if you observe the same improvement.检查您是否观察到相同的改进。

do you want the answer or not?你想要答案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM