Season's greetings,
We have a lambda that:
Step 1: Downloads image from S3
Step 2: Resizes and formats the image using sharp
Step 3: Return a 200 response with the image in body
like so:
const sharpPipe = sharp()
.resize(300, 300)
.toFormat('webp');
const getParams = {
Bucket: myBucket,
Key: imageKey
}
const s3Response = await s3Client
.send(new GetObjectCommand(getParams));
const bodyStream = s3Response.Body;
const imageBuffer = await bodyStream.pipe(sharpPipe).toBuffer();
response = {
statusCode: 200,
headers: {
"Content-Type": 'image/webp',
},
body: imageBuffer,
isBase64Encoded: false,
};
However, this does not render when you hit the Lambda function URL in Chrome or Postman.
I know I can convert the image to base64
and that works fine but it slows down the API significantly and that is an issue in this case.
What other format, that is fast to convert to, can I return the image in that will be rendered by a browser or Postman?
P.S. I did try application/octet-stream
like so:
response = {
statusCode: 200,
headers: {
"Content-Type": `application/octet-stream`
},
body: imageBuffer.toString('binary'),
isBase64Encoded: false,
};
But that returned gibberish