• AWS SDK for C++调用第三方S3 API


    这里介绍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
  • 相关阅读:
    Java版远程控制V1.0
    Struts2使用Kindeditor4.0.3在线编辑器上传图片、视频、FLASH、附件
    给你的网站加上站内搜索Spring+Hibernate基于Compass(基于Lucene)实现
    Hibernate整合进spring使用自己的事务处理
    Hibernate整合进spring使用hibernateTemplate.getSessionFactory().getCurrentSession()理解
    GWT(Google Web Toolkit)入门学习
    转载:狼与哈士奇
    [转]远程桌面无法连接上(管理员已结束了会话)的解决方法
    ibmDW:凤凰涅槃:从 iBatis 到 MyBatis
    hibernate多对多映射拆成2个一对多映射(注解)
  • 原文地址:https://www.cnblogs.com/qiuyi21/p/7239129.html
Copyright © 2020-2023  润新知