本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
篩選政策範例
下列範例顯示由處理客戶交易的 Amazon SNS主題傳遞的訊息承載。
第一個範例包含 MessageAttributes
欄位,其中包含描述交易的屬性:
-
客戶的利益
-
商店名稱
-
事件狀態
-
在 中的購買價格 USD
由於此訊息包含 MessageAttributes
欄位,所以只要訂閱中的 FilterPolicyScope
皆設為 MessageAttributes
,設為 FilterPolicy
的主題訂閱皆可選擇性地接受或拒絕訊息。如需將屬性套用到訊息的資訊,請參閱Amazon SNS 訊息屬性。
{
"Type": "Notification",
"MessageId": "a1b2c34d-567e-8f90-g1h2-i345j67klmn8",
"TopicArn": "arn:aws:sns:us-east-2:123456789012:MyTopic",
"Message": "message-body-with-transaction-details
",
"Timestamp": "2019-11-03T23:28:01.631Z",
"SignatureVersion": "4",
"Signature": "signature
",
"UnsubscribeURL": "unsubscribe-url
",
"MessageAttributes": {
"customer_interests": {
"Type": "String.Array",
"Value": "[\"soccer\", \"rugby\", \"hockey\"]"
},
"store": {
"Type": "String",
"Value":"example_corp"
},
"event": {
"Type": "String",
"Value": "order_placed"
},
"price_usd": {
"Type": "Number",
"Value": "210.75"
}
}
}
下列範例顯示 Message
欄位中的相同屬性,也稱為訊息承載或訊息內文。只要訂閱中的 FilterPolicyScope
皆設為 MessageBody
,任何包含 FilterPolicy
的主題訂閱皆可選擇性地接受或拒絕訊息。
{
"Type": "Notification",
"MessageId": "a1b2c34d-567e-8f90-g1h2-i345j67klmn8",
"TopicArn": "arn:aws:sns:us-east-2:123456789012:MyTopic",
"Message": "{
\"customer_interests\": [\"soccer\", \"rugby\", \"hockey\"],
\"store\": \"example_corp\",
\"event\":\"order_placed\",
\"price_usd\":210.75
}",
"Timestamp": "2019-11-03T23:28:01.631Z",
"SignatureVersion": "4",
"Signature": "signature",
"UnsubscribeURL": "unsubscribe-url"
}
以下篩選政策根據訊息的屬性名稱和值來接受或拒絕訊息。
接受範例訊息政策
以下訂閱篩選政策中的屬性符合指派給範例訊息的屬性。請注意,不論其設為 MessageAttributes
或 MessageBody
,相同的篩選政策皆適用於 FilterPolicyScope
。每個訂閱者會根據從主題接收到的訊息,選擇其篩選範圍。
如果此政策中的任何單一屬性不符指派給該訊息的屬性,政策將拒絕訊息。
{
"store": ["example_corp"],
"event": [{"anything-but": "order_cancelled"}],
"customer_interests": [
"rugby",
"football",
"baseball"
],
"price_usd": [{"numeric": [">=", 100]}]
}
拒絕範例訊息政策
以下訂閱篩選政策在其屬性和指派給範例訊息的屬性之間有諸多不符。例如,由於 encrypted
屬性名稱沒有出現在訊息屬性中,不論指派的值為何,此政策屬性都會導致訊息遭到拒絕。
如果有任何不符,政策會拒絕訊息。
{
"store": ["example_corp"],
"event": ["order_cancelled"],
"encrypted": [false],
"customer_interests": [
"basketball",
"baseball"
]
}