使用 Amazon 软件开发工具包获取 Amazon SNS 主题的属性
以下代码示例展示如何获取 Amazon SNS 主题的属性。
- .NET
-
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; /// <summary> /// This example shows how to retrieve the attributes of an Amazon Simple /// Notification Service (Amazon SNS) topic. The example was written using /// the AWS SDK for .NET 3.7 and .NET Core 5.0. /// </summary> public class GetTopicAttributes { public static async Task Main() { string topicArn = "arn:aws:sns:us-west-2:000000000000:ExampleSNSTopic"; IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient(); var attributes = await GetTopicAttributesAsync(client, topicArn); DisplayTopicAttributes(attributes); } /// <summary> /// Given the ARN of the Amazon SNS topic, this method retrieves the topic /// attributes. /// </summary> /// <param name="client">The initialized Amazon SNS client object used /// to retrieve the attributes for the Amazon SNS topic.</param> /// <param name="topicArn">The ARN of the topic for which to retrieve /// the attributes.</param> /// <returns>A Dictionary of topic attributes.</returns> public static async Task<Dictionary<string, string>> GetTopicAttributesAsync( IAmazonSimpleNotificationService client, string topicArn) { var response = await client.GetTopicAttributesAsync(topicArn); return response.Attributes; } /// <summary> /// This method displays the attributes for an Amazon SNS topic. /// </summary> /// <param name="topicAttributes">A Dictionary containing the /// attributes for an Amazon SNS topic.</param> public static void DisplayTopicAttributes(Dictionary<string, string> topicAttributes) { foreach (KeyValuePair<string, string> entry in topicAttributes) { Console.WriteLine($"{entry.Key}: {entry.Value}\n"); } } }
-
有关 API 详细信息,请参阅 Amazon SDK for .NET API 参考中的 GetTopicAttributes。
-
- C++
-
- SDK for C++
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 Aws::SDKOptions options; Aws::InitAPI(options); { Aws::SNS::SNSClient sns; Aws::String topic_arn = argv[1]; Aws::SNS::Model::GetTopicAttributesRequest gta_req; gta_req.SetTopicArn(topic_arn); auto gta_out = sns.GetTopicAttributes(gta_req); if (gta_out.IsSuccess()) { std::cout << "Topic Attributes:" << std::endl; for (auto const &attribute : gta_out.GetResult().GetAttributes()) { std::cout << " * " << attribute.first << " : " << attribute.second << std::endl; } } else { std::cout << "Error while getting Topic attributes " << gta_out.GetError().GetMessage() << std::endl; } } Aws::ShutdownAPI(options);
-
有关 API 详细信息,请参阅 Amazon SDK for C++ API 参考中的 GetTopicAttributes。
-
- Java
-
- SDK for Java 2.x
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 public static void getSNSTopicAttributes(SnsClient snsClient, String topicArn ) { try { GetTopicAttributesRequest request = GetTopicAttributesRequest.builder() .topicArn(topicArn) .build(); GetTopicAttributesResponse result = snsClient.getTopicAttributes(request); System.out.println("\n\nStatus is " + result.sdkHttpResponse().statusCode() + "\n\nAttributes: \n\n" + result.attributes()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
-
有关 API 详细信息,请参阅 Amazon SDK for Java 2.x API 参考中的 GetTopicAttributes。
-
- JavaScript
-
- SDK for JavaScript V3
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 在单独的模块中创建客户端并将其导出。
import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };
导入软件开发工具包和客户端模块,然后调用 API。
// Import required AWS SDK clients and commands for Node.js import {GetTopicAttributesCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { TopicArn: "TOPIC_ARN" }; // TOPIC_ARN const run = async () => { try { const data = await snsClient.send(new GetTopicAttributesCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();
-
有关更多信息,请参阅 Amazon SDK for JavaScript 开发人员指南。
-
有关 API 详细信息,请参阅 Amazon SDK for JavaScript API 参考中的 GetTopicAttributes。
-
- SDK for JavaScript V2
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 导入软件开发工具包和客户端模块,然后调用 API。
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set region AWS.config.update({region: 'REGION'}); // Create promise and SNS service object var getTopicAttribsPromise = new AWS.SNS({apiVersion: '2010-03-31'}).getTopicAttributes({TopicArn: 'TOPIC_ARN'}).promise(); // Handle promise's fulfilled/rejected states getTopicAttribsPromise.then( function(data) { console.log(data); }).catch( function(err) { console.error(err, err.stack); });
-
有关更多信息,请参阅 Amazon SDK for JavaScript 开发人员指南。
-
有关 API 详细信息,请参阅 Amazon SDK for JavaScript API 参考中的 GetTopicAttributes。
-
- Kotlin
-
- SDK for Kotlin
-
注意 这是适用于预览版中功能的预发行文档。本文档随时可能更改。
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 suspend fun getSNSTopicAttributes(topicArnVal: String) { val request = GetTopicAttributesRequest { topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.getTopicAttributes(request) println("${result.attributes}") } }
-
有关 API 详细信息,请参阅 Amazon SDK for Kotlin API 参考中的 GetTopicAttributes
。
-
- PHP
-
- SDK for PHP
-
提示 要了解如何设置和运行此示例,请参阅 GitHub
。 $SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->getTopicAttributes([ 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
-
有关 API 详细信息,请参阅 Amazon SDK for PHP API 参考中的 GetTopicAttributes。
-
有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 Amazon SNS 与 Amazon 开发工具包结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。