[英]How to return image NOT in base64
Season's greetings,季节的问候,
We have a lambda that:我们有一个 lambda:
Step 1: Downloads image from S3第 1 步:从 S3 下载图像
Step 2: Resizes and formats the image using sharp第 2 步:使用锐利调整图像大小和格式
Step 3: Return a 200 response with the image in body第 3 步:返回 200 响应,正文中包含图像
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.但是,当您在 Chrome 或 Postman 中点击 Lambda 函数 URL 时,这不会呈现。
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.我知道我可以将图像转换为base64
,并且工作正常,但它会显着降低 API 速度,这在这种情况下是一个问题。
What other format, that is fast to convert to, can I return the image in that will be rendered by a browser or Postman?可以快速转换成什么其他格式,我可以返回将由浏览器或 Postman 呈现的图像吗?
PS I did try application/octet-stream
like so:附言我确实像这样尝试application/octet-stream
:
response = {
statusCode: 200,
headers: {
"Content-Type": `application/octet-stream`
},
body: imageBuffer.toString('binary'),
isBase64Encoded: false,
};
But that returned gibberish但这返回了乱码
AWS has an example ( Return binary media from a Lambda proxy integration ) that does in fact recommend converting to base64. AWS 有一个示例( 从 Lambda 代理集成返回二进制媒体)实际上建议转换为 base64。 I believe this step may be necessary because of the way the request gets passed through other services (such as API Gateway) before getting sent to the browser.我认为这一步可能是必要的,因为请求在发送到浏览器之前会通过其他服务(例如 API 网关)。
Following that example, you would set the body
to the base64-encoding of the image binary, and set isBase64Encoded
to true
.按照该示例,您可以将body
设置为图像二进制文件的 base64 编码,并将isBase64Encoded
设置为true
。
To improve performance , consider changing your process to save the resized image back to S3 and not serving it through Lambda on future requests.为了提高性能,请考虑更改您的流程以将调整大小后的图像保存回 S3,而不是通过 Lambda 为将来的请求提供服务。 AWS has a blog post describing how to set this up: AWS 有一篇博文描述了如何进行设置:
Resize Images on the Fly with Amazon S3, AWS Lambda, and Amazon API Gateway 使用 Amazon S3、AWS Lambda 和 Amazon API Gateway 即时调整图像大小
This way, any slight performance hit in the resize step only affects the image the first time it is needed.这样,调整大小步骤中的任何轻微性能影响只会在第一次需要时影响图像。 Subsequent requests for it are served as fast as any other images served directly from S3.对它的后续请求与直接从 S3 提供的任何其他图像一样快。
To further improve performance , you could consider proactively generating the resized image as soon as the original is uploaded to S3.为了进一步提高性能,您可以考虑在原始图像上传到 S3 后立即主动生成调整大小的图像。 This way, even the first request for the resized image will be fast.这样,即使是对调整大小的图像的第一个请求也会很快。 You could do this programmatically in the code that uploads the original image.您可以在上传原始图像的代码中以编程方式执行此操作。 Or you could set up a trigger to execute a Lambda whenever a new image is uploaded.或者,您可以设置触发器以在上传新图像时执行 Lambda。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.