如何在SNS订阅中添加SQS消息属性?

时间:2017-05-29 09:10:01

标签: amazon-web-services amazon-sqs amazon-sns

AWS SNS和SQS的文档包含有关消息属性的部分。但是,当该队列订阅了SNS主题时,没有解释如何获得SQS 消息属性

有没有办法配置AWS SNS以将特定的消息属性添加到通过订阅发送的SQS消息中?

3 个答案:

答案 0 :(得分:7)

在向Amazon SQS发送Amazon SNS消息属性的Using Amazon SNS Message Attributes文档时,似乎属性是在消息的正文中发送的,而不是作为消息属性附加到结果上Amazon SQS消息。

例如,我做了以下事情:

  • 创建了亚马逊SNS主题
  • 创建了Amazon SQS队列并将其订阅到SNS主题
  • 向SNS发布消息

我是通过AWS Command-Line Interface (CLI)发布的:

aws sns publish --topic-arn arn:aws:sns:ap-southeast-2:123456789012:foo --message msg --subject subj --message-attributes '{"somename" : { "DataType":"String", "StringValue":"somevalue"}}'

(我从map datatype in aws cli获得语法帮助)

SQS中显示的消息显示属性作为消息的一部分

{
  "Type" : "Notification",
  "MessageId" : "53e3adad-723a-5eae-a7b7-fc0468ec2d37",
  "TopicArn" : "arn:aws:sns:ap-southeast-2:123456789012:foo",
  "Subject" : "subj",
  "Message" : "msg",
  "Timestamp" : "2017-05-29T12:48:22.186Z",
  ...
  "MessageAttributes" : {
    "somename" : {"Type":"String","Value":"somevalue"}
  }
}

如果将这些属性作为官方SQS属性附加到SQS消息,那将会更好。唉,似乎情况并非如此。

答案 1 :(得分:5)

来自aws文档:

  

要将消息属性与Amazon SQS端点一起使用,必须将订阅属性Raw Message Delivery设置为True。有关原始邮件传递的详细信息,请参阅附录:大型有效负载和原始邮件传递。   https://docs.aws.amazon.com/sns/latest/dg/SNSMessageAttributes.html   https://docs.aws.amazon.com/sns/latest/dg/large-payload-raw-message.html

从现实生活中添加了一个例子。希望它有助于澄清事情。 发布到sns主题的消息如下:

aws sns publish --topic-arn arn:aws:sns:us-west-2:xxx:pollution-event --message '{"operatorId":3375001,"eventTypeId":1,"eventLevelId":1,"validFrom":"2018-03-10T09:00:00Z","validTo":"2018-03-11T09:00:00Z"}'  --message-attributes '{"Type" : { "DataType":"String", "StringValue":"Orchestration.Services.Model.Pollution.PollutionMessage"}}'

启用原始传递为false(默认)。 sqs收到的消息只包含内容,没有属性

{
  "Type": "Notification",
  "MessageId": "78d5bc6f-142c-5060-a75c-ef29b774ec66",
  "TopicArn": "arn:aws:sns:eu-west-2:xxx:pollution-event",
  "Message": "{\"validFrom\": \"2018-03-10T09:00:00Z\",\"validTo\": \"2018-03-11T09:00:00Z\",\"eventLevelId\": 1,\"eventTypeId\": 1,\"operatorId\": 3375001}",
  "Timestamp": "2018-04-17T11:33:44.770Z",
  "SignatureVersion": "1",
  "Signature": "xxx==",
  "SigningCertURL": "https://sns.eu-west-2.amazonaws.com/SimpleNotificationService-xxx.pem",
  "UnsubscribeURL": "https://sns.eu-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-2:xxx",
  "MessageAttributes": {
    "Type": {
      "Type": "String",
      "Value": "Orchestration.Services.Model.Pollution.PollutionMessage"
    },
    "AWS.SNS.MOBILE.MPNS.Type": {
      "Type": "String",
      "Value": "token"
    },
    "AWS.SNS.MOBILE.MPNS.NotificationClass": {
      "Type": "String",
      "Value": "realtime"
    },
    "AWS.SNS.MOBILE.WNS.Type": {
      "Type": "String",
      "Value": "wns/badge"
    }
  }
}

There are no message attributes, it is contained within the message itself

启用原始传送。该邮件包含邮件属性和正确的内容 Message has an attribute The attribute contains expected value

答案 2 :(得分:0)

在为SNS内的主题添加SQS订阅时启用了原始消息传递类型