

# 在 AWS CLI 中访问 Amazon SNS
<a name="cli-services-sns"></a>

您可以使用 AWS Command Line Interface (AWS CLI)访问 Amazon Simple Notification Service (Amazon SNS) 的功能。要列出适用于 Amazon SNS 的 AWS CLI 命令，请使用以下命令。

```
aws sns help
```

在运行任何命令之前，请设置默认证书。有关更多信息，请参阅 [配置 AWS CLI 设置](cli-chap-configure.md)。

本主题显示执行 Amazon SNS 常见任务的 AWS CLI 命令。

**Topics**
+ [创建主题](#cli-create-sns-topic)
+ [订阅主题](#cli-subscribe-sns-topic)
+ [向主题发布](#cli-publish-sns-topic)
+ [取消订阅主题](#cli-unsubscribe-sns-topic)
+ [删除主题](#cli-delete-sns-topic)

## 创建主题
<a name="cli-create-sns-topic"></a>

要创建主题，请使用 [https://docs.aws.amazon.com/cli/latest/reference/sns/create-topic.html](https://docs.aws.amazon.com/cli/latest/reference/sns/create-topic.html) 命令并指定要分配给该主题的名称。

```
$ aws sns create-topic --name my-topic
{
    "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic"
}
```

记下响应的 `TopicArn`，您随后将用它来发布消息。

## 订阅主题
<a name="cli-subscribe-sns-topic"></a>

要订阅主题，请使用 [https://docs.aws.amazon.com/cli/latest/reference/sns/subscribe.html](https://docs.aws.amazon.com/cli/latest/reference/sns/subscribe.html) 命令。

以下示例为 `email` 指定 `notification-endpoint` 协议和电子邮件地址。

```
$ aws sns subscribe --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic --protocol email --notification-endpoint saanvi@example.com
{
    "SubscriptionArn": "pending confirmation"
}
```

AWS 通过向您在 `subscribe` 命令中指定的地址发送电子邮件，立即发送确认电子邮件。电子邮件具有以下文本。

```
You have chosen to subscribe to the topic:
arn:aws:sns:us-west-2:123456789012:my-topic
To confirm this subscription, click or visit the following link (If this was in error no action is necessary):
Confirm subscription
```

收件人单击**确认订阅**链接后，收件人的浏览器显示通知消息，信息类似于以下内容。

```
Subscription confirmed!

You have subscribed saanvi@example.com to the topic:my-topic.

Your subscription's id is:
arn:aws:sns:us-west-2:123456789012:my-topic:1328f057-de93-4c15-512e-8bb22EXAMPLE

If it was not your intention to subscribe, click here to unsubscribe.
```

## 向主题发布
<a name="cli-publish-sns-topic"></a>

要将消息发送给某一主题的所有订阅者，请使用 [https://docs.aws.amazon.com/cli/latest/reference/sns/publish.html](https://docs.aws.amazon.com/cli/latest/reference/sns/publish.html) 命令。

以下示例发送消息“Hello World\$1” 特定主体的所有订阅者。

```
$ aws sns publish --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic --message "Hello World!"
{
    "MessageId": "4e41661d-5eec-5ddf-8dab-2c867EXAMPLE"
}
```

在本示例中，AWS 将包含文本“Hello World\$1”的电子邮件发送 到 `saanvi@example.com`。

## 取消订阅主题
<a name="cli-unsubscribe-sns-topic"></a>

要取消订阅某个主题并停止接收向该主题发布的消息，请使用 [https://docs.aws.amazon.com/cli/latest/reference/sns/unsubscribe.html](https://docs.aws.amazon.com/cli/latest/reference/sns/unsubscribe.html) 命令并指定您要取消订阅的主题的 ARN。

```
$ aws sns unsubscribe --subscription-arn arn:aws:sns:us-west-2:123456789012:my-topic:1328f057-de93-4c15-512e-8bb22EXAMPLE
```

要验证您已成功取消订阅，请使用 [https://docs.aws.amazon.com/cli/latest/reference/sns/list-subscriptions.html](https://docs.aws.amazon.com/cli/latest/reference/sns/list-subscriptions.html) 命令以确认该 ARN 不再显示在列表中。

```
$ aws sns list-subscriptions
```

## 删除主题
<a name="cli-delete-sns-topic"></a>

要删除主题，请运行 [https://docs.aws.amazon.com/cli/latest/reference/sns/delete-topic.html](https://docs.aws.amazon.com/cli/latest/reference/sns/delete-topic.html) 命令。

```
$ aws sns delete-topic --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic
```

要验证 AWS 是否已成功删除主题，请使用 [https://docs.aws.amazon.com/cli/latest/reference/sns/list-topics.html](https://docs.aws.amazon.com/cli/latest/reference/sns/list-topics.html) 命令以确认该主题不再显示在列表中。

```
$ aws sns list-topics
```