

适用于 JavaScript 的 AWS SDK v2 已终止支持。建议您迁移到 [适用于 JavaScript 的 AWS SDK v3](https://docs.aws.amazon.com//sdk-for-javascript/v3/developer-guide/)。有关更多详情和如何迁移的信息，请参阅本[公告](https://aws.amazon.com/blogs//developer/announcing-end-of-support-for-aws-sdk-for-javascript-v2/)。

# 在 Amazon SNS 中管理订阅
<a name="sns-examples-subscribing-unubscribing-topics"></a>

![\[JavaScript code example that applies to Node.js execution\]](http://docs.aws.amazon.com/zh_cn/sdk-for-javascript/v2/developer-guide/images/nodeicon.png)

**此 Node.js 代码示例演示：**
+ 如何列出对 Amazon SNS 主题的所有订阅。
+ 如何将电子邮件地址、应用程序端点或 AWS Lambda 函数订阅到 Amazon SNS 主题。
+ 如何从 Amazon SNS 主题取消订阅。

## 情景
<a name="sns-examples-subscribing-unubscribing-yopics-scenario"></a>

在本示例中，您使用一系列 Node.js 模块将通知消息发布到 Amazon SNS 主题。这些 Node.js 模块使用 SDK for JavaScript，通过 `AWS.SNS` 客户端类的以下方法管理主题：
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#subscribe-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#subscribe-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#confirmSubscription-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#confirmSubscription-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#listSubscriptionsByTopic-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#listSubscriptionsByTopic-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#unsubscribe-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#unsubscribe-property)

## 先决条件任务
<a name="sns-examples-subscribing-unubscribing-topics-prerequisites"></a>

要设置和运行此示例，您必须先完成以下任务：
+ 安装 Node.js。有关安装 Node.js 的更多信息，请参阅 [Node.js 网站](http://nodejs.org)。
+ 使用用户凭证创建共享配置文件。有关提供凭证 JSON 文件的更多信息，请参阅[从共享凭证文件加载 Node.js 中的凭证](loading-node-credentials-shared.md)。

## 列出对主题的订阅
<a name="sns-examples-list-subscriptions-email"></a>

本示例使用 Node.js 模块列出对 Amazon SNS 主题的所有订阅。创建文件名为 `sns_listsubscriptions.js` 的 Node.js 模块。按前面所示配置 SDK。

创建一个对象，其中包含您要列出其订阅的主题的 `TopicArn` 参数。将参数传递到 `listSubscriptionsByTopic` 客户端类的 `AWS.SNS` 方法。要调用 `listSubscriptionsByTopic` 方法，请创建一个 promise 来调用 Amazon SNS 服务对象并传递参数对象。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set region
AWS.config.update({ region: "REGION" });

const params = {
  TopicArn: "TOPIC_ARN",
};

// Create promise and SNS service object
var subslistPromise = new AWS.SNS({ apiVersion: "2010-03-31" })
  .listSubscriptionsByTopic(params)
  .promise();

// Handle promise's fulfilled/rejected states
subslistPromise
  .then(function (data) {
    console.log(data);
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。

```
node sns_listsubscriptions.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sns/sns_listsubscriptions.js)找到。

## 将电子邮件地址订阅到主题
<a name="sns-examples-subscribing-email"></a>

本示例使用 Node.js 模块将电子邮件地址订阅到 Amazon SNS 主题，以从该主题接收 SMTP 电子邮件。创建文件名为 `sns_subscribeemail.js` 的 Node.js 模块。按前面所示配置 SDK。

创建包含 `Protocol` 参数的对象，用于指定 `email` 协议、要订阅到的主题的 `TopicArn` 以及作为邮件 `Endpoint` 的电子邮件地址。将参数传递到 `subscribe` 客户端类的 `AWS.SNS` 方法。您可以使用 `subscribe` 方法，根据在所传递参数中使用的值，将多种不同的端点订阅到某个 Amazon SNS 主题，如本主题中的其它示例所示。

要调用 `subscribe` 方法，请创建一个 promise 来调用 Amazon SNS 服务对象并传递参数对象。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set region
AWS.config.update({ region: "REGION" });

// Create subscribe/email parameters
var params = {
  Protocol: "EMAIL" /* required */,
  TopicArn: "TOPIC_ARN" /* required */,
  Endpoint: "EMAIL_ADDRESS",
};

// Create promise and SNS service object
var subscribePromise = new AWS.SNS({ apiVersion: "2010-03-31" })
  .subscribe(params)
  .promise();

// Handle promise's fulfilled/rejected states
subscribePromise
  .then(function (data) {
    console.log("Subscription ARN is " + data.SubscriptionArn);
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。

```
node sns_subscribeemail.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sns/sns_subscribeemail.js)找到。

## 将应用程序端点订阅到主题
<a name="sns-examples-subscribing-apps"></a>

本示例使用 Node.js 模块将移动应用程序端点订阅到 Amazon SNS 主题，以接收通知。创建文件名为 `sns_subscribeapp.js` 的 Node.js 模块。按前面所示配置 SDK。

创建一个包含 `Protocol` 参数的对象，用于指定 `application` 协议、要订阅到的主题的 `TopicArn` 以及 `Endpoint` 参数的移动应用程序终端节点的 ARN。将参数传递到 `subscribe` 客户端类的 `AWS.SNS` 方法。

要调用 `subscribe` 方法，请创建一个 promise 来调用 Amazon SNS 服务对象并传递参数对象。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set region
AWS.config.update({ region: "REGION" });

// Create subscribe/email parameters
var params = {
  Protocol: "application" /* required */,
  TopicArn: "TOPIC_ARN" /* required */,
  Endpoint: "MOBILE_ENDPOINT_ARN",
};

// Create promise and SNS service object
var subscribePromise = new AWS.SNS({ apiVersion: "2010-03-31" })
  .subscribe(params)
  .promise();

// Handle promise's fulfilled/rejected states
subscribePromise
  .then(function (data) {
    console.log("Subscription ARN is " + data.SubscriptionArn);
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。

```
node sns_subscribeapp.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sns/sns_subscribeapp.js)找到。

## 将 Lambda 函数订阅到主题
<a name="sns-examples-subscribing-lambda"></a>

本示例使用 Node.js 模块将 AWS Lambda 函数订阅到 Amazon SNS 主题，以接收通知。创建文件名为 `sns_subscribelambda.js` 的 Node.js 模块。按前面所示配置 SDK。

创建一个包含 `Protocol` 参数的对象，指定 `lambda` 协议、要订阅到的主题的 `TopicArn` 以及作为 AWS Lambda 参数的 `Endpoint` 函数的 ARN。将参数传递到 `subscribe` 客户端类的 `AWS.SNS` 方法。

要调用 `subscribe` 方法，请创建一个 promise 来调用 Amazon SNS 服务对象并传递参数对象。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set region
AWS.config.update({ region: "REGION" });

// Create subscribe/email parameters
var params = {
  Protocol: "lambda" /* required */,
  TopicArn: "TOPIC_ARN" /* required */,
  Endpoint: "LAMBDA_FUNCTION_ARN",
};

// Create promise and SNS service object
var subscribePromise = new AWS.SNS({ apiVersion: "2010-03-31" })
  .subscribe(params)
  .promise();

// Handle promise's fulfilled/rejected states
subscribePromise
  .then(function (data) {
    console.log("Subscription ARN is " + data.SubscriptionArn);
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。

```
node sns_subscribelambda.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sns/sns_subscribelambda.js)找到。

## 从主题取消订阅
<a name="sns-examples-unsubscribing"></a>

本示例使用 Node.js 模块取消对 Amazon SNS 主题的订阅。创建文件名为 `sns_unsubscribe.js` 的 Node.js 模块。按前面所示配置 SDK。

创建一个包含 `SubscriptionArn` 参数的对象，指定要取消订阅的订阅的 ARN。将参数传递到 `unsubscribe` 客户端类的 `AWS.SNS` 方法。

要调用 `unsubscribe` 方法，请创建一个 promise 来调用 Amazon SNS 服务对象并传递参数对象。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set region
AWS.config.update({ region: "REGION" });

// Create promise and SNS service object
var subscribePromise = new AWS.SNS({ apiVersion: "2010-03-31" })
  .unsubscribe({ SubscriptionArn: TOPIC_SUBSCRIPTION_ARN })
  .promise();

// Handle promise's fulfilled/rejected states
subscribePromise
  .then(function (data) {
    console.log(data);
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。

```
node sns_unsubscribe.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sns/sns_unsubscribe.js)找到。