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

How to add SQS message attributes in SNS subscription?

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

是否可以配置 AWS SNS 以将特定的消息属性添加到通过订阅发送的 SQS 消息中?

注意:请参阅其他答案以获得更好的响应,使用原始消息传递


虽然 Using Amazon SNS Message Attributes 文档将 Amazon SNS 消息属性发送到 Amazon SQS,但这些属性似乎是在消息的 正文 中发送的,而不是附加为生成的 Amazon SQS 消息的消息属性。

例如,我做了以下操作:

  • 创建了一个 Amazon 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 消息中,那就更好了。唉,好像不是这样。

来自 aws 文档:

To use message attributes with Amazon SQS endpoints, you must set the subscription attribute, Raw Message Delivery, to True. For more information about raw message delivery, see Appendix: Large Payload and Raw Message Delivery. https://docs.aws.amazon.com/sns/latest/dg/SNSMessageAttributes.html https://docs.aws.amazon.com/sns/latest/dg/large-payload-raw-message.html

添加了来自 real-life 项目的示例。希望它有助于澄清事情。 发布到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"
    }
  }
}

启用原始传递为真。消息包含消息属性和正确的内容

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

如果您在这里是因为您有一个订阅了 SNS 主题的 SQS 队列,您检查了您的订阅是否已将 Raw Message Delivery 设置为 True,但您仍然无法读取 SQS 消息上的属性:

确保您的 SQS 客户端没有过滤掉消息属性。

当从 SQS 队列接收消息时,下面的代码将只包含 myAttribute

SQS.receiveMessage({
  QueueUrl: queueUrl,
  VisibilityTimeout: 20,
  WaitTimeSeconds: 10,
  MessageAttributeNames: [
    "myAttribute"
  ],
},...

如果您想读取 myAttribute 以外的某些属性的值,您必须指定它(白名单)或将 "myAttribute" 替换为 "All" 以包括所有 SQS 属性.

SQS.receiveMessage({
  MessageAttributeNames: [
    "myAttribute", "myOtherAttribute"
  ],
},...

参考:https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html