使用 Amazon SNS 示例 Amazon CLI - Amazon Command Line Interface
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SNS 示例 Amazon CLI

以下代码示例向您展示了如何在 Amazon SNS 中使用来执行操作和实现常见场景。 Amazon Command Line Interface

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是展示如何通过在同一服务中调用多个函数来完成特定任务任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

操作

以下代码示例演示如何使用 add-permission

Amazon CLI

为主题添加权限

以下add-permission示例添加了账户在 Amazon Amazon 账户987654321098下使用指定主题的Publish操作的权限123456789012

aws sns add-permission \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --label Publish-Permission \ --aws-account-id 987654321098 \ --action-name Publish

此命令不生成任何输出。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考AddPermission中的。

以下代码示例演示如何使用 check-if-phone-number-is-opted-out

Amazon CLI

检查电话号码的 SMS 消息退出

以下check-if-phone-number-is-opted-out示例检查指定的电话号码是否已选择不接收来自当前 Amazon 账户的 SMS 消息。

aws sns check-if-phone-number-is-opted-out \ --phone-number +1555550100

输出:

{ "isOptedOut": false }

以下代码示例演示如何使用 confirm-subscription

Amazon CLI

确认订阅

以下 confirm-subscription 命令将完成订阅名为 my-topic 的 SNS 主题时启动的确认过程。--token 参数来自发送到订阅调用中指定的通知端点的确认消息。

aws sns confirm-subscription \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --token 2336412f37fb687f5d51e6e241d7700ae02f7124d8268910b858cb4db727ceeb2474bb937929d3bdd7ce5d0cce19325d036bc858d3c217426bcafa9c501a2cace93b83f1dd3797627467553dc438a8c974119496fc3eff026eaa5d14472ded6f9a5c43aec62d83ef5f49109da7176391

输出:

{ "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" }

以下代码示例演示如何使用 create-platform-application

Amazon CLI

创建平台应用程序

以下create-platform-application示例使用指定的平台凭据创建了一个 Google Firebase 平台应用程序。

aws sns create-platform-application \ --name MyApplication \ --platform GCM \ --attributes PlatformCredential=EXAMPLEabcd12345jklm67890stuv12345bcdef

输出:

{ "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication" }

以下代码示例演示如何使用 create-topic

Amazon CLI

创建 SNS 主题

以下 create-topic 示例将创建名为 my-topic 的 SNS 主题。

aws sns create-topic \ --name my-topic

输出:

{ "ResponseMetadata": { "RequestId": "1469e8d7-1642-564e-b85d-a19b4b341f83" }, "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" }

有关更多信息,请参阅《 Amazon 命令行界面用户指南》中的在 Amazon SQS 和 Amazon SNS 中使用命令Amazon 行界面。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考CreateTopic中的。

以下代码示例演示如何使用 delete-endpoint

Amazon CLI

删除平台应用程序终端节点

以下delete-endpoint示例删除了指定的平台应用程序终端节点。

aws sns delete-endpoint \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234

此命令不生成任何输出。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考DeleteEndpoint中的。

以下代码示例演示如何使用 delete-platform-application

Amazon CLI

删除平台应用程序

以下delete-platform-application示例删除了指定的平台应用程序。

aws sns delete-platform-application \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/ADM/MyApplication

此命令不生成任何输出。

以下代码示例演示如何使用 delete-topic

Amazon CLI

删除 SNS 主题

以下 delete-topic 示例将删除指定的 SNS 主题。

aws sns delete-topic \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

此命令不生成任何输出。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考DeleteTopic中的。

以下代码示例演示如何使用 get-endpoint-attributes

Amazon CLI

列出平台应用程序终端节点属性

以下get-endpoint-attributes示例列出了指定平台应用程序终端节点的属性。

aws sns get-endpoint-attributes \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234

输出:

{ "Attributes": { "Enabled": "true", "Token": "EXAMPLE12345..." } }

以下代码示例演示如何使用 get-platform-application-attributes

Amazon CLI

列出平台应用程序的属性

以下get-platform-application-attributes示例列出了指定平台应用程序的属性。

aws sns get-platform-application-attributes \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/MPNS/MyApplication

输出:

{ "Attributes": { "Enabled": "true", "SuccessFeedbackSampleRate": "100" } }

以下代码示例演示如何使用 get-sms-attributes

Amazon CLI

列出默认 SMS 消息属性

以下 get-sms-attributes 示例将列出发送 SMS 消息的默认属性。

aws sns get-sms-attributes

输出:

{ "attributes": { "DefaultSenderID": "MyName" } }
  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 GetSMSAttributes

以下代码示例演示如何使用 get-subscription-attributes

Amazon CLI

检索主题的订阅属性

下面get-subscription-attributes显示了指定订阅的属性。你可以subscription-arnlist-subscriptions命令的输出中获取。

aws sns get-subscription-attributes \ --subscription-arn "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f"

输出:

{ "Attributes": { "Endpoint": "my-email@example.com", "Protocol": "email", "RawMessageDelivery": "false", "ConfirmationWasAuthenticated": "false", "Owner": "123456789012", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" } }

以下代码示例演示如何使用 get-topic-attributes

Amazon CLI

检索主题的属性

以下 get-topic-attributes 示例将显示指定主题的属性。

aws sns get-topic-attributes \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

输出:

{ "Attributes": { "SubscriptionsConfirmed": "1", "DisplayName": "my-topic", "SubscriptionsDeleted": "0", "EffectiveDeliveryPolicy": "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}", "Owner": "123456789012", "Policy": "{\"Version\":\"2008-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:DeleteTopic\",\"SNS:GetTopicAttributes\",\"SNS:Publish\",\"SNS:RemovePermission\",\"SNS:AddPermission\",\"SNS:SetTopicAttributes\"],\"Resource\":\"arn:aws:sns:us-west-2:123456789012:my-topic\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"0123456789012\"}}}]}", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionsPending": "0" } }
  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考GetTopicAttributes中的。

以下代码示例演示如何使用 list-endpoints-by-platform-application

Amazon CLI

列出平台应用程序的终端节点

以下list-endpoints-by-platform-application示例列出了指定平台应用程序的终端节点和终端节点属性。

aws sns list-endpoints-by-platform-application \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication

输出:

{ "Endpoints": [ { "Attributes": { "Token": "EXAMPLE12345..., "Enabled": "true" }, "EndpointArn": "arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234" } ] }

以下代码示例演示如何使用 list-phone-numbers-opted-out

Amazon CLI

列出 SMS 消息退出

以下 list-phone-numbers-opted-out 示例将列出退出 SMS 消息接收的电话号码。

aws sns list-phone-numbers-opted-out

输出:

{ "phoneNumbers": [ "+15555550100" ] }

以下代码示例演示如何使用 list-platform-applications

Amazon CLI

列出平台应用程序

以下list-platform-applications示例列出了 ADM 和 MPNS 的平台应用程序。

aws sns list-platform-applications

输出:

{ "PlatformApplications": [ { "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/ADM/MyApplication", "Attributes": { "SuccessFeedbackSampleRate": "100", "Enabled": "true" } }, { "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/MPNS/MyOtherApplication", "Attributes": { "SuccessFeedbackSampleRate": "100", "Enabled": "true" } } ] }

以下代码示例演示如何使用 list-subscriptions-by-topic

Amazon CLI

列出与主题关联的订阅

以下内容list-subscriptions-by-topic检索与指定主题关联的 SNS 订阅列表。

aws sns list-subscriptions-by-topic \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

输出:

{ "Subscriptions": [ { "Owner": "123456789012", "Endpoint": "my-email@example.com", "Protocol": "email", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" } ] }

以下代码示例演示如何使用 list-subscriptions

Amazon CLI

列出 SNS 订阅

以下list-subscriptions示例显示了您 Amazon 账户中的 SNS 订阅列表。

aws sns list-subscriptions

输出:

{ "Subscriptions": [ { "Owner": "123456789012", "Endpoint": "my-email@example.com", "Protocol": "email", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" } ] }
  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考ListSubscriptions中的。

以下代码示例演示如何使用 list-tags-for-resource

Amazon CLI

列出主题的标签

以下list-tags-for-resource示例列出了指定 Amazon SNS 主题的标签。

aws sns list-tags-for-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic

输出:

{ "Tags": [ { "Key": "Team", "Value": "Alpha" } ] }

以下代码示例演示如何使用 list-topics

Amazon CLI

列出 SNS 主题

以下list-topics示例列出了您 Amazon 账户中的所有 SNS 主题。

aws sns list-topics

输出:

{ "Topics": [ { "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" } ] }
  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考ListTopics中的。

以下代码示例演示如何使用 opt-in-phone-number

Amazon CLI

选择接收 SMS 消息

以下opt-in-phone-number示例选择指定的电话号码接收 SMS 消息。

aws sns opt-in-phone-number \ --phone-number +15555550100

此命令不生成任何输出。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考OptInPhoneNumber中的。

以下代码示例演示如何使用 publish

Amazon CLI

示例 1:向主题发布消息

以下 publish 示例将指定消息发布到指定 SNS 主题。该消息来自一个文本文件,您可以在该文件中包含换行符。

aws sns publish \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic" \ --message file://message.txt

message.txt 的内容:

Hello World Second Line

输出:

{ "MessageId": "123a45b6-7890-12c3-45d6-111122223333" }

示例 2:向电话号码发布 SMS 消息

以下 publish 示例将消息 Hello world! 发布到电话号码 +1-555-555-0100

aws sns publish \ --message "Hello world!" \ --phone-number +1-555-555-0100

输出:

{ "MessageId": "123a45b6-7890-12c3-45d6-333322221111" }
  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 Publish

以下代码示例演示如何使用 put-data-protection-policy

Amazon CLI

设置数据保护政策

示例 1:拒绝发布者使用发布消息 CreditCardNumber

以下put-data-protection-policy示例拒绝发布者使用发布消息 CreditCardNumber。

aws sns put-data-protection-policy \ --resource-arn arn:aws:sns:us-east-1:123456789012:mytopic \ --data-protection-policy "{\"Name\":\"data_protection_policy\",\"Description\":\"Example data protection policy\",\"Version\":\"2021-06-01\",\"Statement\":[{\"DataDirection\":\"Inbound\",\"Principal\":[\"*\"],\"DataIdentifier\":[\"arn:aws:dataprotection::aws:data-identifier/CreditCardNumber\"],\"Operation\":{\"Deny\":{}}}]}"

此命令不生成任何输出。

示例 2:从文件加载参数

以下内容从文件put-data-protection-policy加载参数。

aws sns put-data-protection-policy \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --data-protection-policy file://policy.json

此命令不生成任何输出。

以下代码示例演示如何使用 remove-permission

Amazon CLI

从主题中移除权限

以下remove-permission示例Publish-Permission从指定主题中移除权限。

aws sns remove-permission \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --label Publish-Permission

此命令不生成任何输出。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考RemovePermission中的。

以下代码示例演示如何使用 set-endpoint-attributes

Amazon CLI

设置端点属性

以下set-endpoint-attributes示例禁用了指定的平台应用程序终端节点。

aws sns set-endpoint-attributes \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234 \ --attributes Enabled=false

输出:

{ "Attributes": { "Enabled": "false", "Token": "EXAMPLE12345..." } }

以下代码示例演示如何使用 set-platform-application-attributes

Amazon CLI

设置平台应用程序属性

以下set-platform-application-attributes示例将指定平台应用程序的EventDeliveryFailure属性设置为指定 Amazon SNS 主题的 ARN。

aws sns set-platform-application-attributes \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication \ --attributes EventDeliveryFailure=arn:aws:sns:us-west-2:123456789012:AnotherTopic

此命令不生成任何输出。

以下代码示例演示如何使用 set-sms-attributes

Amazon CLI

设置 SMS 消息属性

以下 set-sms-attributes 示例将 SMS 消息的默认发件人 ID 设置为 MyName

aws sns set-sms-attributes \ --attributes DefaultSenderID=MyName

此命令不生成任何输出。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 SetSMSAttributes

以下代码示例演示如何使用 set-subscription-attributes

Amazon 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 "{}"

此命令不生成任何输出。

以下代码示例演示如何使用 set-topic-attributes

Amazon CLI

为主题设置属性

以下 set-topic-attributes 示例为指定主题设置 DisplayName 属性。

aws sns set-topic-attributes \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --attribute-name DisplayName \ --attribute-value MyTopicDisplayName

此命令不生成任何输出。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考SetTopicAttributes中的。

以下代码示例演示如何使用 subscribe

Amazon CLI

订阅主题

以下 subscribe 命令将电子邮件地址订阅到指定主题。

aws sns subscribe \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --protocol email \ --notification-endpoint my-email@example.com

输出:

{ "SubscriptionArn": "pending confirmation" }
  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 Subscribe

以下代码示例演示如何使用 tag-resource

Amazon CLI

为主题添加标签

以下 tag-resource 示例将元数据标签添加到指定 Amazon SNS 主题。

aws sns tag-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --tags Key=Team,Value=Alpha

此命令不生成任何输出。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考TagResource中的。

以下代码示例演示如何使用 unsubscribe

Amazon CLI

从主题取消订阅

以下 unsubscribe 示例将从主题删除指定的订阅。

aws sns unsubscribe \ --subscription-arn arn:aws:sns:us-west-2:0123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f

此命令不生成任何输出。

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 Unsubscribe

以下代码示例演示如何使用 untag-resource

Amazon CLI

从主题中移除标签

以下untag-resource示例从指定的 Amazon SNS 主题中删除所有带有指定密钥的标签。

aws sns untag-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --tag-keys Team

此命令不生成任何输出。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考UntagResource中的。

场景

以下代码示例展示如何为 Amazon SNS 推送通知创建平台终端节点。

Amazon CLI

创建平台应用程序端点

以下 create-platform-endpoint 示例使用指定令牌为指定平台应用程序创建端点。

aws sns create-platform-endpoint \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication \ --token EXAMPLE12345...

输出:

{ "EndpointArn": "arn:aws:sns:us-west-2:1234567890:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234" }