user18079786
user18079786

Reputation:

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. 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. I am new to manipulating images and decided to use the sharp package as it seemed quite simple to use. 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!

Upvotes: 1

Views: 1336

Answers (1)

Try to remove the 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.

Upvotes: 1

Related Questions