AWS Cloud9용 C++ 자습서 - AWS Cloud9

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS Cloud9용 C++ 자습서

이 자습서를 사용하면 AWS Cloud9 개발 환경에서 C++ 코드를 실행할 수 있습니다. 이 코드는 Amazon Web Services에 연결하는 데 사용할 수 있는 모듈화된 플랫폼 간 오픈 소스 라이브러리인 AWS SDK for C++에서 제공되는 리소스도 사용합니다.

이 자습서를 따르고 이 샘플을 생성하면 AWS 계정에 요금이 발생할 수 있습니다. 여기에는 Amazon EC2 및 Amazon S3 같은 서비스에 대한 발생할 수 있는 요금이 포함됩니다. 자세한 내용은 Amazon EC2 요금Amazon S3 요금을 참조하세요.

필수 조건

이 샘플을 사용하기 전에 설정이 다음 요구 사항을 충족하는지 확인하세요.

  • 기존 AWS Cloud9 EC2 개발 환경이 있어야 합니다. 이 샘플에서는 Amazon Linux 또는 Ubuntu 서버를 실행 중인 Amazon EC2 인스턴스에 연결된 EC2 환경이 이미 있다고 가정합니다. 다른 환경 또는 운영 시스템이라면 이 샘플 지침을 관련 도구를 설치하는 데에 적용해야 합니다. 자세한 내용은 AWS Cloud9에서 환경 생성 섹션을 참조하세요.

  • 기존 환경에 대한 AWS Cloud9 IDE가 이미 열려 있습니다. 환경을 열 때 AWS Cloud9은 웹 브라우저에서 환경을 위한 IDE를 엽니다. 자세한 내용은 AWS Cloud9에서 환경 열기 섹션을 참조하세요.

1단계: g++ 및 필요한 개발 패키지 설치

C++ 애플리케이션을 빌드하고 실행하려면 GNU Complier Collection(GCC)에서 제공하는 C++ 컴파일러인 g++ 같은 유틸리티가 필요합니다.

또한 libcurl, libopenssl, libuuid, zlib용 헤더 파일(-dev 패키지)과 선택적으로 Amazon Polly 지원을 위한 libpulse도 필요합니다.

개발 도구를 설치하는 프로세스는 Amazon Linux/Amazon Linux 2 인스턴스를 사용하는지 아니면 Ubuntu 인스턴스를 사용하는지에 따라 약간 다릅니다.

Amazon Linux-based systems

AWS Cloud9 터미널에서 다음 명령을 실행하여 gcc가 이미 설치되어 있는지 확인할 수 있습니다.

g++ --version

g++가 설치되어 있지 않은 경우 ‘Development Tools’라는 패키지 그룹의 일부로 손쉽게 설치할 수 있습니다. 이러한 도구는 yum groupinstall 명령을 사용하여 인스턴스에 추가됩니다.

sudo yum groupinstall "Development Tools"

g++ --version을 다시 실행하여 컴파일러가 설치되었는지 확인합니다.

이제 시스템의 패키지 관리자를 사용하여 필요한 라이브러리의 패키지를 설치합니다.

sudo yum install libcurl-devel openssl-devel libuuid-devel pulseaudio-libs-devel
Ubuntu-based systems

AWS Cloud9 터미널에서 다음 명령을 실행하여 gcc가 이미 설치되어 있는지 확인할 수 있습니다.

g++ --version

gcc가 설치되어 있지 않은 경우, 다음 명령을 실행하여 Ubuntu 기반 시스템에 설치할 수 있습니다.

sudo apt update sudo apt install build-essential sudo apt-get install manpages-dev

g++ --version을 다시 실행하여 컴파일러가 설치되었는지 확인합니다.

이제 시스템의 패키지 관리자를 사용하여 필요한 라이브러리의 패키지를 설치합니다.

sudo apt-get install libcurl4-openssl-dev libssl-dev uuid-dev zlib1g-dev libpulse-dev

2단계: CMake 설치

소스 코드에서 실행 파일을 작성하는 프로세스를 자동화하는 cmake 도구를 설치해야 합니다.

  1. IDE 터미널 창에서 다음 명령을 실행하여 필요한 아카이브를 가져옵니다.

    wget https://cmake.org/files/v3.18/cmake-3.18.0.tar.gz
  2. 아카이브에서 파일을 추출하고 압축을 푼 파일이 포함된 디렉터리로 이동합니다.

    tar xzf cmake-3.18.0.tar.gz cd cmake-3.18.0
  3. 그런 다음 부트 스트랩 스크립트를 실행하고 다음 명령을 실행하여 cmake를 설치합니다.

    ./bootstrap make sudo make install
  4. 다음 명령을 실행하여 도구를 설치했는지 확인합니다.

    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++를 빌드하는 방법을 보여줍니다.

  1. 터미널에서 다음 명령을 실행하여 원격 리포지토리를 복제하고 AWS Cloud9 환경의 모든 git 하위 모듈을 재귀적으로 가져옵니다.

    git clone --recurse-submodules https://github.com/aws/aws-sdk-cpp
  2. aws-sdk-cpp 디렉터리로 이동하여 AWS SDK for C++를 빌드할 하위 디렉터리를 생성하고 다음으로 이동합니다.

    cd aws-sdk-cpp mkdir sdk_build cd sdk_build
  3. 참고

    시간을 절약하기 위해 이 단계에서는 AWS SDK for C++의 Amazon S3 부분만 빌드합니다. 전체 SDK를 빌드하려면 cmake 명령에서 -DBUILD_ONLY=s3를 생략하세요.

    전체 SDK for C++를 구축하는 데에는 Amazon EC2 인스턴스 또는 자체 서버에서 사용할 수 있는 컴퓨팅 리소스에 따라 1시간 이상 걸릴 수 있습니다.

    다음 명령을 실행함으로써 cmake를 사용하여 SDK for C++의 Amazon S3 부분을 sdk_build 디렉터리에 빌드합니다.

    cmake .. -DBUILD_ONLY=s3
  4. 이제 빌드된 SDK에 액세스할 수 있도록 make install 명령을 실행합니다.

    sudo make install cd ..

4단계: C++ 및 CMakeLists 파일 생성

이 단계에서는 프로젝트 사용자가 Amazon S3 버킷과 상호 작용할 수 있도록 하는 C++ 파일을 생성합니다.

또한 cmake가 C++ 라이브러리를 빌드하는 데 사용하는 지침을 제공하는 CMakeLists.txt 파일을 생성합니다.

  1. 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& region) { 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 TESTING_BUILD // 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 bucket_name = argv[1]; Aws::String region = argv[2]; Aws::Client::ClientConfiguration config; config.region = region; Aws::S3::S3Client s3_client(config); if (!FindTheBucket(s3_client, bucket_name)) { return 1; } if (!CreateTheBucket(s3_client, bucket_name, region)) { return 1; } if (!FindTheBucket(s3_client, bucket_name)) { return 1; } if (!DeleteTheBucket(s3_client, bucket_name)) { return 1; } if (!FindTheBucket(s3_client, bucket_name)) { return 1; } } Aws::ShutdownAPI(options); return 0; } #endif // TESTING_BUILD
  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++ 코드 빌드 및 실행

  1. s3-demo.cppCMakeLists.txt를 저장한 환경의 루트 디렉터리에서 cmake를 실행하여 프로젝트를 빌드합니다.

    cmake . make
  2. 이제 명령줄에서 프로그램을 실행할 수 있습니다. 다음 명령에서 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에서 환경 삭제 단원을 참조하세요.