這裏介紹AWS SDK for C++ 1.0.x版本,好比下載:ios
https://github.com/aws/aws-sdk-cpp/archive/1.0.164.tar.gzc++
環境:RHEL/CentOS 7git
1、編譯SDKgithub
配置yum源:centos
[centos] name=centos7 baseurl=http://mirrors.163.com/centos/7/os/x86_64/ enabled=1 gpgcheck=0
刷新源:yum makecache fast服務器
安裝EPEL源:yum -y install epel-releasecurl
再刷新源:yum makecache fastide
安裝必備軟件和庫:ui
yum -y erase cmake yum -y install cmake3 gcc-c++ libstdc++-devel libcurl-devel zlib-devel cd /usr/bin; ln -s cmake3 cmake
可能還會依賴其它庫,根據編譯時報的錯誤來判斷安裝哪一個包。url
準備源碼:
tar -zxf 1.0.164.tar.gz -C /tmp mkdir -p /tmp/build; cd /tmp/build cmake -DCMAKE_BUILD_TYPE=Release /tmp/aws-sdk-cpp-1.0.164
編譯源碼:
make -j `nproc` -C aws-cpp-sdk-core make -j `nproc` -C aws-cpp-sdk-s3
安裝頭文件和庫到一個目錄:
mkdir -p /tmp/install make install DESTDIR=/tmp/install -C aws-cpp-sdk-core make install DESTDIR=/tmp/install -C aws-cpp-sdk-s3
2、編寫示例代碼
列出桶:
#include <iostream> #include <aws/s3/S3Client.h> #include <aws/core/Aws.h> #include <aws/core/auth/AWSCredentialsProvider.h> using namespace Aws::S3; using namespace Aws::S3::Model; using namespace std; int main(int argc, char* argv[]) { Aws::SDKOptions options; options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace; Aws::InitAPI(options); Aws::Client::ClientConfiguration cfg; cfg.endpointOverride = "10.202.91.2:88"; // S3服務器地址和端口 cfg.scheme = Aws::Http::Scheme::HTTP; cfg.verifySSL = false; Aws::Auth::AWSCredentials cred("81BEB5DB4DED", "WzFGRTc34MzI5NjM0ODcxOUJDM0Jd"); // 認證的Key S3Client client(cred, cfg, false, false); auto response = client.ListBuckets(); if (response.IsSuccess()) { auto buckets = response.GetResult().GetBuckets(); for (auto iter = buckets.begin(); iter != buckets.end(); ++iter) { cout << iter->GetName() << "\t" << iter->GetCreationDate().ToLocalTimeString(Aws::Utils::DateFormat::ISO_8601) << endl; } } else { cout << "Error while ListBuckets " << response.GetError().GetExceptionName() << " " << response.GetError().GetMessage() << endl; } Aws::ShutdownAPI(options); return 0; }
3、編譯示例代碼並運行
g++ -std=c++11 -I/tmp/install/usr/local/include -L/tmp/install/usr/local/lib64 -laws-cpp-sdk-core -laws-cpp-sdk-s3 a.cpp export LD_LIBRARY_PATH=/tmp/install/usr/local/lib64 ./a.out