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

使用Amazon CLI的 Amazon SNS 示例

以下代码示例演示了如何通过将 Amazon Command Line Interface与 Amazon SNS 结合使用,来执行操作和实现常见场景。

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

场景是向您演示如何通过在一个服务中调用多个函数或与其他 Amazon Web Services 服务 结合来完成特定任务的代码示例。

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

操作

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

Amazon CLI

向主题添加权限

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

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" }
  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 ConfirmSubscription

以下代码示例演示了如何使用 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 Command Line Interface 用户指南》中的将 Amazon Command Line Interface与 Amazon SQS 和 Amazon SNS 结合使用

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 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 Command Reference》中的 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 将显示指定订阅的属性。您可以从 list-subscriptions 命令输出中获取 subscription-arn

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 Command Reference》中的 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 Command Reference》中的 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" } ] }
  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 ListTagsForResource

以下代码示例演示了如何使用 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 Command Reference》中的 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 Command Reference》中的 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 命令参考》中的 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 命令参考》中的 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" }