本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
剖析訊息格式
Amazon SNS 使用下列格式。
主題
HTTP/HTTPS 標頭
當 Amazon SNS 傳送訂閱確認、通知或取消訂閱 HTTP/HTTPS 端點的確認訊息時,會傳送含有數個 Amazon SNS 特定標頭值的 POST 訊息。您可以將這些標頭值使用於識別訊息類型等任務,而無需剖析 JSON 訊息內文以讀取 Type
值。根據預設,Amazon SNS 會將所有通知傳送至 Content-Type
設定為 text/plain; charset=UTF-8
的 HTTP/S 端點。若要選擇文字/純文字 (預設值) 以外的 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
值,您必須造訪才能確認訂閱 (或者,您可以使用具有 ConfirmSubscription
的 Token
值)。
注意
除非已確認訂閱,否則 Amazon SNS 不會傳送通知至此端點
訂閱確認訊息是 POST 訊息,訊息內文包含 JSON 文件及以下名稱值組。
Type
-
訊息的類型。若是訂閱確認,類型為
SubscriptionConfirmation
。 MessageId
-
全域唯一識別符 (UUID),對於每個發布的訊息均為唯一。若是 Amazon SNS 在重試期間重送的訊息,會使用原始訊息的訊息 ID。
Token
-
您可以與
ConfirmSubscription
動作搭配使用以確認訂閱。或者,您也可直接造訪SubscribeURL
。 TopicArn
-
此端點訂閱之主題的 Amazon Resource Name (ARN)。
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
值的 Base-64 編碼SHA1withRSA
簽章。 -
如果
SignatureVersion
為 2,則Signature
為Message
、MessageId
、Type
、Timestamp
和TopicArn
值的 Base-64 編碼SHA256withRSA
簽章。
-
Signature
-
以 Base64 編碼
SHA1withRSA
或Message
、MessageId
、Type
、Timestamp
和TopicArn
值的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
值的 Base-64 編碼SHA1withRSA
簽章。 -
如果
SignatureVersion
為 2,則Signature
為Message
、MessageId
、Subject
(如果存在)、Type
、Timestamp
和TopicArn
值的 Base-64 編碼SHA256withRSA
簽章。
-
Signature
-
以 Base64 編碼
SHA1withRSA
或Message
、MessageId
、Subject
(如果存在)、Type
、Timestamp
和TopicArn
值的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 資源名稱 (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
值的 Base-64 編碼SHA1withRSA
簽章。 -
如果
SignatureVersion
為 2,則Signature
為Message
、MessageId
、Type
、Timestamp
和TopicArn
值的 Base-64 編碼SHA256withRSA
簽章。
-
Signature
-
以 Base64 編碼
SHA1withRSA
或Message
、MessageId
、Type
、Timestamp
和TopicArn
值的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 內容類型標頭的詳細資訊,請參閱 建立 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 內容類型標頭的詳細資訊,請參閱 建立 HTTP/S 傳遞政策。