是否可以使用发布正文发布到SQS网址 [英] Is it possible to POST to the SQS url using the post body
问题描述
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网址,而不必创建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网关的设计者所想的,但是给出了...
This may not have been exactly what the designers of API Gateway had in mind, but given...
-
AWS Query API(SDK使用的)为SQS之类的服务在线工作的方式-标准
application/x-www-form-urlencoded
格式和
API Gateway(VTL)中的内置主体映射模板语言公开了$util.urlEncode()
和
the fact that the built-in body mapping template language in API Gateway (VTL) exposes $util.urlEncode()
, and
API网关在将请求发送到后端服务时可以透明地签署请求的事实
the fact that API Gateway can transparently sign requests when sending them to a backend service
...这意味着您可以像以前一样使用VTL模板手动构造一个有效的API请求(一个表单发布),其中封装了原始消息正文(采用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网关控制台中:
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
下,添加一个标头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/json
的Content-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屋!