新規のお客様への AWS Cloud9 の提供は終了しました。AWS Cloud9 の既存のお客様は、通常どおりサービスを引き続き使用できます。詳細はこちら
AWS Cloud9 の C++ チュートリアル
このチュートリアルでは、AWS Cloud9 開発環境で C++ コードを実行できます。このコードは、AWS SDK for C++で提供されたリソースも使用できます。Amazon Web Services に接続するために使用できる、モジュール化されたクロスプラットフォームのオープンソースライブラリです。
このチュートリアルに従って、このサンプルを作成すると、AWS アカウントに料金が発生する可能性があります。Amazon EC2 や Amazon S3 などのサービスに対して発生する可能性がある料金も含まれます。詳細については、「Amazon EC2 料金表
トピック
前提条件
このサンプルを使用する前に、設定が次の要件を満たしていることを確認します。
-
既存の AWS Cloud9 EC2 開発環境が存在している必要があります。このサンプルは、Amazon Linux または Ubuntu Server を実行する Amazon EC2 インスタンスに接続された EC2 環境が既にあることを前提としています。別のタイプの環境またはオペレーティングシステムがある場合、このサンプルの指示を関連ツールを設定する必要がある場合があります。詳細については、「AWS Cloud9 で環境を作成する」を参照してください。
-
既存の環境に既に AWS Cloud9 IDE が開いています。環境を開くと、AWS Cloud9 によってその環境の IDE がウェブブラウザで開かれます。詳細については、「AWS Cloud9 で環境を開く」を参照してください。
ステップ1:g ++と必要な dev パッケージをインストールする
C++ アプリケーションを構築して実行するには、g++
など、GNUコンパイラコレクション (GCC)
また、ヘッダーファイル (-dev
パッケージ) を libcurl
、libopenssl
、libuuid
、zlib
、およびオプションで、libpulse
Amazon Polly のサポートのため追加する必要があります。
開発ツールをインストールするプロセスは、Amazon Linux/Amazon Linux 2 インスタンスと Ubuntu インスタンスのどちらを使用しているかによって若干異なります。
ステップ2:CMakeをインストールする
cmake
ツールをインストールする必要があります。このツールは、ソースコードから実行可能ファイルを構築するプロセスを自動化します。
-
IDE のターミナルウィンドウで、次のコマンドを実行して、必要なアーカイブを取得します。
wget https://cmake.org/files/v3.18/cmake-3.18.0.tar.gz
-
アーカイブからファイルを抽出し、解凍したファイルが含まれているディレクトリに移動します。
tar xzf cmake-3.18.0.tar.gz cd cmake-3.18.0
-
次に、ブートストラップスクリプトを実行し、次のコマンドを実行して
cmake
をインストールします。./bootstrap make sudo make install
-
次のコマンドを実行して、ツールがインストールされていることを確認します。
cmake --version
ステップ 3: SDK for C++ の取得と構築
AWS SDK for C++ を設定するには、ソースから直接 SDK を構築するか、パッケージマネージャーを使用してライブラリからダウンロードできます。使用可能なオプションの詳細については、AWS SDK for C++デベロッパーガイドでAWS SDK for C++ を利用して開始するで見つけることができます。
このサンプルは git
の使用を示し、SDK ソースコードと cmake
のクローンを作成して、SDK for C++ を構築します。
-
ターミナルで次のコマンドを実行して、リモートリポジトリをクローンし、AWS Cloud9 環境のため、すべての git サブモジュールを再帰的に取得します。
git clone --recurse-submodules https://github.com/aws/aws-sdk-cpp
-
新しい
aws-sdk-cpp
ディレクトリに移動し、サブディレクトリを作成して、AWS SDK for C++ into を構築してから、以下に移動します。cd aws-sdk-cpp mkdir sdk_build cd sdk_build
-
注記
時間を節約するため、このステップは、AWS SDK for C++ の Amazon S3 部分のみを構築します。完全なSDKを構築したいのであれば、
cmake
コマンドから-DBUILD_ONLY=s3
を省略します。完全な SDK for C++ を構築すると、Amazon EC2 インスタンスまたは自分のサーバーで使用できるコンピューティングリソースに応じて、完了までに 1 時間を超えることがあります。
cmake
を使って、次のコマンドを実行してsdk_build
ディレクトリに SDK for C++ の Amazon S3 の部分を構築します。cmake .. -DBUILD_ONLY=s3
-
では、
make install
コマンドを実行して、ビルドされた SDK にアクセスできるようにします。sudo make install cd ..
ステップ4:C ++および CMakeLists ファイルを作成する
このステップでは、C++
ファイルを作成し、プロジェクトのユーザーに Amazon S3 バケットと対話を許可します。
また、CMakeLists.txt
ファイルを作成して、C ++ライブラリを構築するために、cmake
で使用する指示を提供します。
-
AWS Cloud9 IDE で、このコンテンツを含むファイルを作成し、環境のルート (
/
) にs3-demo.cpp
という名前をつけてファイルを保存します。#include <iostream> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/Bucket.h> #include <aws/s3/model/CreateBucketConfiguration.h> #include <aws/s3/model/CreateBucketRequest.h> #include <aws/s3/model/DeleteBucketRequest.h> // Look for a bucket among all currently available Amazon S3 buckets. bool FindTheBucket(const Aws::S3::S3Client &s3Client, const Aws::String &bucketName) { Aws::S3::Model::ListBucketsOutcome outcome = s3Client.ListBuckets(); if (outcome.IsSuccess()) { std::cout << "Looking for a bucket named '" << bucketName << "'..." << std::endl << std::endl; Aws::Vector<Aws::S3::Model::Bucket> bucket_list = outcome.GetResult().GetBuckets(); for (Aws::S3::Model::Bucket const &bucket: bucket_list) { if (bucket.GetName() == bucketName) { std::cout << "Found the bucket." << std::endl << std::endl; return true; } } std::cout << "Could not find the bucket." << std::endl << std::endl; } else { std::cerr << "listBuckets error: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } // Create an Amazon S3 bucket. bool CreateTheBucket(const Aws::S3::S3Client &s3Client, const Aws::String &bucketName, const Aws::String ®ion) { std::cout << "Creating a bucket named '" << bucketName << "'..." << std::endl << std::endl; Aws::S3::Model::CreateBucketRequest request; request.SetBucket(bucketName); if (region != "us-east-1") { Aws::S3::Model::CreateBucketConfiguration createBucketConfig; createBucketConfig.SetLocationConstraint( Aws::S3::Model::BucketLocationConstraintMapper::GetBucketLocationConstraintForName( region)); request.SetCreateBucketConfiguration(createBucketConfig); } Aws::S3::Model::CreateBucketOutcome outcome = s3Client.CreateBucket(request); if (outcome.IsSuccess()) { std::cout << "Bucket created." << std::endl << std::endl; } else { std::cerr << "createBucket error: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } // Delete an existing Amazon S3 bucket. bool DeleteTheBucket(const Aws::S3::S3Client &s3Client, const Aws::String &bucketName) { std::cout << "Deleting the bucket named '" << bucketName << "'..." << std::endl << std::endl; Aws::S3::Model::DeleteBucketRequest request; request.SetBucket(bucketName); Aws::S3::Model::DeleteBucketOutcome outcome = s3Client.DeleteBucket(request); if (outcome.IsSuccess()) { std::cout << "Bucket deleted." << std::endl << std::endl; } else { std::cerr << "deleteBucket error: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } #ifndef EXCLUDE_MAIN_FUNCTION // Create an S3 bucket and then delete it. // Before and after creating the bucket, and again after deleting the bucket, // try to determine whether that bucket still exists. int main(int argc, char *argv[]) { if (argc < 3) { std::cout << "Usage: s3-demo <bucket name> <AWS Region>" << std::endl << "Example: s3-demo my-bucket us-east-1" << std::endl; return 1; } Aws::SDKOptions options; Aws::InitAPI(options); { Aws::String bucketName = argv[1]; Aws::String region = argv[2]; Aws::Client::ClientConfiguration config; config.region = region; Aws::S3::S3Client s3Client(config); if (!FindTheBucket(s3Client, bucketName)) { return 1; } if (!CreateTheBucket(s3Client, bucketName, region)) { return 1; } if (!FindTheBucket(s3Client, bucketName)) { return 1; } if (!DeleteTheBucket(s3Client, bucketName)) { return 1; } if (!FindTheBucket(s3Client, bucketName)) { return 1; } } Aws::ShutdownAPI(options); return 0; } #endif // EXCLUDE_MAIN_FUNCTION
-
このコンテンツを含んだ 2 番目のファイルを作成し、環境のルート (
/
) にCMakeLists.txt
という名前をつけてファイルを保存します。このファイルを使用すると、コードを実行可能ファイルに組み込むことができます。# A minimal CMakeLists.txt file for the AWS SDK for C++. # The minimum version of CMake that will work. cmake_minimum_required(VERSION 2.8) # The project name. project(s3-demo) # Locate the AWS SDK for C++ package. set(AWSSDK_ROOT_DIR, "/usr/local/") set(BUILD_SHARED_LIBS ON) find_package(AWSSDK REQUIRED COMPONENTS s3) # The executable name and its source files. add_executable(s3-demo s3-demo.cpp) # The libraries used by your executable. target_link_libraries(s3-demo ${AWSSDK_LINK_LIBRARIES})
ステップ 5: C++ コードを構築および実行する
-
s3-demo.cpp
とCMakeLists.txt
を保存した環境のルートディレクトリで、cmake
を実行してプロジェクトを構築します。cmake . make
-
これで、コマンドラインからプログラムを実行できます。次のコマンドで、
my-unique-bucket-name
を Amazon S3 バケットの一意の名前に置き換え、必要であれば、us-east-1
を別の AWS リージョン (バケットを作成したい場所)の識別子に置き換えます。./s3-demo my-unique-bucket-name us-east-1
プログラムが正常に実行された場合は、次のような出力が返されます。
Looking for a bucket named 'my-unique-bucket-name'... Could not find the bucket. Creating a bucket named 'my-unique-bucket-name'... Bucket created. Looking for a bucket named 'my-unique-bucket-name'... Found the bucket. Deleting the bucket named 'my-unique-bucket-name'... Bucket deleted. Looking for a bucket named 'my-unique-bucket-name'... Could not find the bucket.
ステップ 6: クリーンアップする
このサンプルを使用し終わった後、AWS アカウントで料金が継続的に発生するのを防ぐには、環境を削除する必要があります。手順については、AWS Cloud9 で環境を削除する を参照してください。