本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
SetSubscriptionAttributes
搭配 AWS SDK或 使用 CLI
下列程式碼範例示範如何使用 SetSubscriptionAttributes
。
- 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-nameRawMessageDelivery
\ --attribute-valuetrue
此命令不會產生輸出。
下列
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-nameFilterPolicy
\ --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-nameFilterPolicy
\ --attribute-value"{}"
此命令不會產生輸出。
-
如需API詳細資訊,請參閱 命令參考 SetSubscriptionAttributes
中的 。 AWS CLI
-
- Java
-
- SDK 適用於 Java 2.x
-
注意
還有更多 。 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); } } }
-
如需API詳細資訊,請參閱參考 SetSubscriptionAttributes中的 AWS SDK for Java 2.x API 。
-
- Python
-
- SDK for Python (Boto3)
-
注意
還有更多 。 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
-
如需API詳細資訊,請參閱 SetSubscriptionAttributes 中的 AWS SDK for Python (Boto3) API參考 。
-
如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 SNS 搭配 使用 Amazon AWS SDK。本主題也包含有關入門的資訊,以及先前SDK版本的詳細資訊。
SetSMSAttributes
SetSubscriptionAttributesRedrivePolicy