管理 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 主題的AWS Lambda函數。

  • 如何退訂 Amazon SNS 主題。

使用案例

在此範例中,您會使用一系列 Node.js 模組將通知訊息發佈到 Amazon SNS 主題。Node.js 模組會使用 SDK JavaScript 來管理使用用SNS戶端類別的下列方法的主題:

先決條件任務

若要設定和執行此範例,您必須先完成這些任務:

  • 設置項目環境以運行這些節點 TypeScript 示例,並安裝所需AWS SDK for JavaScript的第三方模塊。按照上的說明進行操作 GitHub

  • 透過使用者登入資料建立共用組態檔。有關提供共用認證檔案的詳細資訊,請參閱 AWSSDK 和工具參考指南中的共用設定和認證檔案。

重要

這些範例示範如何使用 ECMAScript6 (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({});

您可以在這裡找到此範例程式碼 GitHub。

以檔名 list-subscriptions-by-topic.js 建立一個 Node.js 模組。依前述內容設定軟體開發套件。

建立一個物件,其中包含要列出訂閱的主題 TopicArn 參數。將參數傳遞至 SNS 用戶端類別的 ListSubscriptionsByTopicCommand 方法。若要呼叫方ListSubscriptionsByTopicCommand法,請建立叫用 Amazon SNS 用戶端服務物件的非同步函數,然後傳遞參數物件。

注意

TOPIC_ARN 取代為您要列出其訂閱的主題的亞馬遜資源名稱 (ARN)。

import { ListSubscriptionsByTopicCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic for which you wish to list subscriptions. */ export const listSubscriptionsByTopic = async (topicArn = "TOPIC_ARN") => { const response = await snsClient.send( new ListSubscriptionsByTopicCommand({ TopicArn: topicArn }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '0934fedf-0c4b-572e-9ed2-a3e38fadb0c8', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // Subscriptions: [ // { // SubscriptionArn: 'PendingConfirmation', // Owner: '901487484989', // Protocol: 'email', // Endpoint: 'corepyle@amazon.com', // TopicArn: 'arn:aws:sns:us-east-1:901487484989:mytopic' // } // ] // } return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node list-subscriptions-by-topic.js

您可以在這裡找到此範例程式碼 GitHub。

使用電子郵件地址訂閱主題

在此範例中,使用 Node.js 模組訂閱電子郵件地址,以便接收來自 Amazon SNS 主題的 SMTP 電子郵件訊息。

創建一個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({});

您可以在這裡找到此範例程式碼 GitHub。

以檔名 subscribe-email.js 建立一個 Node.js 模組。依前述內容設定軟體開發套件。

建立包含 Protocol 參數的物件,以便指定 email 通訊協定、要訂閱的主題 TopicArn,以及要做為訊息 Endpoint 的電子郵件地址。將參數傳遞至 SNS 用戶端類別的 SubscribeCommand 方法。您可以使用該subscribe方法將多個不同的端點訂閱 Amazon SNS 主題,具體取決於用於傳遞參數的值,如本主題中的其他範例所示。

若要呼叫方SubscribeCommand法,請建立叫用 Amazon SNS 用戶端服務物件的非同步函數,然後傳遞參數物件。

注意

TOPIC_ARN 取代為該主題的亞馬遜資源名稱 (ARN),將電子郵件地址取代為要訂閱的電子郵件地址

import { SubscribeCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic for which you wish to confirm a subscription. * @param {string} emailAddress - The email address that is subscribed to the topic. */ export const subscribeEmail = async ( topicArn = "TOPIC_ARN", emailAddress = "usern@me.com", ) => { const response = await snsClient.send( new SubscribeCommand({ Protocol: "email", TopicArn: topicArn, Endpoint: emailAddress, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'c8e35bcd-b3c0-5940-9f66-06f6fcc108f0', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // SubscriptionArn: 'pending confirmation' // } };

若要執行範例,請在命令提示字元中輸入下列命令。

node subscribe-email.js

您可以在這裡找到此範例程式碼 GitHub。

確認訂閱

在此範例中,透過驗證先前的訂閱動作傳送至端點的 Token,使用 Node.js 模組來驗證端點擁有者接收電子郵件的意圖。

創建一個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({});

您可以在這裡找到此範例程式碼 GitHub。

以檔名 confirm-subscription.js 建立一個 Node.js 模組。如先前所示設定 SDK,包括安裝所需的用戶端和套件。

定義參數,包括TOPIC_ARNTOKEN,並定義TRUEFALSE的值AuthenticateOnUnsubscribe

令牌是在上一個SUBSCRIBE操作期間發送給端點所有者的短期令牌。例如,對於電子郵件端點,TOKEN則位於傳送給電子郵件擁有者的「確認訂閱」電子郵件的 URL 中。例如,abc123是下列 URL 中的權杖。

若要呼叫方ConfirmSubscriptionCommand法,請建立叫用 Amazon SNS 用戶端服務物件的非同步函數,並傳遞參數物件。

注意

TOPIC_ARN 取代為主題的 Amazon 資源名稱 (ARN),將 TO KEN 取代為上一個Subscribe動作中傳送至端點擁有者之 URL 的權杖值,並定義 AuthenticateOnUnsubscribe. 值為或。TRUE FALSE

import { ConfirmSubscriptionCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} token - This token is sent the subscriber. Only subscribers * that are not AWS services (HTTP/S, email) need to be confirmed. * @param {string} topicArn - The ARN of the topic for which you wish to confirm a subscription. */ export const confirmSubscription = async ( token = "TOKEN", topicArn = "TOPIC_ARN", ) => { const response = await snsClient.send( // A subscription only needs to be confirmed if the endpoint type is // HTTP/S, email, or in another AWS account. new ConfirmSubscriptionCommand({ Token: token, TopicArn: topicArn, // If this is true, the subscriber cannot unsubscribe while unauthenticated. AuthenticateOnUnsubscribe: "false", }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '4bb5bce9-805a-5517-8333-e1d2cface90b', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // SubscriptionArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:TOPIC_NAME:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // } return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node confirm-subscription.js

您可以在這裡找到此範例程式碼 GitHub。

使用應用程式端點訂閱主題

在此範例中,使用 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({});

您可以在這裡找到此範例程式碼 GitHub。

以檔名 subscribe-app.js 建立一個 Node.js 模組。如先前所示設定 SDK,包括安裝所需的模組和套件。

建立一個物件,其中包含用TopicArn於指定application通訊協定的Protocol參數、要訂閱的主題,以及Endpoint參數行動應用程式端點的 Amazon 資源名稱 (ARN)。將參數傳遞至 SNS 用戶端類別的 SubscribeCommand 方法。

若要呼叫方SubscribeCommand法,請建立叫用 Amazon SNS 服務物件的非同步函數,並傳遞參數物件。

注意

將主題的主題取代為亞馬遜資源名稱 (ARN),將 MOBILE_ENDPOINT _ARN 取代為您要訂閱該主題的端點

import { SubscribeCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic the subscriber is subscribing to. * @param {string} endpoint - The Endpoint ARN of an application. This endpoint is created * when an application registers for notifications. */ export const subscribeApp = async ( topicArn = "TOPIC_ARN", endpoint = "ENDPOINT", ) => { const response = await snsClient.send( new SubscribeCommand({ Protocol: "application", TopicArn: topicArn, Endpoint: endpoint, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'c8e35bcd-b3c0-5940-9f66-06f6fcc108f0', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // SubscriptionArn: 'pending confirmation' // } return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node subscribe-app.js

您可以在這裡找到此範例程式碼 GitHub。

訂閱 Lambda 函數至主題

在此範例中,使用 Node.js 模組來訂閱AWS Lambda函數,以便接收來自 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({});

您可以在這裡找到此範例程式碼 GitHub。

以檔名 subscribe-lambda.js 建立一個 Node.js 模組。依前述內容設定軟體開發套件。

建立包含Protocol參數的物件、指定lambda通訊協定TopicArn、要訂閱的主題,以及AWS Lambda函數的 Amazon 資源名稱 (ARN) 做為Endpoint參數。將參數傳遞至 SNS 用戶端類別的 SubscribeCommand 方法。

若要呼叫方SubscribeCommand法,請建立叫用 Amazon SNS 用戶端服務物件的非同步函數,並傳遞參數物件。

注意

用主題的亞馬遜資源名稱(ARN)替換主題,將 Lambda 函數的亞馬遜資源名稱(ARN)替換為 Lambda 函數的亞馬遜資源名稱(ARN)。

import { SubscribeCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic the subscriber is subscribing to. * @param {string} endpoint - The Endpoint ARN of and AWS Lambda function. */ export const subscribeLambda = async ( topicArn = "TOPIC_ARN", endpoint = "ENDPOINT", ) => { const response = await snsClient.send( new SubscribeCommand({ Protocol: "lambda", TopicArn: topicArn, Endpoint: endpoint, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'c8e35bcd-b3c0-5940-9f66-06f6fcc108f0', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // SubscriptionArn: 'pending confirmation' // } return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node subscribe-lambda.js

您可以在這裡找到此範例程式碼 GitHub。

取消訂閱主題

在此範例中,使用 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({});

您可以在這裡找到此範例程式碼 GitHub。

以檔名 unsubscribe.js 建立一個 Node.js 模組。如先前所示設定 SDK,包括安裝所需的用戶端和套件。

建立包含SubscriptionArn參數的物件,並指定要取消訂閱的 Amazon 資源名稱 (ARN)。將參數傳遞至 SNS 用戶端類別的 UnsubscribeCommand 方法。

若要呼叫方UnsubscribeCommand法,請建立叫用 Amazon SNS 用戶端服務物件的非同步函數,並傳遞參數物件。

注意

主題訂閱的亞馬遜資源名稱 (ARN) 取代主題訂閱,以取消訂閱。

import { UnsubscribeCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} subscriptionArn - The ARN of the subscription to cancel. */ const unsubscribe = async ( subscriptionArn = "arn:aws:sns:us-east-1:xxxxxxxxxxxx:mytopic:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", ) => { const response = await snsClient.send( new UnsubscribeCommand({ SubscriptionArn: subscriptionArn, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '0178259a-9204-507c-b620-78a7570a44c6', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // } // } return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node unsubscribe.js

您可以在這裡找到此範例程式碼 GitHub。