AWS SDK for JavaScript V3 參API考指南詳細描述了 AWS SDK for JavaScript 版本 3(V3)的所有API操作。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
管理 Amazon SNS 中的主題
這個 Node.js 程式碼範例會說明:
-
如何在 Amazon SNS 中建立可以向其發佈通知的主題。
-
如何刪除在 Amazon SNS 創建的主題。
-
如何取得可用主題的清單。
-
如何取得和設定主題屬性。
使用案例
在此範例中,您使用一系列 Node.js 模組來建立、列出和刪除 Amazon SNS 主題,以及處理主題屬性。Node.js 模組會使用 SDK JavaScript 來管理使用用SNS
戶端類別的下列方法的主題:
先決條件任務
若要設定和執行此範例,您必須先完成這些任務:
-
設置項目環境以運行這些節點 TypeScript 示例,並安裝所需AWS SDK for JavaScript的第三方模塊。按照上的說明進行操作 GitHub
。 -
透過使用者登入資料建立共用組態檔。有關提供共用認證檔案的詳細資訊,請參閱 AWSSDK 和工具參考指南中的共用設定和認證檔案。
重要
這些範例示範如何使用 ECMAScript6 (ES6) 匯入/匯出用戶端服務物件和指令。
這需要 Node.js 版本 13.x 或更高版本。要下載並安裝最新版本的 Node.js,請參閱 Node.js 下載。
。 如果您偏好使用 CommonJS 語法,請參閱JavaScript ES6/共同語法。
建立主題
在此範例中,使用 Node.js 模組建立 Amazon SNS 主題。
創建一個libs
目錄,並使用文件名創建一個 Node.js 模塊snsClient.js
。將下列程式碼複製並貼到其中,以建立 Amazon SNS 用戶端物件。以您的地區
取代「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({});
您可以在這裡
以檔名 create-topic.js
建立一個 Node.js 模組。如先前所示設定 SDK,包括安裝所需的用戶端和套件。
建立一個物件,藉此將新主題的 Name
傳遞至 SNS
用戶端類別的 CreateTopicCommand
方法。若要呼叫方CreateTopicCommand
法,請建立叫用 Amazon SNS 服務物件的非同步函數,並傳遞參數物件。傳data
回的包含主題的 ARN。
注意
將主題名稱
取代為主題名稱。
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
您可以在這裡
列出 主題
在此範例中,使用 Node.js 模組列出所有 Amazon SNS 主題。
創建一個libs
目錄,並使用文件名創建一個 Node.js 模塊snsClient.js
。將下列程式碼複製並貼到其中,以建立 Amazon SNS 用戶端物件。以您的地區
取代「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({});
您可以在這裡
以檔名 list-topics.js
建立一個 Node.js 模組。如先前所示設定 SDK,包括安裝所需的用戶端和套件。
建立空白物件,並將其傳遞至 SNS
用戶端類別的 ListTopicsCommand
方法。若要呼叫方ListTopicsCommand
法,請建立叫用 Amazon SNS 服務物件的非同步函數,並傳遞參數物件。data
返回的包含您的主題亞馬遜資源名稱(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
您可以在這裡
刪除主題
在此範例中,使用 Node.js 模組刪除 Amazon SNS 主題。
創建一個libs
目錄,並使用文件名創建一個 Node.js 模塊snsClient.js
。將下列程式碼複製並貼到其中,以建立 Amazon SNS 用戶端物件。以您的地區
取代「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({});
您可以在這裡
以檔名 delete-topic.js
建立一個 Node.js 模組。如先前所示設定 SDK,包括安裝所需的用戶端和套件。
建立包含欲刪除主題 TopicArn
的物件,並將其傳遞至 SNS
用戶端類別的 DeleteTopicCommand
方法。若要呼叫方DeleteTopicCommand
法,請建立叫用 Amazon SNS 用戶端服務物件的非同步函數,並傳遞參數物件。
注意
將 TOPIC_ARN
取代為您要刪除之主題的亞馬遜資源名稱 (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
您可以在這裡
取得主題屬性
在此範例中,使用 Node.js 模組擷取 Amazon SNS 主題的屬性。
創建一個libs
目錄,並使用文件名創建一個 Node.js 模塊snsClient.js
。將下列程式碼複製並貼到其中,以建立 Amazon SNS 用戶端物件。以您的地區
取代「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({});
您可以在這裡
以檔名 get-topic-attributes.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立包含欲刪除主題 TopicArn
的物件,並將其傳遞至 SNS
用戶端類別的 GetTopicAttributesCommand
方法。若要呼叫方GetTopicAttributesCommand
法,請呼叫 Amazon SNS 用戶端服務物件,並傳遞參數物件。
注意
以主題的 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
您可以在這裡
設定主題屬性
在此範例中,使用 Node.js 模組來設定 Amazon SNS 主題的可變屬性。
創建一個libs
目錄,並使用文件名創建一個 Node.js 模塊snsClient.js
。將下列程式碼複製並貼到其中,以建立 Amazon SNS 用戶端物件。以您的地區
取代「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({});
您可以在這裡
以檔名 set-topic-attributes.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立一個物件,其中包含用來更新屬性的參數,包括要設定屬性的主題 TopicArn
、要設定的屬性名稱,以及該屬性的新數值。您只能設定 Policy
、DisplayName
和 DeliveryPolicy
屬性。將參數傳遞至 SNS
用戶端類別的 SetTopicAttributesCommand
方法。若要呼叫方SetTopicAttributesCommand
法,請建立叫用 Amazon SNS 用戶端服務物件的非同步函數,並傳遞參數物件。
注意
將屬性名稱取代為您正在設定的屬性的名稱,將
_VALUE。TOPIC_ARN
取代為您要設定其屬性之主題的亞馬遜資源名稱 (ARN),並以該屬性的新值取代 NEW
_ATERTE
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
您可以在這裡