如何在SNS HTTPS订阅终结点中访问消息属性 [英] How to access message attributes in SNS HTTPS subscription endpoint
问题描述
我正在设置一个集成测试,以确定向SNS发布消息的程序是否正常运行.发布的消息包括一些重要的消息属性,我需要检查这些属性以确定正确性.
I'm setting up an integration test to determine if a program that publishes a message to SNS is working correctly. The published message includes a few critical message attributes that I need to check in order to determine correctness.
到目前为止,我已经建立了一个充当SNS订阅者的简单HTTP服务器.服务器正确地确认了它的订阅,甚至从该主题接收消息,但是我对如何访问这些消息的属性一无所知.
So far, I have set up a simple HTTP server that acts as an SNS subscriber. The server correctly confirms its subscription and even receives messages from the topic, however I am at a loss as to how to access the attributes of these messages.
应该提到的是,我正在使用ngrok将主机暴露给SNS.为此,我使用命令:
It should be mentioned that I am using ngrok to expose my host to SNS. To accomplish this, I'm using the command:
ngrok http 8080
创建订阅时,请确保启用原始消息传递:
When I create the subscription, I have made sure to enable raw message delivery:
subOut, err := snsCli.Subscribe(&sns.SubscribeInput{
Endpoint: &ngrokURL,
Attributes: map[string]*string{
"RawMessageDelivery": aws.String("True"),
},
Protocol: aws.String("https"),
TopicArn: aws.String(topicArn),
})
注意:我还尝试将"RawMessageDelivery"设置为"true",而不是"True".这似乎没有引起任何变化.
Note: I have also tried setting "RawMessageDelivery" to "true" instead of "True." This does not seem to elicit any changes.
我还尝试使用 http
协议代替 https
,但是获得了相同的结果.
I have also tried using the http
protocol instead of https
but have achieved the same results.
以下是端点接收到的通知发布请求(我用星号替换了ARN):
Below is the Notification post request received by the endpoint (I have substituted ARNs with asterisks):
POST / HTTP/1.1
x-amz-sns-message-type: Notification
x-amz-sns-message-id: c8bce1ed-3766-5a04-a9ad-b4afc662cc31
x-amz-sns-topic-arn: ****
x-amz-sns-subscription-arn: ****
x-amz-sns-rawdelivery: true
Content-Length: 1
Content-Type: text/plain; charset=UTF-8
Host: ****.ngrok.io
User-Agent: Amazon Simple Notification Service Agent
Accept-Encoding: gzip,deflate
X-Forwarded-Proto: https
X-Forwarded-For: 54.240.230.176
发布到SNS的邮件的正文中只有一个字节(一个空格字符)-我认为这就是为什么内容长度为1的原因.
The messages published to SNS have only one byte in their body (a single space character) - I assume this is why the content length is 1.
其中一个消息属性是二进制格式,并且包含SNS消息正文中不允许的字符,否则,我只是将编码后的属性放在消息正文中.
One of the message attributes is of binary format and contains characters that are not allowed in the body of an SNS message, otherwise I would simply place the encoded attributes in the message body.
请求中似乎没有任何可用的属性,而我对此完全感到困惑.我希望它们将成为原始请求主体的一部分,但事实并非如此.有什么想法吗?
There do not appear to be any attributes available in the request, and I am totally stumped as to why. I would expect that they would be part of the raw request's body, but this does not seem to be the case. Any ideas?
我可能应该补充说,我已经将SQS订户添加到了相同的主题,并且SQS订户会收到预期的消息属性.
I should probably add that I have added an SQS subscriber to this same topic and that the SQS subscriber receives message attributes as would be expected.
推荐答案
解决方案就像禁用 RawMessageDelivery
一样简单.看来,此选项是将消息属性传递到SQS端点所必需的,但是在传递到HTTP/S端点时,必须必须使用.
The solution is as simple as disabling RawMessageDelivery
. It would seem as though this option is required for delivering message attributes to an SQS endpoint but must not be used when delivering to an HTTP/S endpoint.
这篇关于如何在SNS HTTPS订阅终结点中访问消息属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!