Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Verwendung SetSubscriptionAttributes
mit einem AWS SDK oder CLI
Die folgenden Code-Beispiele zeigen, wie SetSubscriptionAttributes
verwendet wird.
- CLI
-
- AWS CLI
-
So legen Sie Abonnementattribute fest
Im folgenden
set-subscription-attributes
-Beispiel wird dasRawMessageDelivery
-Attribut auf ein SQS-Abonnement festgelegt.aws sns set-subscription-attributes \ --subscription-arn
arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc
\ --attribute-nameRawMessageDelivery
\ --attribute-valuetrue
Mit diesem Befehl wird keine Ausgabe zurückgegeben.
Im folgenden
set-subscription-attributes
-Beispiel wird einFilterPolicy
-Attribut auf ein SQS-Abonnement festgelegt.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\"] }"Mit diesem Befehl wird keine Ausgabe zurückgegeben.
Im folgenden
set-subscription-attributes
-Beispiel wird dasFilterPolicy
-Attribut von einem SQS-Abonnement entfernt.aws sns set-subscription-attributes \ --subscription-arn
arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc
\ --attribute-nameFilterPolicy
\ --attribute-value"{}"
Mit diesem Befehl wird keine Ausgabe zurückgegeben.
-
Einzelheiten zur API finden Sie SetSubscriptionAttributes
in der AWS CLI Befehlsreferenz.
-
- Java
-
- SDK für Java 2.x
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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); } } }
-
Einzelheiten zur API finden Sie SetSubscriptionAttributesin der AWS SDK for Java 2.x API-Referenz.
-
- Python
-
- SDK für Python (Boto3)
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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
-
Einzelheiten zur API finden Sie SetSubscriptionAttributesin AWS SDK for Python (Boto3) API Reference.
-
Eine vollständige Liste der AWS SDK-Entwicklerhandbücher und Codebeispiele finden Sie unter. Amazon SNS mit einem AWS SDK verwenden Dieses Thema enthält auch Informationen zu den ersten Schritten und Details zu früheren SDK-Versionen.