Amazon Lambda Java函数将png返回到API网关 [英] Amazon Lambda Java function returning png to API gateway
问题描述
我需要将PNG从Amazon Lambda Java函数返回到Amazon Rest API.我已经用如下函数原型构建了函数:
I have a requirement to return a PNG from an Amazon Lambda Java Function to a Amazon Rest API. I have built the function with a function prototype like this:
public static void generatePng(InputStream request, OutputStream response, Context context)
InputStream请求详细说明了需要写入图像的信息. OutputStream响应是发回的内容,是png图像.我已经测试了代码,并且知道编码到输出流中的图像是正确的,因为我的测试将其写入文件,可以在图像编辑器中看到该文件.
The InputStream request details the information that needs to be written into the image. The OutputStream response is what is sent back and is a png image. I have tested the code and know that the image encoded in to the output stream is correct as my test writes this to a file which I can see in an image editor.
我遇到的问题是从Lambda函数到API网关的响应布线.这些文档专门讨论String响应,但是我的响应有效载荷是二进制图像数据.
The issue I have is in the wiring of the response from the Lambda Function to the API gateway. The documents talk exclusively of a String response but my response payload is binary image data.
在Lambda集成响应中,我尝试添加以下项的标头映射:
In the Lambda Integration Response I have tried adding header mappings of:
Content-Length集成.response.header.Content-Length
Content-Type integration.response.header.Content-Type
Content-Length integration.response.header.Content-Length
Content-Type integration.response.header.Content-Type
以及以下内容的主体映射: 内容类型:image/png,模板为$ input.body
and a body mapping of: content-type: image/png with a template of $input.body
我还在方法响应"部分中添加了image/png响应模型.
I have also added a response model of image/png in the Method Response section.
在测试API时,对API调用的响应不会显示图片, 而是以适当的png标头开头的图像的字符串转储:
On testing the API, the response to the API call does not show the image though, but rather a string dump of the image starting with the proper png header:
例如:响应正文
?PNG
IHDR?
??~LIDATx? ...
响应头确实显示: {"Content-Type":"image/png"}
The response headers do show: {"Content-Type":"image/png"}
有人知道如何配置Amazon API网关以接受图像作为lambda函数调用的输出,并将其正确地呈现给调用者吗?
Does anybody know how to configure Amazon API gateway to accept an image as the output of a lambda function call and render it back to the caller properly?
推荐答案
API Gateway doesn't support binary data - https://forums.aws.amazon.com/search.jspa?objID=f199&q=binary&x=0&y=0.
您可以考虑在映射模板中使用base64编码/解码功能. 链接
You could consider using the base64 encode/decode feature in the mapping template. Link
这篇关于Amazon Lambda Java函数将png返回到API网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!