这里介绍AWS SDK for C++ 1.0.x版本,比如下载:
https://github.com/aws/aws-sdk-cpp/archive/1.0.164.tar.gz
环境:RHEL/CentOS 7
一、编译SDK
配置yum源:
[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-release
再刷新源:yum makecache fast
安装必备软件和库:
yum -y erase cmake yum -y install cmake3 gcc-c++ libstdc++-devel libcurl-devel zlib-devel cd /usr/bin; ln -s cmake3 cmake
可能还会依赖其它库,根据编译时报的错误来判断安装哪个包。
准备源码:
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
二、编写示例代码
列出桶:
#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() << " " << 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; }
三、编译示例代码并运行
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