使用 C++ SDK 的亚马逊SNS示例 - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 C++ SDK 的亚马逊SNS示例

以下代码示例向您展示了如何通过 AWS SDK for C++ 与 Amazon 一起使用来执行操作和实现常见场景SNS。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

场景是向您展示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

开始使用

以下代码示例展示了如何开始使用 Amazon SNS。

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

CMakeLists.txt CMake 文件的代码。

# 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})

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; }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 ListTopics” 中的。

操作

以下代码示例显示了如何使用CreateTopic

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Create an Amazon Simple Notification Service (Amazon SNS) topic. /*! \param topicName: An Amazon SNS topic name. \param topicARNResult: String to return the Amazon Resource Name (ARN) for the topic. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::createTopic(const Aws::String &topicName, Aws::String &topicARNResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::CreateTopicRequest request; request.SetName(topicName); const Aws::SNS::Model::CreateTopicOutcome outcome = snsClient.CreateTopic(request); if (outcome.IsSuccess()) { topicARNResult = outcome.GetResult().GetTopicArn(); std::cout << "Successfully created an Amazon SNS topic " << topicName << " with topic ARN '" << topicARNResult << "'." << std::endl; } else { std::cerr << "Error creating topic " << topicName << ":" << outcome.GetError().GetMessage() << std::endl; topicARNResult.clear(); } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 CreateTopic” 中的。

以下代码示例显示了如何使用DeleteTopic

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Delete an Amazon Simple Notification Service (Amazon SNS) topic. /*! \param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::deleteTopic(const Aws::String &topicARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::DeleteTopicRequest request; request.SetTopicArn(topicARN); const Aws::SNS::Model::DeleteTopicOutcome outcome = snsClient.DeleteTopic(request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted the Amazon SNS topic " << topicARN << std::endl; } else { std::cerr << "Error deleting topic " << topicARN << ":" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 DeleteTopic” 中的。

以下代码示例显示了如何使用GetSMSAttributes

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Retrieve the default settings for sending SMS messages from your AWS account by using //! Amazon Simple Notification Service (Amazon SNS). /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::getSMSType(const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::GetSMSAttributesRequest request; //Set the request to only retrieve the DefaultSMSType setting. //Without the following line, GetSMSAttributes would retrieve all settings. request.AddAttributes("DefaultSMSType"); const Aws::SNS::Model::GetSMSAttributesOutcome outcome = snsClient.GetSMSAttributes( request); if (outcome.IsSuccess()) { const Aws::Map<Aws::String, Aws::String> attributes = outcome.GetResult().GetAttributes(); if (!attributes.empty()) { for (auto const &att: attributes) { std::cout << att.first << ": " << att.second << std::endl; } } else { std::cout << "AwsDoc::SNS::getSMSType - an empty map of attributes was retrieved." << std::endl; } } else { std::cerr << "Error while getting SMS Type: '" << outcome.GetError().GetMessage() << "'" << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅etSMSAttributes《AWS SDK for C++ API参考资料》中的 G

以下代码示例显示了如何使用GetTopicAttributes

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Retrieve the properties of an Amazon Simple Notification Service (Amazon SNS) topic. /*! \param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::getTopicAttributes(const Aws::String &topicARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::GetTopicAttributesRequest request; request.SetTopicArn(topicARN); const Aws::SNS::Model::GetTopicAttributesOutcome outcome = snsClient.GetTopicAttributes( request); if (outcome.IsSuccess()) { std::cout << "Topic Attributes:" << std::endl; for (auto const &attribute: outcome.GetResult().GetAttributes()) { std::cout << " * " << attribute.first << " : " << attribute.second << std::endl; } } else { std::cerr << "Error while getting Topic attributes " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 GetTopicAttributes” 中的。

以下代码示例显示了如何使用ListSubscriptions

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” 中的。

以下代码示例显示了如何使用ListTopics

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Retrieve a list of Amazon Simple Notification Service (Amazon SNS) topics. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::listTopics(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; do { Aws::SNS::Model::ListTopicsRequest request; if (!nextToken.empty()) { request.SetNextToken(nextToken); } const Aws::SNS::Model::ListTopicsOutcome outcome = snsClient.ListTopics( request); if (outcome.IsSuccess()) { std::cout << "Topics list:" << std::endl; for (auto const &topic: outcome.GetResult().GetTopics()) { std::cout << " * " << topic.GetTopicArn() << std::endl; } } else { std::cerr << "Error listing topics " << outcome.GetError().GetMessage() << std::endl; result = false; break; } nextToken = outcome.GetResult().GetNextToken(); } while (!nextToken.empty()); return result; }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 ListTopics” 中的。

以下代码示例显示了如何使用Publish

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Send a message to an Amazon Simple Notification Service (Amazon SNS) topic. /*! \param message: The message to publish. \param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::publishToTopic(const Aws::String &message, const Aws::String &topicARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::PublishRequest request; request.SetMessage(message); request.SetTopicArn(topicARN); const Aws::SNS::Model::PublishOutcome outcome = snsClient.Publish(request); if (outcome.IsSuccess()) { std::cout << "Message published successfully with id '" << outcome.GetResult().GetMessageId() << "'." << std::endl; } else { std::cerr << "Error while publishing message " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

发布带有属性的消息。

static const Aws::String TONE_ATTRIBUTE("tone"); static const Aws::Vector<Aws::String> TONES = {"cheerful", "funny", "serious", "sincere"}; Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::PublishRequest request; request.SetTopicArn(topicARN); Aws::String message = askQuestion("Enter a message text to publish. "); request.SetMessage(message); if (filteringMessages && askYesNoQuestion( "Add an attribute to this message? (y/n) ")) { for (size_t i = 0; i < TONES.size(); ++i) { std::cout << " " << (i + 1) << ". " << TONES[i] << std::endl; } int selection = askQuestionForIntRange( "Enter a number for an attribute. ", 1, static_cast<int>(TONES.size())); Aws::SNS::Model::MessageAttributeValue messageAttributeValue; messageAttributeValue.SetDataType("String"); messageAttributeValue.SetStringValue(TONES[selection - 1]); request.AddMessageAttributes(TONE_ATTRIBUTE, messageAttributeValue); } Aws::SNS::Model::PublishOutcome outcome = snsClient.Publish(request); if (outcome.IsSuccess()) { std::cout << "Your message was successfully published." << std::endl; } else { std::cerr << "Error with TopicsAndQueues::Publish. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; }
  • 有关API详细信息,请参阅在AWS SDK for C++ API参考发布

以下代码示例显示了如何使用SetSMSAttributes

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

如何使用 Amazon SNS 来设置 D efaultSMSType 属性。

//! Set the default settings for sending SMS messages. /*! \param smsType: The type of SMS message that you will send by default. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::setSMSType(const Aws::String &smsType, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::SetSMSAttributesRequest request; request.AddAttributes("DefaultSMSType", smsType); const Aws::SNS::Model::SetSMSAttributesOutcome outcome = snsClient.SetSMSAttributes( request); if (outcome.IsSuccess()) { std::cout << "SMS Type set successfully " << std::endl; } else { std::cerr << "Error while setting SMS Type: '" << outcome.GetError().GetMessage() << "'" << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅etSMSAttributes《AWS SDK for C++ API参考资料》中的 S

以下代码示例显示了如何使用Subscribe

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

通过电子邮件地址订阅主题。

//! Subscribe to an Amazon Simple Notification Service (Amazon SNS) topic with delivery to an email address. /*! \param topicARN: An SNS topic Amazon Resource Name (ARN). \param emailAddress: An email address. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::subscribeEmail(const Aws::String &topicARN, const Aws::String &emailAddress, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::SubscribeRequest request; request.SetTopicArn(topicARN); request.SetProtocol("email"); request.SetEndpoint(emailAddress); const Aws::SNS::Model::SubscribeOutcome outcome = snsClient.Subscribe(request); if (outcome.IsSuccess()) { std::cout << "Subscribed successfully." << std::endl; std::cout << "Subscription ARN '" << outcome.GetResult().GetSubscriptionArn() << "'." << std::endl; } else { std::cerr << "Error while subscribing " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

为移动应用程序订阅主题。

//! Subscribe to an Amazon Simple Notification Service (Amazon SNS) topic with delivery to a mobile app. /*! \param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic. \param endpointARN: The ARN for a mobile app or device endpoint. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::subscribeApp(const Aws::String &topicARN, const Aws::String &endpointARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::SubscribeRequest request; request.SetTopicArn(topicARN); request.SetProtocol("application"); request.SetEndpoint(endpointARN); const Aws::SNS::Model::SubscribeOutcome outcome = snsClient.Subscribe(request); if (outcome.IsSuccess()) { std::cout << "Subscribed successfully." << std::endl; std::cout << "Subscription ARN '" << outcome.GetResult().GetSubscriptionArn() << "'." << std::endl; } else { std::cerr << "Error while subscribing " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

为主题订阅 Lambda 函数。

//! Subscribe to an Amazon Simple Notification Service (Amazon SNS) topic with delivery to an AWS Lambda function. /*! \param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic. \param lambdaFunctionARN: The ARN for an AWS Lambda function. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::subscribeLambda(const Aws::String &topicARN, const Aws::String &lambdaFunctionARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::SubscribeRequest request; request.SetTopicArn(topicARN); request.SetProtocol("lambda"); request.SetEndpoint(lambdaFunctionARN); const Aws::SNS::Model::SubscribeOutcome outcome = snsClient.Subscribe(request); if (outcome.IsSuccess()) { std::cout << "Subscribed successfully." << std::endl; std::cout << "Subscription ARN '" << outcome.GetResult().GetSubscriptionArn() << "'." << std::endl; } else { std::cerr << "Error while subscribing " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

为SQS队列订阅主题。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::SubscribeRequest request; request.SetTopicArn(topicARN); request.SetProtocol("sqs"); request.SetEndpoint(queueARN); Aws::SNS::Model::SubscribeOutcome outcome = snsClient.Subscribe(request); if (outcome.IsSuccess()) { Aws::String subscriptionARN = outcome.GetResult().GetSubscriptionArn(); std::cout << "The queue '" << queueName << "' has been subscribed to the topic '" << "'" << topicName << "'" << std::endl; std::cout << "with the subscription ARN '" << subscriptionARN << "." << std::endl; subscriptionARNS.push_back(subscriptionARN); } else { std::cerr << "Error with TopicsAndQueues::Subscribe. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; }

使用过滤器订阅主题。

static const Aws::String TONE_ATTRIBUTE("tone"); static const Aws::Vector<Aws::String> TONES = {"cheerful", "funny", "serious", "sincere"}; Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::SubscribeRequest request; request.SetTopicArn(topicARN); request.SetProtocol("sqs"); request.SetEndpoint(queueARN); if (isFifoTopic) { if (first) { std::cout << "Subscriptions to a FIFO topic can have filters." << std::endl; std::cout << "If you add a filter to this subscription, then only the filtered messages " << "will be received in the queue." << std::endl; std::cout << "For information about message filtering, " << "see https://docs.aws.amazon.com/sns/latest/dg/sns-message-filtering.html" << std::endl; std::cout << "For this example, you can filter messages by a \"" << TONE_ATTRIBUTE << "\" attribute." << std::endl; } std::ostringstream ostringstream; ostringstream << "Filter messages for \"" << queueName << "\"'s subscription to the topic \"" << topicName << "\"? (y/n)"; // Add filter if user answers yes. if (askYesNoQuestion(ostringstream.str())) { Aws::String jsonPolicy = getFilterPolicyFromUser(); if (!jsonPolicy.empty()) { filteringMessages = true; std::cout << "This is the filter policy for this subscription." << std::endl; std::cout << jsonPolicy << std::endl; request.AddAttributes("FilterPolicy", jsonPolicy); } else { std::cout << "Because you did not select any attributes, no filter " << "will be added to this subscription." << std::endl; } } } // if (isFifoTopic) Aws::SNS::Model::SubscribeOutcome outcome = snsClient.Subscribe(request); if (outcome.IsSuccess()) { Aws::String subscriptionARN = outcome.GetResult().GetSubscriptionArn(); std::cout << "The queue '" << queueName << "' has been subscribed to the topic '" << "'" << topicName << "'" << std::endl; std::cout << "with the subscription ARN '" << subscriptionARN << "." << std::endl; subscriptionARNS.push_back(subscriptionARN); } else { std::cerr << "Error with TopicsAndQueues::Subscribe. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } //! Routine that lets the user select attributes for a subscription filter policy. /*! \sa getFilterPolicyFromUser() \return Aws::String: The filter policy as JSON. */ Aws::String AwsDoc::TopicsAndQueues::getFilterPolicyFromUser() { std::cout << "You can filter messages by one or more of the following \"" << TONE_ATTRIBUTE << "\" attributes." << std::endl; std::vector<Aws::String> filterSelections; int selection; do { for (size_t j = 0; j < TONES.size(); ++j) { std::cout << " " << (j + 1) << ". " << TONES[j] << std::endl; } selection = askQuestionForIntRange( "Enter a number (or enter zero to stop adding more). ", 0, static_cast<int>(TONES.size())); if (selection != 0) { const Aws::String &selectedTone(TONES[selection - 1]); // Add the tone to the selection if it is not already added. if (std::find(filterSelections.begin(), filterSelections.end(), selectedTone) == filterSelections.end()) { filterSelections.push_back(selectedTone); } } } while (selection != 0); Aws::String result; if (!filterSelections.empty()) { std::ostringstream jsonPolicyStream; jsonPolicyStream << "{ \"" << TONE_ATTRIBUTE << "\": ["; for (size_t j = 0; j < filterSelections.size(); ++j) { jsonPolicyStream << "\"" << filterSelections[j] << "\""; if (j < filterSelections.size() - 1) { jsonPolicyStream << ","; } } jsonPolicyStream << "] }"; result = jsonPolicyStream.str(); } return result; }
  • 有关API详细信息,请参阅AWS SDK for C++ API参考资料中的订阅

以下代码示例显示了如何使用Unsubscribe

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Delete a subscription to an Amazon Simple Notification Service (Amazon SNS) topic. /*! \param subscriptionARN: The Amazon Resource Name (ARN) for an Amazon SNS topic subscription. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::unsubscribe(const Aws::String &subscriptionARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::UnsubscribeRequest request; request.SetSubscriptionArn(subscriptionARN); const Aws::SNS::Model::UnsubscribeOutcome outcome = snsClient.Unsubscribe(request); if (outcome.IsSuccess()) { std::cout << "Unsubscribed successfully " << std::endl; } else { std::cerr << "Error while unsubscribing " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考资料” 中的 “取消订阅”。

场景

以下代码示例演示如何创建无服务器应用程序,让用户能够使用标签管理照片。

SDK对于 C++

演示如何开发照片资产管理应用程序,该应用程序使用 Amazon Rekognition 检测图像中的标签并将其存储以供日后检索。

有关如何设置和运行的完整源代码和说明,请参阅上的完整示例 GitHub

要深入了解这个例子的起源,请参阅 AWS 社区上的博文。

本示例中使用的服务
  • API网关

  • DynamoDB

  • Lambda

  • Amazon Rekognition

  • Amazon S3

  • Amazon SNS

以下代码示例展示了如何使用 Amazon 发布SMS消息SNS。

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/** * Publish SMS: use Amazon Simple Notification Service (Amazon SNS) to send an SMS text message to a phone number. * Note: This requires additional AWS configuration prior to running example. * * NOTE: When you start using Amazon SNS to send SMS messages, your AWS account is in the SMS sandbox and you can only * use verified destination phone numbers. See https://docs.aws.amazon.com/sns/latest/dg/sns-sms-sandbox.html. * NOTE: If destination is in the US, you also have an additional restriction that you have use a dedicated * origination ID (phone number). You can request an origination number using Amazon Pinpoint for a fee. * See https://aws.amazon.com/blogs/compute/provisioning-and-using-10dlc-origination-numbers-with-amazon-sns/ * for more information. * * <phone_number_value> input parameter uses E.164 format. * For example, in United States, this input value should be of the form: +12223334444 */ //! Send an SMS text message to a phone number. /*! \param message: The message to publish. \param phoneNumber: The phone number of the recipient in E.164 format. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::publishSms(const Aws::String &message, const Aws::String &phoneNumber, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::PublishRequest request; request.SetMessage(message); request.SetPhoneNumber(phoneNumber); const Aws::SNS::Model::PublishOutcome outcome = snsClient.Publish(request); if (outcome.IsSuccess()) { std::cout << "Message published successfully with message id, '" << outcome.GetResult().GetMessageId() << "'." << std::endl; } else { std::cerr << "Error while publishing message " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅在AWS SDK for C++ API参考发布

以下代码示例展示了如何:

  • 创建主题(FIFO或非-FIFO)。

  • 针对主题订阅多个队列,并提供应用筛选条件的选项。

  • 将消息发布到主题。

  • 轮询队列中是否有收到的消息。

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; //! Workflow for messaging with topics and queues using Amazon SNS and Amazon SQS. /*! \param clientConfig Aws client configuration. \return bool: Successful completion. */ bool AwsDoc::TopicsAndQueues::messagingWithTopicsAndQueues( const Aws::Client::ClientConfiguration &clientConfiguration) { std::cout << "Welcome to messaging with topics and queues." << std::endl; printAsterisksLine(); std::cout << "In this workflow, you will create an SNS topic and subscribe " << NUMBER_OF_QUEUES << " SQS queues to the topic." << std::endl; std::cout << "You can select from several options for configuring the topic and the subscriptions for the " << NUMBER_OF_QUEUES << " queues." << std::endl; std::cout << "You can then post to the topic and see the results in the queues." << std::endl; Aws::SNS::SNSClient snsClient(clientConfiguration); printAsterisksLine(); std::cout << "SNS topics can be configured as FIFO (First-In-First-Out)." << std::endl; std::cout << "FIFO topics deliver messages in order and support deduplication and message filtering." << std::endl; bool isFifoTopic = askYesNoQuestion( "Would you like to work with FIFO topics? (y/n) "); bool contentBasedDeduplication = false; Aws::String topicName; if (isFifoTopic) { printAsterisksLine(); std::cout << "Because you have chosen a FIFO topic, deduplication is supported." << std::endl; std::cout << "Deduplication IDs are either set in the message or automatically generated " << "from content using a hash function." << std::endl; std::cout << "If a message is successfully published to an SNS FIFO topic, any message " << "published and determined to have the same deduplication ID, " << std::endl; std::cout << "within the five-minute deduplication interval, is accepted but not delivered." << std::endl; std::cout << "For more information about deduplication, " << "see https://docs.aws.amazon.com/sns/latest/dg/fifo-message-dedup.html." << std::endl; contentBasedDeduplication = askYesNoQuestion( "Use content-based deduplication instead of entering a deduplication ID? (y/n) "); } printAsterisksLine(); Aws::SQS::SQSClient sqsClient(clientConfiguration); Aws::Vector<Aws::String> queueURLS; Aws::Vector<Aws::String> subscriptionARNS; Aws::String topicARN; { topicName = askQuestion("Enter a name for your SNS topic. "); // 1. Create an Amazon SNS topic, either FIFO or non-FIFO. Aws::SNS::Model::CreateTopicRequest request; if (isFifoTopic) { request.AddAttributes("FifoTopic", "true"); if (contentBasedDeduplication) { request.AddAttributes("ContentBasedDeduplication", "true"); } topicName = topicName + FIFO_SUFFIX; std::cout << "Because you have selected a FIFO topic, '.fifo' must be appended to the topic name." << std::endl; } request.SetName(topicName); Aws::SNS::Model::CreateTopicOutcome outcome = snsClient.CreateTopic(request); if (outcome.IsSuccess()) { topicARN = outcome.GetResult().GetTopicArn(); std::cout << "Your new topic with the name '" << topicName << "' and the topic Amazon Resource Name (ARN) " << std::endl; std::cout << "'" << topicARN << "' has been created." << std::endl; } else { std::cerr << "Error with TopicsAndQueues::CreateTopic. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } } printAsterisksLine(); std::cout << "Now you will create " << NUMBER_OF_QUEUES << " SQS queues to subscribe to the topic." << std::endl; Aws::Vector<Aws::String> queueNames; bool filteringMessages = false; bool first = true; for (int i = 1; i <= NUMBER_OF_QUEUES; ++i) { Aws::String queueURL; Aws::String queueName; { printAsterisksLine(); std::ostringstream ostringstream; ostringstream << "Enter a name for " << (first ? "an" : "the next") << " SQS queue. "; queueName = askQuestion(ostringstream.str()); // 2. Create an SQS queue. Aws::SQS::Model::CreateQueueRequest request; if (isFifoTopic) { request.AddAttributes(Aws::SQS::Model::QueueAttributeName::FifoQueue, "true"); queueName = queueName + FIFO_SUFFIX; if (first) // Only explain this once. { std::cout << "Because you are creating a FIFO SQS queue, '.fifo' must " << "be appended to the queue name." << std::endl; } } request.SetQueueName(queueName); queueNames.push_back(queueName); Aws::SQS::Model::CreateQueueOutcome outcome = sqsClient.CreateQueue(request); if (outcome.IsSuccess()) { queueURL = outcome.GetResult().GetQueueUrl(); std::cout << "Your new SQS queue with the name '" << queueName << "' and the queue URL " << std::endl; std::cout << "'" << queueURL << "' has been created." << std::endl; } else { std::cerr << "Error with SQS::CreateQueue. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } } queueURLS.push_back(queueURL); if (first) // Only explain this once. { std::cout << "The queue URL is used to retrieve the queue ARN, which is " << "used to create a subscription." << std::endl; } Aws::String queueARN; { // 3. Get the SQS queue ARN attribute. Aws::SQS::Model::GetQueueAttributesRequest request; request.SetQueueUrl(queueURL); request.AddAttributeNames(Aws::SQS::Model::QueueAttributeName::QueueArn); Aws::SQS::Model::GetQueueAttributesOutcome outcome = sqsClient.GetQueueAttributes(request); if (outcome.IsSuccess()) { const Aws::Map<Aws::SQS::Model::QueueAttributeName, Aws::String> &attributes = outcome.GetResult().GetAttributes(); const auto &iter = attributes.find( Aws::SQS::Model::QueueAttributeName::QueueArn); if (iter != attributes.end()) { queueARN = iter->second; std::cout << "The queue ARN '" << queueARN << "' has been retrieved." << std::endl; } else { std::cerr << "Error ARN attribute not returned by GetQueueAttribute." << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } } else { std::cerr << "Error with SQS::GetQueueAttributes. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } } if (first) { std::cout << "An IAM policy must be attached to an SQS queue, enabling it to receive " "messages from an SNS topic." << std::endl; } { // 4. Set the SQS queue policy attribute with a policy enabling the receipt of SNS messages. Aws::SQS::Model::SetQueueAttributesRequest request; request.SetQueueUrl(queueURL); Aws::String policy = createPolicyForQueue(queueARN, topicARN); request.AddAttributes(Aws::SQS::Model::QueueAttributeName::Policy, policy); Aws::SQS::Model::SetQueueAttributesOutcome outcome = sqsClient.SetQueueAttributes(request); if (outcome.IsSuccess()) { std::cout << "The attributes for the queue '" << queueName << "' were successfully updated." << std::endl; } else { std::cerr << "Error with SQS::SetQueueAttributes. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } } printAsterisksLine(); { // 5. Subscribe the SQS queue to the SNS topic. Aws::SNS::Model::SubscribeRequest request; request.SetTopicArn(topicARN); request.SetProtocol("sqs"); request.SetEndpoint(queueARN); if (isFifoTopic) { if (first) { std::cout << "Subscriptions to a FIFO topic can have filters." << std::endl; std::cout << "If you add a filter to this subscription, then only the filtered messages " << "will be received in the queue." << std::endl; std::cout << "For information about message filtering, " << "see https://docs.aws.amazon.com/sns/latest/dg/sns-message-filtering.html" << std::endl; std::cout << "For this example, you can filter messages by a \"" << TONE_ATTRIBUTE << "\" attribute." << std::endl; } std::ostringstream ostringstream; ostringstream << "Filter messages for \"" << queueName << "\"'s subscription to the topic \"" << topicName << "\"? (y/n)"; // Add filter if user answers yes. if (askYesNoQuestion(ostringstream.str())) { Aws::String jsonPolicy = getFilterPolicyFromUser(); if (!jsonPolicy.empty()) { filteringMessages = true; std::cout << "This is the filter policy for this subscription." << std::endl; std::cout << jsonPolicy << std::endl; request.AddAttributes("FilterPolicy", jsonPolicy); } else { std::cout << "Because you did not select any attributes, no filter " << "will be added to this subscription." << std::endl; } } } // if (isFifoTopic) Aws::SNS::Model::SubscribeOutcome outcome = snsClient.Subscribe(request); if (outcome.IsSuccess()) { Aws::String subscriptionARN = outcome.GetResult().GetSubscriptionArn(); std::cout << "The queue '" << queueName << "' has been subscribed to the topic '" << "'" << topicName << "'" << std::endl; std::cout << "with the subscription ARN '" << subscriptionARN << "." << std::endl; subscriptionARNS.push_back(subscriptionARN); } else { std::cerr << "Error with TopicsAndQueues::Subscribe. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } } first = false; } first = true; do { printAsterisksLine(); // 6. Publish a message to the SNS topic. Aws::SNS::Model::PublishRequest request; request.SetTopicArn(topicARN); Aws::String message = askQuestion("Enter a message text to publish. "); request.SetMessage(message); if (isFifoTopic) { if (first) { std::cout << "Because you are using a FIFO topic, you must set a message group ID." << std::endl; std::cout << "All messages within the same group will be received in the " << "order they were published." << std::endl; } Aws::String messageGroupID = askQuestion( "Enter a message group ID for this message. "); request.SetMessageGroupId(messageGroupID); if (!contentBasedDeduplication) { if (first) { std::cout << "Because you are not using content-based deduplication, " << "you must enter a deduplication ID." << std::endl; } Aws::String deduplicationID = askQuestion( "Enter a deduplication ID for this message. "); request.SetMessageDeduplicationId(deduplicationID); } } if (filteringMessages && askYesNoQuestion( "Add an attribute to this message? (y/n) ")) { for (size_t i = 0; i < TONES.size(); ++i) { std::cout << " " << (i + 1) << ". " << TONES[i] << std::endl; } int selection = askQuestionForIntRange( "Enter a number for an attribute. ", 1, static_cast<int>(TONES.size())); Aws::SNS::Model::MessageAttributeValue messageAttributeValue; messageAttributeValue.SetDataType("String"); messageAttributeValue.SetStringValue(TONES[selection - 1]); request.AddMessageAttributes(TONE_ATTRIBUTE, messageAttributeValue); } Aws::SNS::Model::PublishOutcome outcome = snsClient.Publish(request); if (outcome.IsSuccess()) { std::cout << "Your message was successfully published." << std::endl; } else { std::cerr << "Error with TopicsAndQueues::Publish. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } first = false; } while (askYesNoQuestion("Post another message? (y/n) ")); printAsterisksLine(); std::cout << "Now the SQS queue will be polled to retrieve the messages." << std::endl; askQuestion("Press any key to continue...", alwaysTrueTest); for (size_t i = 0; i < queueURLS.size(); ++i) { // 7. Poll an SQS queue for its messages. std::vector<Aws::String> messages; std::vector<Aws::String> receiptHandles; while (true) { Aws::SQS::Model::ReceiveMessageRequest request; request.SetMaxNumberOfMessages(10); request.SetQueueUrl(queueURLS[i]); // Setting WaitTimeSeconds to non-zero enables long polling. // For information about long polling, see // https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html request.SetWaitTimeSeconds(1); Aws::SQS::Model::ReceiveMessageOutcome outcome = sqsClient.ReceiveMessage(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::SQS::Model::Message> &newMessages = outcome.GetResult().GetMessages(); if (newMessages.empty()) { break; } else { for (const Aws::SQS::Model::Message &message: newMessages) { messages.push_back(message.GetBody()); receiptHandles.push_back(message.GetReceiptHandle()); } } } else { std::cerr << "Error with SQS::ReceiveMessage. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } } printAsterisksLine(); if (messages.empty()) { std::cout << "No messages were "; } else if (messages.size() == 1) { std::cout << "One message was "; } else { std::cout << messages.size() << " messages were "; } std::cout << "received by the queue '" << queueNames[i] << "'." << std::endl; for (const Aws::String &message: messages) { std::cout << " Message : '" << message << "'." << std::endl; } // 8. Delete a batch of messages from an SQS queue. if (!receiptHandles.empty()) { Aws::SQS::Model::DeleteMessageBatchRequest request; request.SetQueueUrl(queueURLS[i]); int id = 1; // Ids must be unique within a batch delete request. for (const Aws::String &receiptHandle: receiptHandles) { Aws::SQS::Model::DeleteMessageBatchRequestEntry entry; entry.SetId(std::to_string(id)); ++id; entry.SetReceiptHandle(receiptHandle); request.AddEntries(entry); } Aws::SQS::Model::DeleteMessageBatchOutcome outcome = sqsClient.DeleteMessageBatch(request); if (outcome.IsSuccess()) { std::cout << "The batch deletion of messages was successful." << std::endl; } else { std::cerr << "Error with SQS::DeleteMessageBatch. " << outcome.GetError().GetMessage() << std::endl; cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient); return false; } } } return cleanUp(topicARN, queueURLS, subscriptionARNS, snsClient, sqsClient, true); // askUser } bool AwsDoc::TopicsAndQueues::cleanUp(const Aws::String &topicARN, const Aws::Vector<Aws::String> &queueURLS, const Aws::Vector<Aws::String> &subscriptionARNS, const Aws::SNS::SNSClient &snsClient, const Aws::SQS::SQSClient &sqsClient, bool askUser) { bool result = true; printAsterisksLine(); if (!queueURLS.empty() && askUser && askYesNoQuestion("Delete the SQS queues? (y/n) ")) { for (const auto &queueURL: queueURLS) { // 9. Delete an SQS queue. Aws::SQS::Model::DeleteQueueRequest request; request.SetQueueUrl(queueURL); Aws::SQS::Model::DeleteQueueOutcome outcome = sqsClient.DeleteQueue(request); if (outcome.IsSuccess()) { std::cout << "The queue with URL '" << queueURL << "' was successfully deleted." << std::endl; } else { std::cerr << "Error with SQS::DeleteQueue. " << outcome.GetError().GetMessage() << std::endl; result = false; } } for (const auto &subscriptionARN: subscriptionARNS) { // 10. Unsubscribe an SNS subscription. Aws::SNS::Model::UnsubscribeRequest request; request.SetSubscriptionArn(subscriptionARN); Aws::SNS::Model::UnsubscribeOutcome outcome = snsClient.Unsubscribe(request); if (outcome.IsSuccess()) { std::cout << "Unsubscribe of subscription ARN '" << subscriptionARN << "' was successful." << std::endl; } else { std::cerr << "Error with TopicsAndQueues::Unsubscribe. " << outcome.GetError().GetMessage() << std::endl; result = false; } } } printAsterisksLine(); if (!topicARN.empty() && askUser && askYesNoQuestion("Delete the SNS topic? (y/n) ")) { // 11. Delete an SNS topic. Aws::SNS::Model::DeleteTopicRequest request; request.SetTopicArn(topicARN); Aws::SNS::Model::DeleteTopicOutcome outcome = snsClient.DeleteTopic(request); if (outcome.IsSuccess()) { std::cout << "The topic with ARN '" << topicARN << "' was successfully deleted." << std::endl; } else { std::cerr << "Error with TopicsAndQueues::DeleteTopicRequest. " << outcome.GetError().GetMessage() << std::endl; result = false; } } return result; } //! Create an IAM policy that gives an SQS queue permission to receive messages from an SNS topic. /*! \sa createPolicyForQueue() \param queueARN: The SQS queue Amazon Resource Name (ARN). \param topicARN: The SNS topic ARN. \return Aws::String: The policy as JSON. */ Aws::String AwsDoc::TopicsAndQueues::createPolicyForQueue(const Aws::String &queueARN, const Aws::String &topicARN) { std::ostringstream policyStream; policyStream << R"({ "Statement": [ { "Effect": "Allow", "Principal": { "Service": "sns.amazonaws.com" }, "Action": "sqs:SendMessage", "Resource": ")" << queueARN << R"(", "Condition": { "ArnEquals": { "aws:SourceArn": ")" << topicARN << R"(" } } } ] })"; return policyStream.str(); }