AWS SDK ListSubscriptions またはコマンドラインツールと併用してください。 - Amazon Simple Notification Service

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK ListSubscriptions またはコマンドラインツールと併用してください。

次のコード例は使用方法を示しています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 for 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次の例では、アカウントの SNS サブスクリプションのリストが表示されます。 AWS

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" } ] }
  • API の詳細については、「AWS CLI コマンドリファレンスListSubscriptionsのを参照してください。

Java
SDK for Java 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
JavaScript (v3) 用の SDK
注記

にはまだまだあります。 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; };
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScript API ListSubscriptionsリファレンスのを参照してください

Kotlin
SDK for Kotlin
注記

にはまだまだあります 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 の詳細については、「AWS SDK for Kotlin API リファレンス」のを参照してくださいListSubscriptions

PHP
SDK for PHP
注記

にはまだまだあります。 GitHubAWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

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 for Python (Boto3)
注記

にはまだまだあります 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 の詳細については、『AWS SDK for Python (Boto3) API リファレンス』のを参照してくださいListSubscriptions

Ruby
SDK for Ruby
注記

にはまだまだあります。 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 for 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 の詳細については、AWS SDK for SAP ABAP API リファレンスのを参照してくださいListSubscriptions

AWS SDK 開発者ガイドとコード例の完全なリストについては、を参照してくださいAWS SDK での Amazon SNS の使用。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。