解析 Amazon SNS 消息格式
Amazon SNS 使用下列格式。
主题
HTTP/HTTPS 标题
当 Amazon SNS 向 HTTP/HTTPS 终端节点发送订阅确认消息、通知、或者取消订阅确认消息时,它将发出一个带有多个 Amazon SNS 标头值的 POST 消息。可以使用标头值执行以下任务,例如识别消息类型而无需解析 JSON 消息主体来读取 Type
值。原定设置情况下,Amazon SNS 会将所有通知发送到 HTTP/S 端点,Content-Type
设置为 text/plain; charset=UTF-8
。要选择除文本/纯文本(原定设置)以外的 Content-Type
,请参阅创建 HTTP/S 传输策略中的 headerContentType
。
x-amz-sns-message-type
-
消息类型。可能的值为
SubscriptionConfirmation
、Notification
和UnsubscribeConfirmation
。 x-amz-sns-message-id
-
通用唯一标识符(UUID),它对于每条发布的消息是唯一的。对于 Amazon SNS 在重试期间重新发送的通知,使用原始消息的消息 ID。
x-amz-sns-topic-arn
-
此消息所发布到的主题的 Amazon 资源名称(ARN)。
x-amz-sns-subscription-arn
-
用于订阅终端节点的 ARN。
下面的 HTTP POST 标头是一条发送至 HTTP 端点的 Notification
消息的标头示例。
POST / HTTP/1.1 x-amz-sns-message-type: Notification x-amz-sns-message-id: 165545c9-2a5c-472c-8df2-7ff2be2b3b1b x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55 Content-Length: 1336 Content-Type: text/plain; charset=UTF-8 Host: myhost.example.com Connection: Keep-Alive User-Agent: Amazon Simple Notification Service Agent
HTTP/HTTPS 订阅确认 JSON 格式
订阅一个 HTTP/HTTPS 终端节点后,Amazon SNS 将发送一条订阅确认消息至HTTP/HTTPS 终端节点。此条消息包含您必须访问的 SubscribeURL
值,以确认订阅(或者,您可以将 Token
值与 ConfirmSubscription
结合使用)。
注意
直到订阅被确认后,Amazon SNS 才会向终端节点发送通知
订阅确认消息是一条 POST 消息,消息正文包含一个带以下名称/值对的 JSON 格式文档。
Type
-
消息类型。为订阅确认,消息类型为:
SubscriptionConfirmation
。 MessageId
-
通用唯一标识符(UUID),它对于每条发布的消息是唯一的。对于 Amazon SNS 在重试期间重新发送的消息,原始消息的消息 ID 被使用。
Token
-
您可以使用
ConfirmSubscription
操作确认订阅的一个值。或者,您只需访问SubscribeURL
。 TopicArn
-
终端节点已经订阅该主题的 Amazon Resource Name。
Message
-
一个描述消息的字符串。为订阅确认,字符串看上去像这样:
You have chosen to subscribe to the topic arn:aws:sns:us-east-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.
SubscribeURL
-
为了确认订阅而必须访问的 URL。或者,您可以改为将
Token
与ConfirmSubscription
操作结合使用以确认订阅。 Timestamp
-
订阅确认发出的时间 (GMT)。
SignatureVersion
-
所用 Amazon SNS 签名的版本。
-
如果
SignatureVersion
为 1,则Signature
是Message
、MessageId
、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA1withRSA
签名。 -
如果
SignatureVersion
为 2,则Signature
是Message
、MessageId
、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA256withRSA
签名。
-
Signature
-
Message
、MessageId
、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA1withRSA
或SHA256withRSA
签名。 SigningCertURL
-
用于签署消息的证书的 URL。
以下 HTTP POST 消息是发送至 HTTP 端点的一条 SubscriptionConfirmation
消息的示例。
POST / HTTP/1.1 x-amz-sns-message-type: SubscriptionConfirmation x-amz-sns-message-id: 165545c9-2a5c-472c-8df2-7ff2be2b3b1b x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic Content-Length: 1336 Content-Type: text/plain; charset=UTF-8 Host: myhost.example.com Connection: Keep-Alive User-Agent: Amazon Simple Notification Service Agent { "Type" : "SubscriptionConfirmation", "MessageId" : "165545c9-2a5c-472c-8df2-7ff2be2b3b1b", "Token" : "2336412f37...", "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic", "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.", "SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37...", "Timestamp" : "2012-04-26T20:45:04.751Z", "SignatureVersion" : "1", "Signature" : "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=", "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem" }
HTTP/HTTPS 通知 JSON 格式
当 Amazon SNS 向已订阅的 HTTP 或 HTTPS 终端节点发送一条通知时,发送到终端节点的 POST 消息具有包含一个带下列名称/值对的 JSON 格式文档的消息正文。
Type
-
消息类型。用于通知,这种类型属于
Notification
。 MessageId
-
通用唯一标识符(UUID),它对于每条发布的消息是唯一的。对于 Amazon SNS 在重试期间重新发送的通知,使用原始消息的消息 ID。
TopicArn
-
此消息所发布到的主题的 Amazon 资源名称(ARN)。
Subject
-
在将通知发布至主题时指定的
Subject
参数。注意
此参数为可选参数。如果未指定
Subject
,则此 JSON 格式文档中不会显示该名称/值对。 Message
-
当通知发布至主题时指定的
Message
值。 Timestamp
-
通知发布的时间 (GMT)。
SignatureVersion
-
所用 Amazon SNS 签名的版本。
-
如果
SignatureVersion
为 1,则Signature
是Message
、MessageId
、Subject
(如果存在)、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA1withRSA
签名。 -
如果
SignatureVersion
为 2,则Signature
是Message
、MessageId
、Subject
(如果存在)、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA256withRSA
签名。
-
Signature
-
Message
、MessageId
、Subject
(如果存在)、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA1withRSA
或SHA256withRSA
签名。 SigningCertURL
-
用于签署消息的证书的 URL。
UnsubscribeURL
-
可以用作从主题取消订阅终端节点的 URL。如果您访问此 URL,那么 Amazon SNS 将取消订阅终端节点并不发送通知至此终端节点。
以下 HTTP POST 消息是发送至 HTTP 端点的一条 Notification
消息的示例。
POST / HTTP/1.1 x-amz-sns-message-type: Notification x-amz-sns-message-id: 22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324 x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96 Content-Length: 773 Content-Type: text/plain; charset=UTF-8 Host: myhost.example.com Connection: Keep-Alive User-Agent: Amazon Simple Notification Service Agent { "Type" : "Notification", "MessageId" : "22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324", "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic", "Subject" : "My First Message", "Message" : "Hello world!", "Timestamp" : "2012-05-02T00:54:06.655Z", "SignatureVersion" : "1", "Signature" : "EXAMPLEw6JRN...", "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem", "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96" } }
HTTP/HTTPS 取消订阅确认 JSON 格式
HTTP/HTTPS 终端节点从一个主题取消订阅之后,Amazon SNS 将向终端节点发送一条取消订阅的消息。
取消订阅确认消息是一条 POST 消息,消息正文包含一个带以下名称/值对的 JSON 格式文档。
Type
-
消息类型。为取消订阅确认,消息类型为
UnsubscribeConfirmation
。 MessageId
-
通用唯一标识符(UUID),它对于每条发布的消息是唯一的。对于 Amazon SNS 在重试期间重新发送的消息,原始消息的消息 ID 被使用。
Token
-
您可以使用
ConfirmSubscription
操作重新确认订阅的一个值。或者,您只需访问SubscribeURL
。 TopicArn
-
此终端节点已经从主题取消订阅的 Amazon Resource Name (ARN)。
Message
-
一个描述消息的字符串。为了取消订阅确认,字符串应看起来像这样:
You have chosen to deactivate subscription arn:aws:sns:us-east-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55.\nTo cancel this operation and restore the subscription, visit the SubscribeURL included in this message.
SubscribeURL
-
为了重新确认订阅,您必须访问的 URL。或者,您可以改为将
Token
与ConfirmSubscription
操作结合使用以重新确认订阅。 Timestamp
-
取消订阅确认发送的时间(GMT)。
SignatureVersion
-
所用 Amazon SNS 签名的版本。
-
如果
SignatureVersion
为 1,则Signature
是Message
、MessageId
、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA1withRSA
签名。 -
如果
SignatureVersion
为 2,则Signature
是Message
、MessageId
、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA256withRSA
签名。
-
Signature
-
Message
、MessageId
、Type
、Timestamp
和TopicArn
值的 Base64 编码SHA1withRSA
或SHA256withRSA
签名。 SigningCertURL
-
用于签署消息的证书的 URL。
以下 HTTP POST 消息是发送至 HTTP 端点的一条 UnsubscribeConfirmation
消息的示例。
POST / HTTP/1.1 x-amz-sns-message-type: UnsubscribeConfirmation x-amz-sns-message-id: 47138184-6831-46b8-8f7c-afc488602d7d x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55 Content-Length: 1399 Content-Type: text/plain; charset=UTF-8 Host: myhost.example.com Connection: Keep-Alive User-Agent: Amazon Simple Notification Service Agent { "Type" : "UnsubscribeConfirmation", "MessageId" : "47138184-6831-46b8-8f7c-afc488602d7d", "Token" : "2336412f37...", "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic", "Message" : "You have chosen to deactivate subscription arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55.\nTo cancel this operation and restore the subscription, visit the SubscribeURL included in this message.", "SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37fb6...", "Timestamp" : "2012-04-26T20:06:41.581Z", "SignatureVersion" : "1", "Signature" : "EXAMPLEHXgJm...", "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem" }
SetSubscriptionAttributes 传输策略 JSON 格式
如果您向 SetSubscriptionAttributes
操作发送一个请求并将 AttributeName
参数设置为 DeliveryPolicy
值,那么 AttributeValue
参数的值必须是一个有效的 JSON 对象。例如,以下例子将传输策略设置为 5 次重试。
http://sns.us-east-2.amazonaws.com/ ?Action=SetSubscriptionAttributes &SubscriptionArn=arn%3Aaws%3Asns%3Aus-east-2%3A123456789012%3AMy-Topic%3A80289ba6-0fd4-4079-afb4-ce8c8260f0ca &AttributeName=DeliveryPolicy &AttributeValue={"healthyRetryPolicy":{"numRetries":5}} ...
为 AttributeValue
参数的值使用下列 JSON 格式。
{ "healthyRetryPolicy" : { "minDelayTarget" : int, "maxDelayTarget" : int, "numRetries" : int, "numMaxDelayRetries" : int, "backoffFunction" : "linear|arithmetic|geometric|exponential" }, "throttlePolicy" : { "maxReceivesPerSecond" : int }, "requestPolicy" : { "headerContentType" : "text/plain | application/json | application/xml" } }
有关 SetSubscriptionAttribute
操作的更多信息,请转到《Amazon Simple Notification Service API 参考》中的 SetSubscriptionAttributes。有关支持的 HTTP content-type 标头的更多信息,请参阅创建 HTTP/S 传输策略。
SetTopicAttributes 传输策略 JSON 格式
如果您向 SetTopicAttributes
操作发送一个请求并将 AttributeName
参数设置为 DeliveryPolicy
值,那么 AttributeValue
参数的值必须是一个有效的 JSON 对象。例如,以下例子将传输策略设置为 5 次重试。
http://sns.us-east-2.amazonaws.com/ ?Action=SetTopicAttributes &TopicArn=arn%3Aaws%3Asns%3Aus-east-2%3A123456789012%3AMy-Topic &AttributeName=DeliveryPolicy &AttributeValue={"http":{"defaultHealthyRetryPolicy":{"numRetries":5}}} ...
为 AttributeValue
参数的值使用下列 JSON 格式。
{ "http" : { "defaultHealthyRetryPolicy" : { "minDelayTarget": int, "maxDelayTarget": int, "numRetries": int, "numMaxDelayRetries": int, "backoffFunction": "linear|arithmetic|geometric|exponential" }, "disableSubscriptionOverrides" : Boolean, "defaultThrottlePolicy" : { "maxReceivesPerSecond" : int }, "defaultRequestPolicy" : { "headerContentType" : "text/plain | application/json | application/xml" } } }
有关 SetTopicAttribute
操作的更多信息,请转到 Amazon Simple Notification Service API 参考中的 SetTopicAttributes。有关支持的 HTTP content-type 标头的更多信息,请参阅创建 HTTP/S 传输策略。