是否可以使用帖子正文发布到 SQS 网址 [英] Is it possible to POST to the SQS url using the post body

查看:26
本文介绍了是否可以使用帖子正文发布到 SQS 网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://docs.aws.amazon.com/AWSSimpleQueueService/最新/APIReference/API_SendMessage.html

https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue/
?Action=SendMessage
&MessageBody=This+is+a+test+message

这工作正常,但是是否可以使用帖子正文发送有效负载?

This works fine, however is it possible to send the payload using the post body?

我有一个外部服务在帖子正文中发送 json 有效负载(以非常高的吞吐量).如果我可以简单地直接向他们提供 SQS url,并且不必创建 AWS api 网关 --> lambda --> SQS 堆栈,那将是理想的.

I have an external service sending json payloads in the post body (at a very high throughput). It would be ideal if I can simply feed them the SQS url directly, and avoid having to create a AWS api gateway --> lambda --> SQS stack.

我愿意接受替代解决方案.

I'm open to alternative solutions.

推荐答案

这可能不是完全 API Gateway 的设计者所想的,但是给...

This may not have been exactly what the designers of API Gateway had in mind, but given...

  • AWS 查询 API(由开发工具包使用)在网络上为 SQS 等服务工作的方式——标准 application/x-www-form- 中的 url 编码键/值对urlencoded 格式,和

API 网关 (VTL) 中内置的主体映射模板语言公开了 $util.urlEncode()

the fact that the built-in body mapping template language in API Gateway (VTL) exposes $util.urlEncode(), and

API Gateway 可以在将请求发送到后端服务时对其进行透明签名的事实

the fact that API Gateway can transparently sign requests when sending them to a backend service

...这意味着您可以简单地手工构造一个有效的 API 请求——一个表单帖子——就像使用 VTL 模板一样,它封装了原始消息体——urlencoded,这就是我们所需要的.我们实际上并不关心它是 JSON,只要它是有效的字符数据.

...this means you can simply construct a valid API request -- a form post -- by hand, as were, using a VTL template, that encapsulates the original message body -- urlencoded, and that's all we need. We don't actually care that it's JSON, as long as it is valid character data.

使用此解决方案,避免了 Lambda 函数,并且客户端无需了解 SQS 期望消息的格式.

With this solution, a Lambda function is avoided, and the client does not need to know anything about how SQS expects messages to be formatted.

整个传入请求正文成为 SQS 中的 Message.

The entire incoming request body becomes the Message in SQS.

在 API Gateway 控制台中:

In the API Gateway console:

创建资源(例如/)和方法(例如POST).

Create the resource (e.g. /) and the method (e.g. POST).

在集成请求设置中:

Integration type: AWS Service
AWS Region: [select your region]
AWS Service: Simple Queue Service (SQS)
AWS Subdomain [leave blank]
HTTP Method: POST
Action Type: Use path override
Path override: /
Execution Role: [your role ARN, needs to be able to send a message to the queue]
Credentials cache: Do not add caller credentials to cache key
Content Handling: Passthrough

HTTP Headers下,添加一个header,Content-Type.此值应指定为映射自"'application/x-www-form-urlencoded' -- 请注意,这是一个单引号字符串.

Under HTTP Headers, add one header, Content-Type. This value should be specified as being "mapped from" 'application/x-www-form-urlencoded' -- note that this is a single-quoted string.

Body Mapping Templates 下,选择 Never.

添加 application/jsonContent-Type 并使用以下映射模板:

Add a Content-Type of application/json and use the following mapping template:

Action=SendMessage##
&QueueUrl=$util.urlEncode('https://sqs.us-east-2.amazonaws.com/000000000000/my-queue-name')##
&MessageBody=$util.urlEncode($input.body)##

您有一个 API,可以将原始 JSON 输入正文转换为 SQS SendMessage API 请求.

And you have an API that transforms the a raw JSON input body into an SQS SendMessage API request.

每行末尾的 ## 是为了便于阅读 -- VTL 是一种文本模板语言,因此保留了空格和换行符.将 ## 放在每行的末尾会导致换行符被剥离,这是构建正确的 Web 表单所必需的.否则,整个身体映射模板将需要在一行上.

The ## at the end of each line are for readability -- VTL is a text templating language, so whitespace and newlines are preserved. Placing ## at the end of each line causes the newline to be stripped, which is necessary for building a correct web form. Otherwise, the entire body mapping template would need to be on a single line.

部署然后测试:

$ curl -X POST https://xxxxxxxxxx.execute-api.us-east-2.amazonaws.com/v1 --data '{"works": true}' -H 'Content-Type: application/json'

回复:

{"SendMessageResponse":{"ResponseMetadata":{"RequestId":"bbbbbbbb-aaaa-5555-8888-334ed25bb6b3"},"SendMessageResult":{"MD5OfMessageAttributes":null,"MD5OfMessageBody":"81f2ecc3cb027268138bdfe7af0f8a3f","MessageId":"cccccccc-dddd-4444-1111-542e08bb39af","SequenceNumber":null}}}

额外的功劳,集成响应中的主体映射模板也可以重复用于自定义响应.

For extra credit, a body mapping template in the Integration Response can be reused to customize the response, as well.

这篇关于是否可以使用帖子正文发布到 SQS 网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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