在 Amazon SNS 中套用訂閱篩選政策 - Amazon Simple Notification Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在 Amazon SNS 中套用訂閱篩選政策

Amazon SNS 中的訊息篩選可讓您根據篩選器政策選擇性地將訊息傳遞給訂閱者。這些原則會定義郵件必須符合才能傳遞至訂用帳戶的條件。雖然原始郵件傳遞是可能會影響郵件處理的選項,但訂閱篩選器無法運作。

您可以使用 Amazon SNS 主控台將篩選政策套用到 Amazon SNS 訂閱。或者,若要以程式設計方式套用政策,您可以使用 Amazon SNS API、 AWS Command Line Interface (AWS CLI) 或任何支援 Amazon SNS 的 AWS 開發套件。您也可以使用 AWS CloudFormation.

啟用原始訊息傳遞

原始郵件傳遞可確保郵件承載按原樣傳遞給訂閱者,無需任何額外的編碼或轉換。當訂閱者需要原始郵件格式進行處理時,這很有用。不過,原始郵件傳遞與訂閱篩選器的功能並不直接相關。

套用訂閱篩選器

若要將訊息篩選器套用至訂閱,您可以使用 JSON 語法定義篩選原則。此原則指定郵件必須符合才能傳遞至訂閱的條件。篩選器可以以郵件屬性為基礎,例如郵件屬性、郵件結構,甚至是郵件內容。

原始訊息傳遞與訂閱篩選器之間的關係

雖然啟用原始郵件傳遞可能會影響訂閱者傳遞及處理郵件的方式,但不是使用訂閱篩選器的先決條件。但是,在訂閱者需要原始郵件格式而不進行任何修改的情況下,啟用原始郵件傳遞可能會與訂閱篩選器一起有益。

有效篩選的考量

實作訊息篩選時,請考量應用程式和訂閱者的特定需求。定義準確符合郵件傳遞準則的篩選器原則,以確保有效率且鎖定目標的郵件發佈。

重要

AWS IAM 和 Amazon SNS 等服務使用稱為最終一致性的分散式運算模型。對訂閱篩選原則進行新增或變更,最多需要 15 分鐘才能完全生效。

AWS Management Console

  1. 登入 Amazon SNS 主控台

  2. 在導覽面板上,選擇 Subscriptions (訂閱)

  3. 選取訂閱,然後選擇 Edit (編輯)。

  4. Edit (編輯) 頁面上,展開 Subscription filter policy (訂閱篩選政策) 區段。

  5. 選擇以屬性為基礎的篩選以承載為基礎的篩選

  6. JSON editor (JSON 編輯器) 欄位中,提供您篩選政策的 JSON 內文

  7. 選擇儲存變更

    Amazon SNS 套用您的篩選政策到訂閱。

AWS CLI

若要使用 AWS Command Line Interface (AWS CLI) 套用篩選原則,請使用set-subscription-attributes命令,如下列範例所示。對於 --attribute-name 選項,指定 FilterPolicy。針對 --attribute-value,請指定 JSON 政策

$ aws sns set-subscription-attributes --subscription-arn arn:aws:sns: ... --attribute-name FilterPolicy --attribute-value '{"store":["example_corp"],"event":["order_placed"]}'

若要為您的政策提供有效的 JSON,請使用雙引號括住屬性名稱和值。您也必須用引號括住整個政策引數。若要避免逸出引號,您可以如上方範例所示,使用單引號括住政策,並用雙引號括住 JSON 名稱和值。

如果您想要從以屬性為基礎的 (預設) 切換為以承載為基礎的郵件篩選,也可以使用命令。set-subscription-attributes對於 --attribute-name 選項,指定 FilterPolicyScope。對於 --attribute-value,請指定 MessageBody

$ aws sns set-subscription-attributes --subscription-arn arn:aws:sns: ... --attribute-name FilterPolicyScope --attribute-value MessageBody

如果要確認是否已套用篩選政策,請使用 get-subscription-attributes 命令。如以下範例所示,內部輸出中的屬性應顯示您的 FilterPolicy 索引鍵的篩選政策:

$ aws sns get-subscription-attributes --subscription-arn arn:aws:sns: ... { "Attributes": { "Endpoint": "endpoint . . .", "Protocol": "https", "RawMessageDelivery": "false", "EffectiveDeliveryPolicy": "delivery policy . . .", "ConfirmationWasAuthenticated": "true", "FilterPolicy": "{\"store\": [\"example_corp\"], \"event\": [\"order_placed\"]}", "FilterPolicyScope": "MessageAttributes", "Owner": "111122223333", "SubscriptionArn": "arn:aws:sns: . . .", "TopicArn": "arn:aws:sns: . . ." } }

AWS 開發套件

下列程式碼範例會示範如何使用SetSubscriptionAttributes

重要

如果您使用適用於 Java 2.x 的 SDK 範例,則類別 SNSMessageFilterPolicy 非可立即運作。有關如何安裝此類的說明,請參閱 GitHub 網站上的示

CLI
AWS CLI

設定訂閱屬性

下列 set-subscription-attributes 範例會將 RawMessageDelivery 屬性設定為 SQS 訂閱。

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name RawMessageDelivery \ --attribute-value true

此命令不會產生輸出。

下列 set-subscription-attributes 範例會將 FilterPolicy 屬性設定為 SQS 訂閱。

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name FilterPolicy \ --attribute-value "{ \"anyMandatoryKey\": [\"any\", \"of\", \"these\"] }"

此命令不會產生輸出。

下列 set-subscription-attributes 範例會從 SQS 訂閱移除 FilterPolicy 屬性。

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name FilterPolicy \ --attribute-value "{}"

此命令不會產生輸出。

Java
適用於 Java 2.x 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SnsException; import java.util.ArrayList; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class UseMessageFilterPolicy { public static void main(String[] args) { final String usage = """ Usage: <subscriptionArn> Where: subscriptionArn - The ARN of a subscription. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String subscriptionArn = args[0]; SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); usePolicy(snsClient, subscriptionArn); snsClient.close(); } public static void usePolicy(SnsClient snsClient, String subscriptionArn) { try { SNSMessageFilterPolicy fp = new SNSMessageFilterPolicy(); // Add a filter policy attribute with a single value fp.addAttribute("store", "example_corp"); fp.addAttribute("event", "order_placed"); // Add a prefix attribute fp.addAttributePrefix("customer_interests", "bas"); // Add an anything-but attribute fp.addAttributeAnythingBut("customer_interests", "baseball"); // Add a filter policy attribute with a list of values ArrayList<String> attributeValues = new ArrayList<>(); attributeValues.add("rugby"); attributeValues.add("soccer"); attributeValues.add("hockey"); fp.addAttribute("customer_interests", attributeValues); // Add a numeric attribute fp.addAttribute("price_usd", "=", 0); // Add a numeric attribute with a range fp.addAttributeRange("price_usd", ">", 0, "<=", 100); // Apply the filter policy attributes to an Amazon SNS subscription fp.apply(snsClient, subscriptionArn); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
Python
適用於 Python (Boto3) 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

class SnsWrapper: """Encapsulates Amazon SNS topic and subscription functions.""" def __init__(self, sns_resource): """ :param sns_resource: A Boto3 Amazon SNS resource. """ self.sns_resource = sns_resource @staticmethod def add_subscription_filter(subscription, attributes): """ Adds a filter policy to a subscription. A filter policy is a key and a list of values that are allowed. When a message is published, it must have an attribute that passes the filter or it will not be sent to the subscription. :param subscription: The subscription the filter policy is attached to. :param attributes: A dictionary of key-value pairs that define the filter. """ try: att_policy = {key: [value] for key, value in attributes.items()} subscription.set_attributes( AttributeName="FilterPolicy", AttributeValue=json.dumps(att_policy) ) logger.info("Added filter to subscription %s.", subscription.arn) except ClientError: logger.exception( "Couldn't add filter to subscription %s.", subscription.arn ) raise

Amazon SNS API

若要使用 Amazon SNS API 套用篩選政策,請提出 SetSubscriptionAttributes 動作的請求。將 AttributeName 參數設定為 FilterPolicy,然後將 AttributeValue 參數設定為您的篩選政策 JSON。

若您想要從以屬性為基礎 (預設) 切換到以承載為基礎的訊息篩選,也可以使用 SetSubscriptionAttributes 動作。將 AttributeName 參數設定為 FilterPolicyScope,並將 AttributeValue 參數設定為 MessageBody

AWS CloudFormation

若要使用套用篩選原則 AWS CloudFormation,請使用 JSON 或 YAML 範本建立 AWS CloudFormation 堆疊。如需詳細資訊,請參閱《AWS CloudFormation 使用指南》中的AWS::SNS::Subscription資源FilterPolicy屬性範例 AWS CloudFormation 範本

  1. 登入 AWS CloudFormation 主控台

  2. 選擇 Create Stack (建立堆疊)。

  3. Select Template (選擇範本) 頁面中,選擇 Upload a template to Amazon S3 (上傳範本到 Amazon S3)、選擇檔案,並選擇 Next (下一步)。

  4. Specify Details (指定詳細資訊) 頁面上,執行下列作業:

    1. Stack Name (堆疊名稱) 中輸入 MyFilterPolicyStack

    2. 在中 myHttpEndpoint,輸入要訂閱您主題的 HTTP 端點。

      提示

      若您沒有 HTTP 端點,請建立一個。

  5. Options (選項) 頁面上,選擇 Next (下一步)。

  6. Review (檢閱) 頁面上,選擇 Create (建立)。