简体   繁体   English

如何返回不在 base64 中的图像

[英]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.

相关问题 base64 图像损坏上传到 S3 - base64 image corrupted uploading to S3 节点JS | 谷歌视觉 API | 将 base64 发送到 Google Cloud Vision 时出现“错误的图像数据” - Node JS | Google Vision API | "Bad image data" when sending base64 to Google Cloud Vision base64 编码 io.Reader - base64 encode io.Reader 为什么将 base64 图像存储在 Firestore 中被认为是个坏主意? - Why is storing base64 images in firestore considered a bad idea? Twilio Base64 谷歌语音到文本的媒体有效负载 API 无响应 - Twilio Base64 Media Payload for Google Speech To Text API not Responding 通过 ApiGateway 下载二进制文件 (nosql db) 由于配置错误导致执行失败:无法对正文进行 base64 解码 - Download binary (nosql db) through ApiGateway gives Execution failed due to configuration error: Unable to base64 decode the body 如何为 firestore 集合中的每个文档返回一个带有图像元素的 div - How to return a div with an image element for each document in a firestore collection 如何在浏览器中使用 Java Web 服务返回图像或文件 - How to return an image or file using Java web service in Browser 将 []float64 像素切片转换为图像 - Convert []float64 pixel slice to an Image AWS API Gateway 和 Lambda 返回图像 - AWS API Gateway and Lambda to return image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM