通过API网关从AWS Lambda返回二进制主体和HTTP标头 [英] Returning binary body and http headers from an AWS lambda through API gateway

查看:109
本文介绍了通过API网关从AWS Lambda返回二进制主体和HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个lambda,需要通过api网关(使用lambda集成)返回二进制对象和一些http标头(例如content-type),或者重定向到另一个URL.在二进制支持示例中(例如

I have a lambda that needs to return a binary object and some http headers (e.g. content-type) through an api gateway (using lambda integration) OR redirect to another URL. In the binary support examples (e.g. https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/) the lambda only returns the (base64 of the) binary object (the image). In my case, I also need to return a status code and http headers (or something equivalent). I struggle with how I can make this work with binary support in api gateway.

lambda会以以下形式返回json:

The lambda returns a json on this form:

{
  "statusCode": 200,
  "headers": {
    "content-type": "image/jpeg"
  },
  "body": "/9j/4AAQS...gLDAoKCAwZK",
  "isBase64Encoded": true
}

在集成响应中,我添加了图像/jpeg(等)形式的主体映射:

In the integration response I add body mappings for image/jpeg (etc) of the form:

$input.json('$.body')

内容类型"的标头映射如下:

And header mapping for 'content-type' like so:

integration.response.body.headers['content-type']

我尝试了上述方法的许多变体,但结果始终如一

I've tried many variations of the above, but the result is consistently

Execution failed due to configuration error: Unable to transform response 

如何将lambda中的json转换为可通过api网关转换为带有HTTP标头和所有标头的形式?我可以从api网关中获取更多的调试日志,以更具体地显示它不满意的地方吗?

How do I transform the json from the lambda into a form that can be converted to binary by the api gateway, with http headers and all? Can I get more debug logging out of the api gateway to show more specific what it is unhappy with?

也许有办法让更多的调试日志退出api网关吗?

Is there perhaps a way to get more debug logging out of the api gateway?

推荐答案

在遇到类似问题后,我最近开始工作了.

I recently got this working after facing a similar problem.

就我而言,我缺少两件事:

In my case, I was missing two things:

首先,我需要在"Accept"标头中更改AWS将发送到上游的类型的列表.

First, I needed to change the list of types AWS will send to the upstream in the "Accept" header"

"x-amazon-apigateway-binary-media-types" : [
  "image/jpeg"
]

第二,我需要将集成响应"设置为转换为二进制(如果需要)":

Secondly, I needed to set the Integration Response to "Convert to binary (if needed)":

"contentHandling": "CONVERT_TO_BINARY"

有关详细信息和示例配置,请参见此答案.

See this answer for the details, and sample config.

我还发现我没有足够的耐心.每当我部署API时,我都会立即进行检查,而不是等待几分钟来传播更改.

I also found that I wasn't being patient enough. Whenever I deployed the API, I was checking immediately, instead of waiting a few minutes for the changes to propagate.

这篇关于通过API网关从AWS Lambda返回二进制主体和HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆