AWS SDK for C++ を使用したシンプルなアプリケーションの作成 - AWS SDK for C++

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

AWS SDK for C++ を使用したシンプルなアプリケーションの作成

CMake は、アプリケーションの依存関係を管理し、構築するプラットフォームに適した makefile を作成するために使用するビルドツールです。CMake を使用して、 を使用してプロジェクトを作成および構築できます AWS SDK for C++。

この例では、所有している Amazon S3 バケットを報告します。この例では、 AWS アカウントに Amazon S3 バケットを含める必要はありませんが、少なくとも 1 つあれば、はるかに興味深いものになります。バケットがない場合は、「Amazon Simple Storage Service ユーザーガイド」の「バケットの作成」を参照してください。

ステップ 1: コードを記述する

この例では、1 つのソースファイル (hello_s3.cpp) と 1 つのCMakeLists.txtファイルを含む 1 つのフォルダで構成されます。プログラムは Amazon S3 を使用してストレージバケット情報をレポートします。このコードは、GitHub の AWS Code Examples Repository でも使用できます。

CMakeLists.txt ビルド設定ファイルには多くのオプションを設定できます。詳細については、CMake ウェブサイトの CMake チュートリアルを参照してください。 CMake

注記

詳細: 設定 CMAKE_PREFIX_PATH

デフォルトでは、macOS、Linux、Android、およびその他の Windows 以外のプラットフォーム AWS SDK for C++ 上の は にインストール/usr/localされ、Windows 上の は にインストールされます\Program Files (x86)\aws-cpp-sdk-all

AWS SDK をこれらの標準ロケーションにインストールすると、CMake は必要なリソースを自動的に検出します。ただし、 AWS SDK をカスタムの場所にインストールする場合は、SDK の構築によって生じる以下のリソースの場所を CMake に伝える必要があります。

  • AWSSDKConfig.cmake: CMake にプロジェクト内の AWS SDK ライブラリを検索して使用する方法を指示する設定ファイル。このファイルがないと、CMake は AWS SDK ヘッダーファイルを見つけたり、 AWS SDK ライブラリにリンクしたり、適切なコンパイラフラグを設定したりできません。

  • (バージョン 1.8 以前の場合) 依存関係の場所: aws-c-event-streamaws-c-commonaws-checksums

カスタムインストールパスを設定するには:

cmake -DCMAKE_PREFIX_PATH=/path/to/your/aws-sdk-installation /path/to/project/you/are/building

カスタムインストールCMAKE_PREFIX_PATH用に を設定しない場合、CMake が find_package(AWSSDK)で処理しようとすると、ビルドはAWSSDK が見つからない」などのエラーで失敗しますCMakeLists.txt

注記

Deep Dive: Windows ランタイムライブラリ

プログラムを実行するには、プログラムの実行場所に複数の DLLs が必要です: aws-c-common.dll、、aws-c-event-stream.dllaws-checksums.dllaws-cpp-sdk-core.dll、およびプログラムのコンポーネントに基づく特定の DLLs (この例では Amazon S3 を使用するaws-cpp-sdk-s3ため、 も必要)。CMakeLists.txt ファイル内の 2 番目のifステートメントは、この要件を満たすために、これらのライブラリをインストール場所から実行可能場所にコピーします。 AWSSDK_CPY_DYN_LIBSは、SDK の DLLs をインストール場所からプログラムの実行場所にコピー AWS SDK for C++ する で定義されるマクロです。これらの DLLs「ファイルが見つかりません」というランタイム例外が発生します。これらのエラーが発生した場合は、CMakeLists.txtファイルのこの部分で、一意の環境に必要な変更を確認します。

フォルダとソースファイルを作成するには
  1. ソースファイルを保持するhello_s3ディレクトリやプロジェクトを作成します。

    注記

    Visual Studio でこの例を完了するには、新規プロジェクトの作成を選択し、CMake プロジェクトを選択します。プロジェクトに hello_s3 という名前を付けます。このプロジェクト名は CMakeLists.txt ファイルで使用されます。

  2. そのフォルダ内に、所有している Amazon S3 バケットをレポートする次のコードを含むhello_s3.cppファイルを追加します。

    #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <iostream> #include <aws/core/auth/AWSCredentialsProviderChain.h> using namespace Aws; using namespace Aws::Auth; /* * A "Hello S3" starter application which initializes an Amazon Simple Storage Service (Amazon S3) client * and lists the Amazon S3 buckets in the selected region. * * main function * * Usage: 'hello_s3' * */ 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. int result = 0; { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; // You don't normally have to test that you are authenticated. But the S3 service permits anonymous requests, thus the s3Client will return "success" and 0 buckets even if you are unauthenticated, which can be confusing to a new user. auto provider = Aws::MakeShared<DefaultAWSCredentialsProviderChain>("alloc-tag"); auto creds = provider->GetAWSCredentials(); if (creds.IsEmpty()) { std::cerr << "Failed authentication" << std::endl; } Aws::S3::S3Client s3Client(clientConfig); auto outcome = s3Client.ListBuckets(); if (!outcome.IsSuccess()) { std::cerr << "Failed with error: " << outcome.GetError() << std::endl; result = 1; } else { std::cout << "Found " << outcome.GetResult().GetBuckets().size() << " buckets\n"; for (auto &bucket: outcome.GetResult().GetBuckets()) { std::cout << bucket.GetName() << std::endl; } } } Aws::ShutdownAPI(options); // Should only be called once. return result; }
  3. プロジェクトの名前、実行可能ファイル、ソースCMakeLists.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 s3) # Set this project's name. project("hello_s3") # 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_s3.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

ステップ 2: CMake で構築する

CMake は の情報CMakeLists.txtを使用して実行可能プログラムを構築します。

IDE の標準プラクティスに従ってアプリケーションを構築することをお勧めします。

コマンドラインからアプリケーションを構築するには
  1. cmake がアプリケーションを構築するディレクトリを作成します。

    mkdir my_project_build
  2. ビルドディレクトリに変更し、プロジェクトのソースディレクトリへのパスcmakeを使用して を実行します。

    cd my_project_build cmake ../
  3. がビルドディレクトリcmakeを生成したら、 make または MSBUILD (msbuild ALL_BUILD.vcxproj nmakeまたは cmake --build . --config=Debug) を使用してアプリケーションをビルドできます。

ステップ 3: を実行する

このアプリケーションを実行すると、Amazon S3 バケットの総数と各バケットの名前を一覧表示するコンソール出力が表示されます。

IDE の標準プラクティスに従ってアプリケーションを実行することをお勧めします。

注記

必ずサインインしてください。IAM Identity Center を使用して認証する場合は、 コマンドを使用して AWS CLI aws sso loginサインインすることを忘れないでください。

コマンドラインを使用してプログラムを実行するには
  1. ビルドの結果が生成されたデバッグディレクトリに変更します。

  2. 実行可能ファイルの名前を使用してプログラムを実行します。

    hello_s3

を使用したその他の例については AWS SDK for C++、「」を参照してくださいAWS SDK for C++ AWS のサービス を使用して を呼び出すためのガイド付き例