在 Amazon SNS 中管理主题 - AWS SDK for JavaScript

AWS SDK for JavaScript V3 API 参考指南详细描述了 AWS SDK for JavaScript 版本 3 (V3) 的所有 API 操作。

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

在 Amazon SNS 中管理主题


                    JavaScript code example that applies to Node.js execution

此 Node.js 代码示例演示:

  • 如何在 Amazon SNS 中创建可以将通知发布到的主题。

  • 如何删除在 Amazon SNS 中创建的主题。

  • 如何获取可用主题的列表。

  • 如何获取和设置主题属性。

情景

在本示例中,您使用一系列 Node.js 模块来创建、列出和删除 Amazon SNS 主题,以及处理主题属性。Node.js 模块使用 SDK for JavaScript,通过 SNS 客户端类的以下方法管理主题:

先决条件任务

要设置和运行此示例,您必须先完成以下任务:

  • 设置项目环境以运行这些 Node TypeScript 示例,并安装所需的 AWS SDK for JavaScript 和第三方模块。请按照 GitHub 上的说明进行操作。

  • 使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息,请参阅《AWS SDK 和工具参考指南》 中的共享配置和凭证文件

重要

这些示例演示了如何使用 ECMAScript6 (ES6) 导入/导出客户端服务对象和命令。

创建主题

在本示例中,使用 Node.js 模块创建 Amazon SNS 主题。

创建一个 libs 目录,然后使用文件名 snsClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SNS 客户端对象。将 REGION 替换为您的 AWS 区域。

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

此示例代码可在 GitHub 上的此处找到。

创建文件名为 create-topic.js 的 Node.js 模块。如前所示配置 SDK,包括安装所需的客户端和软件包。

创建对象,将新主题的 Name 传递到 SNS 客户端类的 CreateTopicCommand 方法。要调用 CreateTopicCommand 方法,请创建一个用于调用 Amazon SNS 服务对象的异步函数并传递参数对象。返回的 data 包含主题的 ARN。

注意

TOPIC_NAME 替换为主题名称。

import { CreateTopicCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicName - The name of the topic to create. */ export const createTopic = async (topicName = "TOPIC_NAME") => { const response = await snsClient.send( new CreateTopicCommand({ Name: topicName }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '087b8ad2-4593-50c4-a496-d7e90b82cf3e', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:TOPIC_NAME' // } return response; };

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

node create-topic.js

此示例代码可在 GitHub 上的此处找到。

列出主题

在本示例中,使用 Node.js 模块列出所有 Amazon SNS 主题。

创建一个 libs 目录,然后使用文件名 snsClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SNS 客户端对象。将 REGION 替换为您的 AWS 区域。

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

此示例代码可在 GitHub 上的此处找到。

创建文件名为 list-topics.js 的 Node.js 模块。如前所示配置 SDK,包括安装所需的客户端和软件包。

创建一个空对象以传递到 SNS 客户端类的 ListTopicsCommand 方法。要调用 ListTopicsCommand 方法,请创建一个用于调用 Amazon SNS 服务对象的异步函数并传递参数对象。返回的 data 包含您的主题 Amazon 资源名称 (ARN) 的一个数组。

import { ListTopicsCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; export const listTopics = async () => { const response = await snsClient.send(new ListTopicsCommand({})); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '936bc5ad-83ca-53c2-b0b7-9891167b909e', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // Topics: [ { TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:mytopic' } ] // } return response; };

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

node list-topics.js

此示例代码可在 GitHub 上的此处找到。

删除主题

在本示例中,使用 Node.js 模块删除 Amazon SNS 主题。

创建一个 libs 目录,然后使用文件名 snsClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SNS 客户端对象。将 REGION 替换为您的 AWS 区域。

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

此示例代码可在 GitHub 上的此处找到。

创建文件名为 delete-topic.js 的 Node.js 模块。如前所示配置 SDK,包括安装所需的客户端和软件包。

创建包含要删除的主题的 TopicArn 的对象,将其传递到 SNS 客户端类的 DeleteTopicCommand 方法。要调用 DeleteTopicCommand方法,请创建一个异步函数,调用 Amazon SNS 客户端服务对象并传递参数对象。

注意

TOPIC_ARN 替换为要删除的主题的 Amazon 资源名称 (ARN)。

import { DeleteTopicCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic to delete. */ export const deleteTopic = async (topicArn = "TOPIC_ARN") => { const response = await snsClient.send( new DeleteTopicCommand({ TopicArn: topicArn }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'a10e2886-5a8f-5114-af36-75bd39498332', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // } // } };

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

node delete-topic.js

此示例代码可在 GitHub 上的此处找到。

获取主题属性

在本示例中,使用 Node.js 模块检索 Amazon SNS 主题的属性。

创建一个 libs 目录,然后使用文件名 snsClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SNS 客户端对象。将 REGION 替换为您的 AWS 区域。

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

此示例代码可在 GitHub 上的此处找到。

创建文件名为 get-topic-attributes.js 的 Node.js 模块。按前面所示配置 SDK。

创建包含要删除主题的 TopicArn 的对象,将其传递到 SNS 客户端类的 GetTopicAttributesCommand 方法。要调用 GetTopicAttributesCommand 方法,请调用一个 Amazon SNS 客户端服务对象来传递参数对象。

注意

TOPIC_ARN 替换为主题的 ARN。

import { GetTopicAttributesCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic to retrieve attributes for. */ export const getTopicAttributes = async (topicArn = "TOPIC_ARN") => { const response = await snsClient.send( new GetTopicAttributesCommand({ TopicArn: topicArn, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '36b6a24e-5473-5d4e-ac32-ff72d9a73d94', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // Attributes: { // Policy: '{...}', // Owner: 'xxxxxxxxxxxx', // SubscriptionsPending: '1', // TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:mytopic', // TracingConfig: 'PassThrough', // EffectiveDeliveryPolicy: '{"http":{"defaultHealthyRetryPolicy":{"minDelayTarget":20,"maxDelayTarget":20,"numRetries":3,"numMaxDelayRetries":0,"numNoDelayRetries":0,"numMinDelayRetries":0,"backoffFunction":"linear"},"disableSubscriptionOverrides":false,"defaultRequestPolicy":{"headerContentType":"text/plain; charset=UTF-8"}}}', // SubscriptionsConfirmed: '0', // DisplayName: '', // SubscriptionsDeleted: '1' // } // } return response; };

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

node get-topic-attributes.js

此示例代码可在 GitHub 上的此处找到。

设置主题属性

在本示例中,使用 Node.js 模块设置 Amazon SNS 主题的可变属性。

创建一个 libs 目录,然后使用文件名 snsClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SNS 客户端对象。将 REGION 替换为您的 AWS 区域。

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

此示例代码可在 GitHub 上的此处找到。

创建文件名为 set-topic-attributes.js 的 Node.js 模块。按前面所示配置 SDK。

创建包含用于属性更新参数的对象,这包括要设置其属性的主题的 TopicArn、要设置的属性的名称以及该属性的新值。您只能设置 PolicyDisplayNameDeliveryPolicy 属性。将参数传递到 SNS 客户端类的 SetTopicAttributesCommand 方法。要调用 SetTopicAttributesCommand方法,请创建一个异步函数,调用 Amazon SNS 客户端服务对象并传递参数对象。

注意

ATTRIBUTE_NAME 替换为您正在设置的属性的名称,将 TOPIC_ARN 替换为您要设置属性的主题的 Amazon 资源名称 (ARN),将 NEW_ATTRIBUTE_VALUE 替换为该属性的新值。

import { SetTopicAttributesCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; export const setTopicAttributes = async ( topicArn = "TOPIC_ARN", attributeName = "DisplayName", attributeValue = "Test Topic", ) => { const response = await snsClient.send( new SetTopicAttributesCommand({ AttributeName: attributeName, AttributeValue: attributeValue, TopicArn: topicArn, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'd1b08d0e-e9a4-54c3-b8b1-d03238d2b935', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // } // } return response; };

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

node set-topic-attributes.js

此示例代码可在 GitHub 上的此处找到。