Selecione suas preferências de cookies

Usamos cookies essenciais e ferramentas semelhantes que são necessárias para fornecer nosso site e serviços. Usamos cookies de desempenho para coletar estatísticas anônimas, para que possamos entender como os clientes usam nosso site e fazer as devidas melhorias. Cookies essenciais não podem ser desativados, mas você pode clicar em “Personalizar” ou “Recusar” para recusar cookies de desempenho.

Se você concordar, a AWS e terceiros aprovados também usarão cookies para fornecer recursos úteis do site, lembrar suas preferências e exibir conteúdo relevante, incluindo publicidade relevante. Para aceitar ou recusar todos os cookies não essenciais, clique em “Aceitar” ou “Recusar”. Para fazer escolhas mais detalhadas, clique em “Personalizar”.

Exemplos de código para o Amazon SNS usando AWS SDKs

Modo de foco
Exemplos de código para o Amazon SNS usando AWS SDKs - Amazon Simple Notification Service

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Os exemplos de código a seguir mostram como usar o Amazon SNS com um kit de desenvolvimento AWS de software (SDK).

Ações são trechos de código de programas maiores e devem ser executadas em contexto. Embora as ações mostrem como chamar perfis de serviço individuais, você pode ver as ações no contexto em seus cenários relacionados.

Cenários são exemplos de código que mostram como realizar tarefas específicas chamando várias funções dentro de um serviço ou combinadas com outros Serviços da AWS.

Para obter uma lista completa dos guias do desenvolvedor do AWS SDK e exemplos de código, consulteUsando o Amazon SNS com um SDK AWS. Este tópico também inclui informações sobre como começar e detalhes sobre versões anteriores do SDK.

Conceitos básicos

Os exemplos de código a seguir mostram como começar a usar o Amazon SNS.

.NET
AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; namespace SNSActions; public static class HelloSNS { static async Task Main(string[] args) { var snsClient = new AmazonSimpleNotificationServiceClient(); Console.WriteLine($"Hello Amazon SNS! Following are some of your topics:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get a list of topics. var response = await snsClient.ListTopicsAsync( new ListTopicsRequest()); foreach (var topic in response.Topics) { Console.WriteLine($"\tTopic ARN: {topic.TopicArn}"); Console.WriteLine(); } } }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for .NET da API.

C++
SDK para C++
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Código para o CMake arquivo CMake Lists.txt.

# Set the minimum required version of CMake for this project. cmake_minimum_required(VERSION 3.13) # Set the AWS service components used by this project. set(SERVICE_COMPONENTS sns) # Set this project's name. project("hello_sns") # Set the C++ standard to use to build this target. # At least C++ 11 is required for the AWS SDK for C++. set(CMAKE_CXX_STANDARD 11) # Use the MSVC variable to determine if this is a Windows build. set(WINDOWS_BUILD ${MSVC}) if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK. string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all") list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH}) endif () # Find the AWS SDK for C++ package. find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS}) if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS) # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging. # set(BIN_SUB_DIR "/Debug") # If you are building from the command line you may need to uncomment this # and set the proper subdirectory to the executables' location. AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR}) endif () add_executable(${PROJECT_NAME} hello_sns.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

Código para o arquivo de origem hello_sns.cpp.

#include <aws/core/Aws.h> #include <aws/sns/SNSClient.h> #include <aws/sns/model/ListTopicsRequest.h> #include <iostream> /* * A "Hello SNS" starter application which initializes an Amazon Simple Notification * Service (Amazon SNS) client and lists the SNS topics in the current account. * * main function * * Usage: 'hello_sns' * */ int main(int argc, char **argv) { Aws::SDKOptions options; // Optionally change the log level for debugging. // options.loggingOptions.logLevel = Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); // Should only be called once. { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::SNS::SNSClient snsClient(clientConfig); Aws::Vector<Aws::SNS::Model::Topic> allTopics; Aws::String nextToken; // Next token is used to handle a paginated response. do { Aws::SNS::Model::ListTopicsRequest request; if (!nextToken.empty()) { request.SetNextToken(nextToken); } const Aws::SNS::Model::ListTopicsOutcome outcome = snsClient.ListTopics( request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::SNS::Model::Topic> &paginatedTopics = outcome.GetResult().GetTopics(); if (!paginatedTopics.empty()) { allTopics.insert(allTopics.cend(), paginatedTopics.cbegin(), paginatedTopics.cend()); } } else { std::cerr << "Error listing topics " << outcome.GetError().GetMessage() << std::endl; return 1; } nextToken = outcome.GetResult().GetNextToken(); } while (!nextToken.empty()); std::cout << "Hello Amazon SNS! You have " << allTopics.size() << " topic" << (allTopics.size() == 1 ? "" : "s") << " in your account." << std::endl; if (!allTopics.empty()) { std::cout << "Here are your topic ARNs." << std::endl; for (const Aws::SNS::Model::Topic &topic: allTopics) { std::cout << " * " << topic.GetTopicArn() << std::endl; } } } Aws::ShutdownAPI(options); // Should only be called once. return 0; }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for C++ da API.

Go
SDK para Go V2
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

package main import ( "context" "fmt" "log" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/sns" "github.com/aws/aws-sdk-go-v2/service/sns/types" ) // main uses the AWS SDK for Go V2 to create an Amazon Simple Notification Service // (Amazon SNS) client and list the topics in your account. // This example uses the default settings specified in your shared credentials // and config files. func main() { ctx := context.Background() sdkConfig, err := config.LoadDefaultConfig(ctx) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } snsClient := sns.NewFromConfig(sdkConfig) fmt.Println("Let's list the topics for your account.") var topics []types.Topic paginator := sns.NewListTopicsPaginator(snsClient, &sns.ListTopicsInput{}) for paginator.HasMorePages() { output, err := paginator.NextPage(ctx) if err != nil { log.Printf("Couldn't get topics. Here's why: %v\n", err) break } else { topics = append(topics, output.Topics...) } } if len(topics) == 0 { fmt.Println("You don't have any topics!") } else { for _, topic := range topics { fmt.Printf("\t%v\n", *topic.TopicArn) } } }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK para Go da API.

Java
SDK para Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

package com.example.sns; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SnsException; import software.amazon.awssdk.services.sns.paginators.ListTopicsIterable; public class HelloSNS { public static void main(String[] args) { SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); listSNSTopics(snsClient); snsClient.close(); } public static void listSNSTopics(SnsClient snsClient) { try { ListTopicsIterable listTopics = snsClient.listTopicsPaginator(); listTopics.stream() .flatMap(r -> r.topics().stream()) .forEach(content -> System.out.println(" Topic ARN: " + content.topicArn())); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for Java 2.x da API.

JavaScript
SDK para JavaScript (v3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Inicialize um cliente SNS e liste tópicos em sua conta.

import { SNSClient, paginateListTopics } from "@aws-sdk/client-sns"; export const helloSns = async () => { // The configuration object (`{}`) is required. If the region and credentials // are omitted, the SDK uses your local configuration if it exists. const client = new SNSClient({}); // You can also use `ListTopicsCommand`, but to use that command you must // handle the pagination yourself. You can do that by sending the `ListTopicsCommand` // with the `NextToken` parameter from the previous request. const paginatedTopics = paginateListTopics({ client }, {}); const topics = []; for await (const page of paginatedTopics) { if (page.Topics?.length) { topics.push(...page.Topics); } } const suffix = topics.length === 1 ? "" : "s"; console.log( `Hello, Amazon SNS! You have ${topics.length} topic${suffix} in your account.`, ); console.log(topics.map((t) => ` * ${t.TopicArn}`).join("\n")); };
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for JavaScript da API.

Kotlin
SDK para Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

import aws.sdk.kotlin.services.sns.SnsClient import aws.sdk.kotlin.services.sns.model.ListTopicsRequest import aws.sdk.kotlin.services.sns.paginators.listTopicsPaginated import kotlinx.coroutines.flow.transform /** Before running this Kotlin 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-kotlin/latest/developer-guide/setup.html */ suspend fun main() { listTopicsPag() } suspend fun listTopicsPag() { SnsClient { region = "us-east-1" }.use { snsClient -> snsClient .listTopicsPaginated(ListTopicsRequest { }) .transform { it.topics?.forEach { topic -> emit(topic) } } .collect { topic -> println("The topic ARN is ${topic.topicArn}") } } }
  • Para obter detalhes da API, consulte a ListTopicsreferência da API AWS SDK for Kotlin.

Swift
SDK para Swift
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

O arquivo Package.swift.

import PackageDescription let package = Package( name: "sns-basics", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13), .iOS(.v15) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/awslabs/aws-sdk-swift", from: "1.0.0"), .package( url: "https://github.com/apple/swift-argument-parser.git", branch: "main" ) ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "sns-basics", dependencies: [ .product(name: "AWSSNS", package: "aws-sdk-swift"), .product(name: "ArgumentParser", package: "swift-argument-parser") ], path: "Sources") ] )

O programa principal do Swift.

import ArgumentParser import AWSClientRuntime import AWSSNS import Foundation struct ExampleCommand: ParsableCommand { @Option(help: "Name of the Amazon Region to use (default: us-east-1)") var region = "us-east-1" static var configuration = CommandConfiguration( commandName: "sns-basics", abstract: """ This example shows how to list all of your available Amazon SNS topics. """, discussion: """ """ ) /// Called by ``main()`` to run the bulk of the example. func runAsync() async throws { let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) var topics: [String] = [] let outputPages = snsClient.listTopicsPaginated( input: ListTopicsInput() ) // Each time a page of results arrives, process its contents. for try await output in outputPages { guard let topicList = output.topics else { print("Unable to get a page of Amazon SNS topics.") return } // Iterate over the topics listed on this page, adding their ARNs // to the `topics` array. for topic in topicList { guard let arn = topic.topicArn else { print("Topic has no ARN.") return } topics.append(arn) } } print("You have \(topics.count) topics:") for topic in topics { print(" \(topic)") } } } /// The program's asynchronous entry point. @main struct Main { static func main() async { let args = Array(CommandLine.arguments.dropFirst()) do { let command = try ExampleCommand.parse(args) try await command.runAsync() } catch { ExampleCommand.exit(withError: error) } } }
  • Para obter detalhes da API, consulte ListTopicsa referência da API AWS SDK for Swift.

Os exemplos de código a seguir mostram como começar a usar o Amazon SNS.

.NET
AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; namespace SNSActions; public static class HelloSNS { static async Task Main(string[] args) { var snsClient = new AmazonSimpleNotificationServiceClient(); Console.WriteLine($"Hello Amazon SNS! Following are some of your topics:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get a list of topics. var response = await snsClient.ListTopicsAsync( new ListTopicsRequest()); foreach (var topic in response.Topics) { Console.WriteLine($"\tTopic ARN: {topic.TopicArn}"); Console.WriteLine(); } } }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for .NET da API.

C++
SDK para C++
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Código para o CMake arquivo CMake Lists.txt.

# Set the minimum required version of CMake for this project. cmake_minimum_required(VERSION 3.13) # Set the AWS service components used by this project. set(SERVICE_COMPONENTS sns) # Set this project's name. project("hello_sns") # Set the C++ standard to use to build this target. # At least C++ 11 is required for the AWS SDK for C++. set(CMAKE_CXX_STANDARD 11) # Use the MSVC variable to determine if this is a Windows build. set(WINDOWS_BUILD ${MSVC}) if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK. string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all") list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH}) endif () # Find the AWS SDK for C++ package. find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS}) if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS) # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging. # set(BIN_SUB_DIR "/Debug") # If you are building from the command line you may need to uncomment this # and set the proper subdirectory to the executables' location. AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR}) endif () add_executable(${PROJECT_NAME} hello_sns.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

Código para o arquivo de origem hello_sns.cpp.

#include <aws/core/Aws.h> #include <aws/sns/SNSClient.h> #include <aws/sns/model/ListTopicsRequest.h> #include <iostream> /* * A "Hello SNS" starter application which initializes an Amazon Simple Notification * Service (Amazon SNS) client and lists the SNS topics in the current account. * * main function * * Usage: 'hello_sns' * */ int main(int argc, char **argv) { Aws::SDKOptions options; // Optionally change the log level for debugging. // options.loggingOptions.logLevel = Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); // Should only be called once. { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::SNS::SNSClient snsClient(clientConfig); Aws::Vector<Aws::SNS::Model::Topic> allTopics; Aws::String nextToken; // Next token is used to handle a paginated response. do { Aws::SNS::Model::ListTopicsRequest request; if (!nextToken.empty()) { request.SetNextToken(nextToken); } const Aws::SNS::Model::ListTopicsOutcome outcome = snsClient.ListTopics( request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::SNS::Model::Topic> &paginatedTopics = outcome.GetResult().GetTopics(); if (!paginatedTopics.empty()) { allTopics.insert(allTopics.cend(), paginatedTopics.cbegin(), paginatedTopics.cend()); } } else { std::cerr << "Error listing topics " << outcome.GetError().GetMessage() << std::endl; return 1; } nextToken = outcome.GetResult().GetNextToken(); } while (!nextToken.empty()); std::cout << "Hello Amazon SNS! You have " << allTopics.size() << " topic" << (allTopics.size() == 1 ? "" : "s") << " in your account." << std::endl; if (!allTopics.empty()) { std::cout << "Here are your topic ARNs." << std::endl; for (const Aws::SNS::Model::Topic &topic: allTopics) { std::cout << " * " << topic.GetTopicArn() << std::endl; } } } Aws::ShutdownAPI(options); // Should only be called once. return 0; }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for C++ da API.

Go
SDK para Go V2
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

package main import ( "context" "fmt" "log" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/sns" "github.com/aws/aws-sdk-go-v2/service/sns/types" ) // main uses the AWS SDK for Go V2 to create an Amazon Simple Notification Service // (Amazon SNS) client and list the topics in your account. // This example uses the default settings specified in your shared credentials // and config files. func main() { ctx := context.Background() sdkConfig, err := config.LoadDefaultConfig(ctx) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } snsClient := sns.NewFromConfig(sdkConfig) fmt.Println("Let's list the topics for your account.") var topics []types.Topic paginator := sns.NewListTopicsPaginator(snsClient, &sns.ListTopicsInput{}) for paginator.HasMorePages() { output, err := paginator.NextPage(ctx) if err != nil { log.Printf("Couldn't get topics. Here's why: %v\n", err) break } else { topics = append(topics, output.Topics...) } } if len(topics) == 0 { fmt.Println("You don't have any topics!") } else { for _, topic := range topics { fmt.Printf("\t%v\n", *topic.TopicArn) } } }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK para Go da API.

Java
SDK para Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

package com.example.sns; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SnsException; import software.amazon.awssdk.services.sns.paginators.ListTopicsIterable; public class HelloSNS { public static void main(String[] args) { SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); listSNSTopics(snsClient); snsClient.close(); } public static void listSNSTopics(SnsClient snsClient) { try { ListTopicsIterable listTopics = snsClient.listTopicsPaginator(); listTopics.stream() .flatMap(r -> r.topics().stream()) .forEach(content -> System.out.println(" Topic ARN: " + content.topicArn())); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for Java 2.x da API.

JavaScript
SDK para JavaScript (v3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Inicialize um cliente SNS e liste tópicos em sua conta.

import { SNSClient, paginateListTopics } from "@aws-sdk/client-sns"; export const helloSns = async () => { // The configuration object (`{}`) is required. If the region and credentials // are omitted, the SDK uses your local configuration if it exists. const client = new SNSClient({}); // You can also use `ListTopicsCommand`, but to use that command you must // handle the pagination yourself. You can do that by sending the `ListTopicsCommand` // with the `NextToken` parameter from the previous request. const paginatedTopics = paginateListTopics({ client }, {}); const topics = []; for await (const page of paginatedTopics) { if (page.Topics?.length) { topics.push(...page.Topics); } } const suffix = topics.length === 1 ? "" : "s"; console.log( `Hello, Amazon SNS! You have ${topics.length} topic${suffix} in your account.`, ); console.log(topics.map((t) => ` * ${t.TopicArn}`).join("\n")); };
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for JavaScript da API.

Kotlin
SDK para Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

import aws.sdk.kotlin.services.sns.SnsClient import aws.sdk.kotlin.services.sns.model.ListTopicsRequest import aws.sdk.kotlin.services.sns.paginators.listTopicsPaginated import kotlinx.coroutines.flow.transform /** Before running this Kotlin 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-kotlin/latest/developer-guide/setup.html */ suspend fun main() { listTopicsPag() } suspend fun listTopicsPag() { SnsClient { region = "us-east-1" }.use { snsClient -> snsClient .listTopicsPaginated(ListTopicsRequest { }) .transform { it.topics?.forEach { topic -> emit(topic) } } .collect { topic -> println("The topic ARN is ${topic.topicArn}") } } }
  • Para obter detalhes da API, consulte a ListTopicsreferência da API AWS SDK for Kotlin.

Swift
SDK para Swift
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

O arquivo Package.swift.

import PackageDescription let package = Package( name: "sns-basics", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13), .iOS(.v15) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/awslabs/aws-sdk-swift", from: "1.0.0"), .package( url: "https://github.com/apple/swift-argument-parser.git", branch: "main" ) ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "sns-basics", dependencies: [ .product(name: "AWSSNS", package: "aws-sdk-swift"), .product(name: "ArgumentParser", package: "swift-argument-parser") ], path: "Sources") ] )

O programa principal do Swift.

import ArgumentParser import AWSClientRuntime import AWSSNS import Foundation struct ExampleCommand: ParsableCommand { @Option(help: "Name of the Amazon Region to use (default: us-east-1)") var region = "us-east-1" static var configuration = CommandConfiguration( commandName: "sns-basics", abstract: """ This example shows how to list all of your available Amazon SNS topics. """, discussion: """ """ ) /// Called by ``main()`` to run the bulk of the example. func runAsync() async throws { let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) var topics: [String] = [] let outputPages = snsClient.listTopicsPaginated( input: ListTopicsInput() ) // Each time a page of results arrives, process its contents. for try await output in outputPages { guard let topicList = output.topics else { print("Unable to get a page of Amazon SNS topics.") return } // Iterate over the topics listed on this page, adding their ARNs // to the `topics` array. for topic in topicList { guard let arn = topic.topicArn else { print("Topic has no ARN.") return } topics.append(arn) } } print("You have \(topics.count) topics:") for topic in topics { print(" \(topic)") } } } /// The program's asynchronous entry point. @main struct Main { static func main() async { let args = Array(CommandLine.arguments.dropFirst()) do { let command = try ExampleCommand.parse(args) try await command.runAsync() } catch { ExampleCommand.exit(withError: error) } } }
  • Para obter detalhes da API, consulte ListTopicsa referência da API AWS SDK for Swift.

AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; namespace SNSActions; public static class HelloSNS { static async Task Main(string[] args) { var snsClient = new AmazonSimpleNotificationServiceClient(); Console.WriteLine($"Hello Amazon SNS! Following are some of your topics:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get a list of topics. var response = await snsClient.ListTopicsAsync( new ListTopicsRequest()); foreach (var topic in response.Topics) { Console.WriteLine($"\tTopic ARN: {topic.TopicArn}"); Console.WriteLine(); } } }
  • Para obter detalhes da API, consulte ListTopicsa Referência AWS SDK for .NET da API.

PrivacidadeTermos do sitePreferências de cookies
© 2025, Amazon Web Services, Inc. ou suas afiliadas. Todos os direitos reservados.