

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 解析 Amazon SNS 消息格式
<a name="sns-message-and-json-formats"></a>

当 Amazon SNS 向 HTTP/HTTPS 终端节点发送消息时，它们同时包含 HTTP 标头和 JSON 消息正文。这些消息遵循特定的结构格式，其中包括消息类型、主题 ARN、时间戳以及数字签名等元数据。通过正确解析 Amazon SNS 消息，您可以判断消息是订阅确认、通知还是取消订阅确认，并提取相关数据，同时通过签名验证来确认消息的真实性。

# HTTP/HTTPS 标题
<a name="http-header"></a>

当 Amazon SNS 向 HTTP/HTTPS 终端节点发送订阅确认、通知或取消订阅确认消息时，它会发送一条包含许多 Amazon SNS 特定标头值的 POST 消息。可以使用标头值执行以下任务，例如识别消息类型而无需解析 JSON 消息主体来读取 `Type` 值。原定设置情况下，Amazon SNS 会将所有通知发送到 HTTP/S 端点，`Content-Type` 设置为 `text/plain; charset=UTF-8`。要选择 text/plain （默认）以`Content-Type`外的选项，请参阅`headerContentType`中的[创建配 HTTP/S 送策略](sns-message-delivery-retries.md#creating-delivery-policy)。

**`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 格式
<a name="http-subscription-confirmation-json"></a>

在您订阅 HTTP/HTTPS 终端节点后，Amazon SNS 会向该 HTTP/HTTPS 终端节点发送订阅确认消息。此条消息包含您必须访问的 `SubscribeURL` 值，以确认订阅（或者，您可以将 `Token` 值与 [https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html](https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html) 结合使用）。

**注意**  
直到订阅被确认后，Amazon SNS 才会向终端节点发送通知

订阅确认消息是一条 POST 消息，消息正文包含一个带以下名称/值对的 JSON 格式文档。

**`Type`**  
消息类型。为订阅确认，消息类型为：`SubscriptionConfirmation`。

**`MessageId`**  
通用唯一标识符（UUID），它对于每条发布的消息是唯一的。对于 Amazon SNS 在重试期间重新发送的消息，原始消息的消息 ID 被使用。

**`Token`**  
您可以使用 [https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html](https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html) 操作确认订阅的一个值。或者，您只需访问`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` 与 [https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html](https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html) 操作结合使用以确认订阅。

**`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 格式
<a name="http-notification-json"></a>

当 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 格式
<a name="http-unsubscribe-confirmation-json"></a>

在 HTTP/HTTPS 终端节点取消订阅主题后，Amazon SNS 会向该终端节点发送取消订阅确认消息。

取消订阅确认消息是一条 POST 消息，消息正文包含一个带以下名称/值对的 JSON 格式文档。

**`Type`**  
消息类型。为取消订阅确认，消息类型为`UnsubscribeConfirmation`。

**`MessageId`**  
通用唯一标识符（UUID），它对于每条发布的消息是唯一的。对于 Amazon SNS 在重试期间重新发送的消息，原始消息的消息 ID 被使用。

**`Token`**  
您可以使用 [https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html](https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html) 操作重新确认订阅的一个值。或者，您只需访问`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` 与 [https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html](https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html) 操作结合使用以重新确认订阅。

**`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 格式
<a name="set-sub-attributes-delivery-policy-json"></a>

如果您向 `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`操作的更多信息，请[SetSubscriptionAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html)访问《*亚马逊简单通知服务 API 参考*》。有关支持的 HTTP content-type 标头的更多信息，请参阅[创建配 HTTP/S 送策略](sns-message-delivery-retries.md#creating-delivery-policy)。

# SetTopicAttributes 交付策略 JSON 格式
<a name="set-topic-attributes-delivery-policy-json"></a>

如果您向 `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`操作的更多信息，请[SetTopicAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetTopicAttributes.html)访问《*亚马逊简单通知服务 API 参考*》。有关支持的 HTTP content-type 标头的更多信息，请参阅[创建配 HTTP/S 送策略](sns-message-delivery-retries.md#creating-delivery-policy)。