搭ListSubscriptions配 AWS SDK或使用 CLI - AWS SDK 程式碼範例

AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

ListSubscriptions配 AWS SDK或使用 CLI

下列程式碼範例會示範如何使用ListSubscriptions

.NET
AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; /// <summary> /// This example will retrieve a list of the existing Amazon Simple /// Notification Service (Amazon SNS) subscriptions. /// </summary> public class ListSubscriptions { public static async Task Main() { IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient(); Console.WriteLine("Enter a topic ARN to list subscriptions for a specific topic, " + "or press Enter to list subscriptions for all topics."); var topicArn = Console.ReadLine(); Console.WriteLine(); var subscriptions = await GetSubscriptionsListAsync(client, topicArn); DisplaySubscriptionList(subscriptions); } /// <summary> /// Gets a list of the existing Amazon SNS subscriptions, optionally by specifying a topic ARN. /// </summary> /// <param name="client">The initialized Amazon SNS client object used /// to obtain the list of subscriptions.</param> /// <param name="topicArn">The optional ARN of a specific topic. Defaults to null.</param> /// <returns>A list containing information about each subscription.</returns> public static async Task<List<Subscription>> GetSubscriptionsListAsync(IAmazonSimpleNotificationService client, string topicArn = null) { var results = new List<Subscription>(); if (!string.IsNullOrEmpty(topicArn)) { var paginateByTopic = client.Paginators.ListSubscriptionsByTopic( new ListSubscriptionsByTopicRequest() { TopicArn = topicArn, }); // Get the entire list using the paginator. await foreach (var subscription in paginateByTopic.Subscriptions) { results.Add(subscription); } } else { var paginateAllSubscriptions = client.Paginators.ListSubscriptions(new ListSubscriptionsRequest()); // Get the entire list using the paginator. await foreach (var subscription in paginateAllSubscriptions.Subscriptions) { results.Add(subscription); } } return results; } /// <summary> /// Display a list of Amazon SNS subscription information. /// </summary> /// <param name="subscriptionList">A list containing details for existing /// Amazon SNS subscriptions.</param> public static void DisplaySubscriptionList(List<Subscription> subscriptionList) { foreach (var subscription in subscriptionList) { Console.WriteLine($"Owner: {subscription.Owner}"); Console.WriteLine($"Subscription ARN: {subscription.SubscriptionArn}"); Console.WriteLine($"Topic ARN: {subscription.TopicArn}"); Console.WriteLine($"Endpoint: {subscription.Endpoint}"); Console.WriteLine($"Protocol: {subscription.Protocol}"); Console.WriteLine(); } } }
  • 如需詳API細資訊,請參閱AWS SDK for .NET API參考ListSubscriptions中的。

C++
SDK對於 C ++
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

//! Retrieve a list of Amazon Simple Notification Service (Amazon SNS) subscriptions. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::listSubscriptions( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::String nextToken; // Next token is used to handle a paginated response. bool result = true; Aws::Vector<Aws::SNS::Model::Subscription> subscriptions; do { Aws::SNS::Model::ListSubscriptionsRequest request; if (!nextToken.empty()) { request.SetNextToken(nextToken); } const Aws::SNS::Model::ListSubscriptionsOutcome outcome = snsClient.ListSubscriptions( request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::SNS::Model::Subscription> &newSubscriptions = outcome.GetResult().GetSubscriptions(); subscriptions.insert(subscriptions.cend(), newSubscriptions.begin(), newSubscriptions.end()); } else { std::cerr << "Error listing subscriptions " << outcome.GetError().GetMessage() << std::endl; result = false; break; } nextToken = outcome.GetResult().GetNextToken(); } while (!nextToken.empty()); if (result) { if (subscriptions.empty()) { std::cout << "No subscriptions found" << std::endl; } else { std::cout << "Subscriptions list:" << std::endl; for (auto const &subscription: subscriptions) { std::cout << " * " << subscription.GetSubscriptionArn() << std::endl; } } } return result; }
  • 如需詳API細資訊,請參閱AWS SDK for C++ API參考ListSubscriptions中的。

CLI
AWS CLI

列出您的SNS訂閱

下列list-subscriptions範例會顯示您 AWS 帳戶中的SNS訂閱清單。

aws sns list-subscriptions

輸出:

{ "Subscriptions": [ { "Owner": "123456789012", "Endpoint": "my-email@example.com", "Protocol": "email", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" } ] }
Java
SDK對於爪哇 2.x
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.ListSubscriptionsRequest; import software.amazon.awssdk.services.sns.model.ListSubscriptionsResponse; import software.amazon.awssdk.services.sns.model.SnsException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListSubscriptions { public static void main(String[] args) { SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); listSNSSubscriptions(snsClient); snsClient.close(); } public static void listSNSSubscriptions(SnsClient snsClient) { try { ListSubscriptionsRequest request = ListSubscriptionsRequest.builder() .build(); ListSubscriptionsResponse result = snsClient.listSubscriptions(request); System.out.println(result.subscriptions()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 如需詳API細資訊,請參閱AWS SDK for Java 2.x API參考ListSubscriptions中的。

JavaScript
SDK對於 JavaScript (3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 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({});

匯入SDK和用戶端模組並呼叫API.

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; };
Kotlin
SDK對於科特林
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun listSNSSubscriptions() { SnsClient { region = "us-east-1" }.use { snsClient -> val response = snsClient.listSubscriptions(ListSubscriptionsRequest {}) response.subscriptions?.forEach { sub -> println("Sub ARN is ${sub.subscriptionArn}") println("Sub protocol is ${sub.protocol}") } } }
  • 有API關詳細資訊,請參閱ListSubscriptionsAWS SDK的以取得 Kotlin API 的參考資料

PHP
適用於 PHP 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient; /** * Returns a list of Amazon SNS subscriptions in the requested region. * * This code expects that you have AWS credentials set up per: * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html */ $SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); try { $result = $SnSclient->listSubscriptions(); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
  • 如需詳API細資訊,請參閱AWS SDK for PHP API參考ListSubscriptions中的。

Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class SnsWrapper: """Encapsulates Amazon SNS topic and subscription functions.""" def __init__(self, sns_resource): """ :param sns_resource: A Boto3 Amazon SNS resource. """ self.sns_resource = sns_resource def list_subscriptions(self, topic=None): """ Lists subscriptions for the current account, optionally limited to a specific topic. :param topic: When specified, only subscriptions to this topic are returned. :return: An iterator that yields the subscriptions. """ try: if topic is None: subs_iter = self.sns_resource.subscriptions.all() else: subs_iter = topic.subscriptions.all() logger.info("Got subscriptions.") except ClientError: logger.exception("Couldn't get subscriptions.") raise else: return subs_iter
  • 如需API詳細資訊,請參閱ListSubscriptions中AWS SDK的〈〉中的〈〉API

Ruby
SDK對於紅寶石
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

# This class demonstrates how to list subscriptions to an Amazon Simple Notification Service (SNS) topic class SnsSubscriptionLister def initialize(sns_client) @sns_client = sns_client @logger = Logger.new($stdout) end # Lists subscriptions for a given SNS topic # @param topic_arn [String] The ARN of the SNS topic # @return [Types::ListSubscriptionsResponse] subscriptions: The response object def list_subscriptions(topic_arn) @logger.info("Listing subscriptions for topic: #{topic_arn}") subscriptions = @sns_client.list_subscriptions_by_topic(topic_arn: topic_arn) subscriptions.subscriptions.each do |subscription| @logger.info("Subscription endpoint: #{subscription.endpoint}") end subscriptions rescue Aws::SNS::Errors::ServiceError => e @logger.error("Error listing subscriptions: #{e.message}") raise end end # Example usage: if $PROGRAM_NAME == __FILE__ sns_client = Aws::SNS::Client.new topic_arn = "SNS_TOPIC_ARN" # Replace with your SNS topic ARN lister = SnsSubscriptionLister.new(sns_client) begin lister.list_subscriptions(topic_arn) rescue StandardError => e puts "Failed to list subscriptions: #{e.message}" exit 1 end end
SAP ABAP
SDK為 SAP ABAP
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

TRY. oo_result = lo_sns->listsubscriptions( ). " oo_result is returned for testing purposes. " DATA(lt_subscriptions) = oo_result->get_subscriptions( ). MESSAGE 'Retrieved list of subscribers.' TYPE 'I'. CATCH /aws1/cx_rt_generic. MESSAGE 'Unable to list subscribers.' TYPE 'E'. ENDTRY.
  • 如需詳API細資訊,請參閱ListSubscriptionsAWS SDK的以供SAPABAPAPI參考