史上最全C++ S3 SDK使用流程梳理

1、aws-s3 sdk获取

https://github.com/aws/aws-sdk-cpp

以上链接可获取aws各版本的c++ sdk,目前最新版本为1.8,请根据需要获取相应版本sdk。

如:wget https://github.com/aws/aws-sdk-cpp/archive/1.0.164.tar.gz

2、安装必备软件以及依赖库

yum -y erase cmake

yum -y install cmake3 gcc-c++ libstdc++-devel libcurl-devel zlib-devel

cd /usr/bin && ln -s cmake3 cmake

主要为c++代码编译所准备,因环境不同,若后续编译报错,请根据报错安装相关依赖。

3、准备源码

tar -zxvf 源码包名 -C /root/

mkdir -p /root/build && cd /root/build

cmake -DCMAKE_BUILD_TYPE=Release /root/解压后的源码文件夹名

4、编译源码

仅使用s3 sdk 的情况下只需要编译aws-cpp-sdk-core和aws-cpp-sdk-s3。nproc为自动匹配你的机器是几核。

make -j `nproc` -C aws-cpp-sdk-core

make -j `nproc` -C aws-cpp-sdk-s3

5、安装头文件和库到一个目录

mkdir -p /root/install

make install DESTDIR=/root/install -C aws-cpp-sdk-core

make install DESTDIR=/root/install -C aws-cpp-sdk-s3

6、编写代码(示例)

1)列出桶

#include <iostream>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.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[]) {
    // 初始化API
    Aws::SDKOptions options;
    options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
    Aws::InitAPI(options);

    Aws::Client::ClientConfiguration cfg;
    cfg.endpointOverride = "192.168.11.201:7480";  // S3服务器地址和端口
    cfg.scheme = Aws::Http::Scheme::HTTP;
    cfg.verifySSL = false;

    Aws::Auth::AWSCredentials cred("84MLS4O2U9PVF1F6FFDD", "f5LfdftuEOE9TVtlfHazRAkDXbYSuq85mgnRdfUp");  // ak,sk
    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;
}

2)创建桶

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.h>
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>

using namespace Aws::S3;
using namespace Aws::S3::Model;
using namespace std;

/**
 * 指定区域创建s3存储桶
 */
bool create_bucket(const Aws::String &bucket_name,
    const Aws::S3::Model::BucketLocationConstraint &region = Aws::S3::Model::BucketLocationConstraint::us_east_2)
{
    // 设置请求
    Aws::S3::Model::CreateBucketRequest request;
    request.SetBucket(bucket_name);

    // 如果区域不是默认的us-east-2,是自定义区域
    if (region != Aws::S3::Model::BucketLocationConstraint::us_east_2)
    {
        // 将区域指定为位置约束
        Aws::S3::Model::CreateBucketConfiguration bucket_config;
        bucket_config.SetLocationConstraint(region);
        request.SetCreateBucketConfiguration(bucket_config);
    }

    // 创建存储桶

    Aws::Client::ClientConfiguration cfg;
    cfg.endpointOverride = "192.168.11.201:7480";  // S3服务器地址和端口
    cfg.scheme = Aws::Http::Scheme::HTTP;
    cfg.verifySSL = false;

    Aws::Auth::AWSCredentials cred("84MLS4O2U9PVF1F6FFDD", "f5LfdftuEOE9TVtlfHazRAkDXbYSuq85mgnRdfUp");  // ak,sk
    S3Client client(cred, cfg, false, false);
    auto outcome = client.CreateBucket(request);
	//如果创建存储桶失败
    if (!outcome.IsSuccess())
    {
        auto err = outcome.GetError();
        std::cout << "ERROR: CreateBucket: " << 
            err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
        return false;
    }
    return true;
}


int main()
{

    // 设置默认区域要创建的桶名
    const Aws::String bucket_name_in_default_region = "BUCKET1";
	// 设置指定区域要创建的桶名
    const Aws::String bucket_name_in_specified_region = "BUCKET_NAME";
    const Aws::S3::Model::BucketLocationConstraint region = 
        Aws::S3::Model::BucketLocationConstraint::us_west_2;

    Aws::SDKOptions options;
    Aws::InitAPI(options);
    {
        // 在默认区域(us-east-2)中创建桶
        if (create_bucket(bucket_name_in_default_region))
        {
            std::cout << "在默认区域创建存储桶: " << bucket_name_in_default_region << "\n";
        }

        // 在指定区域中创建桶
        /*if (create_bucket(bucket_name_in_specified_region, region))
        {
            std::cout << "在指定区域中创建存储桶: " << bucket_name_in_specified_region << std::endl;
        }*/
    }
    Aws::ShutdownAPI(options);
}

3)删除桶

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/DeleteBucketRequest.h>
#include <aws/core/auth/AWSCredentialsProvider.h>

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        std::cout << "DeleteBucket - delete an S3 bucket" << std::endl
            << "\nUsage:" << std::endl
            << "  DeleteBucket <bucket> [region]" << std::endl
            << "\nWhere:" << std::endl
            << "  bucket - the bucket to delete" << std::endl
            << "  region - AWS region for the bucket" << std::endl
            << "           (optional, default: us-east-2)" << std::endl
            << "\注意!! 存储桶删除将不可逆!"
            << std::endl
            << "\nExample:" << std::endl
            << "  DeleteBucket testbucket\n" << std::endl << std::endl;
        exit(1);
    }

    Aws::SDKOptions options;
    Aws::InitAPI(options);
    {
        const Aws::String bucket_name = argv[1];
        const Aws::String user_region = (argc >= 3) ? argv[2] : "us-east-2";

        std::cout << "Deleting S3 bucket: " << bucket_name << std::endl;

		Aws::Client::ClientConfiguration cfg;
		cfg.endpointOverride = "192.168.11.201:7480";  // S3服务器地址和端口
		cfg.scheme = Aws::Http::Scheme::HTTP;
		cfg.verifySSL = false;

		Aws::Auth::AWSCredentials cred("84MLS4O2U9PVF1F6FFDD", "f5LfdftuEOE9TVtlfHazRAkDXbYSuq85mgnRdfUp");  // ak,sk
		
        cfg.region = user_region;
        Aws::S3::S3Client s3_client(cred, cfg, false, false);

        Aws::S3::Model::DeleteBucketRequest bucket_request;
        bucket_request.SetBucket(bucket_name);

        auto outcome = s3_client.DeleteBucket(bucket_request);

        if (outcome.IsSuccess())
        {
            std::cout << "Done!" << std::endl;
        }
        else
        {
            std::cout << "删除存储桶错误: "
                << outcome.GetError().GetExceptionName() << " - "
                << outcome.GetError().GetMessage() << std::endl;
        }
    }
    Aws::ShutdownAPI(options);
}

4) 上传文件

#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <iostream>
#include <fstream>
#include <sys/stat.h>

using namespace std;

inline bool file_exists(const string& name)
{
    struct stat buffer;
    return (stat(name.c_str(), &buffer) == 0);
}


bool put_s3_object(const Aws::String& s3_bucket_name, 
    const Aws::String& s3_object_name, 
    const string& file_name, 
    const Aws::String& region = "")
{
    // 判断文件是否存在
    if (!file_exists(file_name)) {
        cout << "ERROR: 找不到这个文件,这个文件不存在" 
            << endl;
        return false;
    }

    // 如果指定了地区
    Aws::Client::ClientConfiguration cfg;
    if (!region.empty())
        cfg.region = region;


	//s3连接
    cfg.endpointOverride = "192.168.11.201:7480";  // S3服务器地址和端口
    cfg.scheme = Aws::Http::Scheme::HTTP;
    cfg.verifySSL = false;

    Aws::Auth::AWSCredentials cred("84MLS4O2U9PVF1F6FFDD", "f5LfdftuEOE9TVtlfHazRAkDXbYSuq85mgnRdfUp");  // ak,sk
    Aws::S3::S3Client s3_client(cred, cfg, false, false);
    Aws::S3::Model::PutObjectRequest object_request;

    object_request.SetBucket(s3_bucket_name);
    object_request.SetKey(s3_object_name);
    const shared_ptr<Aws::IOStream> input_data = 
        Aws::MakeShared<Aws::FStream>("SampleAllocationTag", 
                                      file_name.c_str(), 
                                      ios_base::in | ios_base::binary);
    object_request.SetBody(input_data);

    // 上传文件
    auto put_object_outcome = s3_client.PutObject(object_request);
    if (!put_object_outcome.IsSuccess()) {
        auto error = put_object_outcome.GetError();
        cout << "ERROR: " << error.GetExceptionName() << ": " 
            << error.GetMessage() << endl;
        return false;
    }
    return true;
}


int main(int argc, char** argv)
{

	if (argc < 2)
    {
        cout << "put_object - 上传一个文件" << endl
            << "\nUsage:" << endl
            << "  put_object <bucket> <objname> <objname>" << endl
            << "\nWhere:" << endl
            << "  bucket - 桶名称" << endl
            << "  objname - 文件名" << endl
            << "\nExample:" << endl
            << "  put_object testbucket testfile testfile\n" << endl << endl;
        exit(1);
    }

    Aws::SDKOptions options;
    Aws::InitAPI(options);
    {
		
        const Aws::String bucket_name = argv[1];
        const string file_name = argv[2];
        const Aws::String object_name = argv[3];
        const Aws::String region = "";     
		
        if (put_s3_object(bucket_name, object_name, file_name, region)) {
            cout << "上传文件 " << file_name 
                << " 到存储桶 " << bucket_name 
                << " 存为 " << object_name << endl;
        }
    }
    Aws::ShutdownAPI(options);
}

5)列出文件

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/s3/model/Object.h>
#include <aws/core/auth/AWSCredentialsProvider.h>

int main(int argc, char** argv)
{
    Aws::SDKOptions options;
    Aws::InitAPI(options);
    {
        if (argc < 2)
        {
            std::cout << "Ex: list_objects <bucket-name>" << std::endl
                << std::endl;
            exit(1);
        }

        const Aws::String bucket_name = argv[1];
        std::cout << "在" << bucket_name << "桶中搜索到文件" << std::endl;

		Aws::Client::ClientConfiguration cfg;
		cfg.endpointOverride = "192.168.11.201:7480";  // S3服务器地址和端口
		cfg.scheme = Aws::Http::Scheme::HTTP;
		cfg.verifySSL = false;

		Aws::Auth::AWSCredentials cred("84MLS4O2U9PVF1F6FFDD", "f5LfdftuEOE9TVtlfHazRAkDXbYSuq85mgnRdfUp");  // ak,sk
		Aws::S3::S3Client s3_client(cred, cfg, false, false);

        Aws::S3::Model::ListObjectsRequest objects_request;
        objects_request.WithBucket(bucket_name);

        auto list_objects_outcome = s3_client.ListObjects(objects_request);

        if (list_objects_outcome.IsSuccess())
        {
            Aws::Vector<Aws::S3::Model::Object> object_list =
                list_objects_outcome.GetResult().GetContents();

            for (auto const &s3_object : object_list)
            {
                std::cout << "* " << s3_object.GetKey() << std::endl;
            }
        }
        else
        {
            std::cout << "ListObjects error: " <<
                list_objects_outcome.GetError().GetExceptionName() << " " <<
                list_objects_outcome.GetError().GetMessage() << std::endl;
        }
        // snippet-end:[s3.cpp.list_objects.code]
    }

    Aws::ShutdownAPI(options);
}

6)删除文件

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/DeleteObjectRequest.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <fstream>

int main(int argc, char** argv)
{
    if (argc < 3)
    {
        std::cout << "delete from it." << std::endl << std::endl <<
            "Ex: delete_object <bucketname> <filename>\n" << std::endl;
        exit(1);
    }

    Aws::SDKOptions options;
    Aws::InitAPI(options);
    {
        const Aws::String bucket_name = argv[1];
        const Aws::String key_name = argv[2];

        std::cout << "从存储桶" << bucket_name << " 删除文件 " <<
            key_name << std::endl;


        Aws::Client::ClientConfiguration cfg;
		cfg.endpointOverride = "192.168.11.201:7480";  // S3服务器地址和端口
		cfg.scheme = Aws::Http::Scheme::HTTP;
		cfg.verifySSL = false;

		Aws::Auth::AWSCredentials cred("84MLS4O2U9PVF1F6FFDD", "f5LfdftuEOE9TVtlfHazRAkDXbYSuq85mgnRdfUp");  // ak,sk
		Aws::S3::S3Client s3_client(cred, cfg, false, false);


        Aws::S3::Model::DeleteObjectRequest object_request;
        object_request.WithBucket(bucket_name).WithKey(key_name);

        auto delete_object_outcome = s3_client.DeleteObject(object_request);

        if (delete_object_outcome.IsSuccess())
        {
            std::cout << "Done!" << std::endl;
        }
        else
        {
            std::cout << "DeleteObject error: " <<
                delete_object_outcome.GetError().GetExceptionName() << " " <<
                delete_object_outcome.GetError().GetMessage() << std::endl;
        }

    }

    Aws::ShutdownAPI(options);
}

7、编译代码

g++ -std=c++11 -I/root/install/usr/local/include -L/root/install/usr/local/lib64 -laws-cpp-sdk-core -laws-cpp-sdk-s3 ListBucket.cpp -o ListBucket

g++ -std=c++11 -I/root/install/usr/local/include -L/root/install/usr/local/lib64 -laws-cpp-sdk-core -laws-cpp-sdk-s3 CreateBucket.cpp -o CreateBucket

g++ -std=c++11 -I/root/install/usr/local/include -L/root/install/usr/local/lib64 -laws-cpp-sdk-core -laws-cpp-sdk-s3 DeleteBucket.cpp -o DeleteBucket

export LD_LIBRARY_PATH=/root/install/usr/local/lib64

8、执行

1)Listbucket在这里插入图片描述
2) CreateBucket
在这里插入图片描述
boto3验证
在这里插入图片描述
3)DeleteBucket
在这里插入图片描述
4) PutObject
在这里插入图片描述
5)ListObject
在这里插入图片描述
6)DeleteObject
在这里插入图片描述

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐