美文网首页
cos兼容aws-sdk-c++使用指南

cos兼容aws-sdk-c++使用指南

作者: lewzylu | 来源:发表于2017-07-11 21:52 被阅读0次

1、搭建环境

以下测试均在ubuntu14下进行。

g++4.9.2

http://www.cnblogs.com/loveidea/p/4384837.html/

cmake v3+

https://cmake.org/

以及一些其他的依赖

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

2、安装sdk

下载aws-sdk-c++

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

编译

mkdir sdk_build
cd sdk_build
cmake  -DBUILD_ONLY="s3"  <path/to/sdk/source>
make
make install

详细的安装指南可到官网查询

http://docs.aws.amazon.com/zh_cn/sdk-for-cpp/v1/developer-guide/welcome.html

3、使用SDK

以下为不带签名的使用,即在设置bucket为公有读写之后的操作
更多的操作以及接口可以在这个网址查询

https://sdk.amazonaws.com/cpp/api/0.12.9/de/dcd/namespace_aws_1_1_s3_1_1_model.html
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/DeleteObjectRequest.h>
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/model/Object.h>
#include <aws/core/http/Scheme.h>
#include <iostream>
#include <fstream>
int main(int argc, char** argv)
{
    const Aws::String bucket_name = "lewzylu05-1252448703";
    const Aws::String key_name = "a.cpp";
    const Aws::String file_name_in = "a.cpp";
    const Aws::String file_name_out = "s.cpp";
    
    Aws::SDKOptions options;
    Aws::InitAPI(options);
    std::cout << "Objects in S3 bucket: " << bucket_name << std::endl;
    {
        //config
        Aws::Client::ClientConfiguration awsCC;
        awsCC.scheme = Aws::Http::Scheme::HTTP;
        awsCC.region = "cn-north";
        awsCC.endpointOverride = "cn-north.myqcloud.com"; 
        Aws::S3::S3Client s3_client(awsCC);
       
        //upload object
        Aws::S3::Model::PutObjectRequest put_object_request;
        put_object_request.WithBucket(bucket_name).WithKey(key_name);
        auto input_data = Aws::MakeShared<Aws::FStream>("PutObjectInputStream",file_name_in.c_str(), std::ios_base::in);
        put_object_request.SetBody(input_data);
        auto put_object_outcome = s3_client.PutObject(put_object_request);
        if (put_object_outcome.IsSuccess()) {
            std::cout << "Done!" << std::endl;
        } else {
            std::cout << "GetObject error: " <<
                put_object_outcome.GetError().GetExceptionName() << " " <<
                put_object_outcome.GetError().GetMessage() << std::endl;
        }
        
        //download object
        Aws::S3::Model::GetObjectRequest get_object_request;
        get_object_request.WithBucket(bucket_name).WithKey(key_name);
        auto get_object_outcome = s3_client.GetObject(get_object_request);
        if (get_object_outcome.IsSuccess()) {
            Aws::OFStream local_file;
            local_file.open(file_name_out.c_str(), std::ios::out | std::ios::binary);
            local_file << get_object_outcome.GetResult().GetBody().rdbuf();
            std::cout << "Done!" << std::endl;
        } else {
            std::cout << "GetObject error: " <<
                get_object_outcome.GetError().GetExceptionName() << " " <<
                get_object_outcome.GetError().GetMessage() << std::endl;
        }
        
        //delete object
        Aws::S3::Model::DeleteObjectRequest del_object_request;
        del_object_request.WithBucket(bucket_name).WithKey(key_name);
        auto delete_object_outcome = s3_client.DeleteObject(del_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;
        }

        //list objects
        Aws::S3::Model::ListObjectsRequest list_objects_request;
        list_objects_request.WithBucket(bucket_name);
        auto list_objects_outcome = s3_client.ListObjects(list_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;
        }
    }

    Aws::ShutdownAPI(options);
}

4、V2签名相关

官网提供的SDK不支持V2签名,github上发布的所有官方版本均不支持V2签名。
github较老的版本中有一个非官方支持V2的贡献,但是被作者拒绝了。
我也下载并尝试编译这个版本,但并不能有效地运行。

https://github.com/hkleynhans/aws-sdk-cpp.git
branch:auth_v2_signer

我尝试将这个分支的修改合到最新版本的sdk中,中间也参考了朱戈提供的C的V2签名,但是到目前为止还没有调通。

相关文章

  • cos兼容aws-sdk-c++使用指南

    1、搭建环境 以下测试均在ubuntu14下进行。 g++4.9.2 cmake v3+ 以及一些其他的依赖 2、...

  • cos兼容aws-php-sdk使用指南

    1.SDK名称版本:aws-php-sdk-v2 由于最新版本的cos已经完全兼容s3,那么用aws的sdk来使用...

  • cos兼容aws-go-sdk使用指南

    目前cos现网支持V4签名,所以之前由于V4签名被卡掉的awssdk现在都可以兼容cos了。在这里提供一份aws-...

  • cos兼容s3cmd使用指南

    1.安装s3cmd工具 2.配置信息 输入以下命令 按照提示配置个人信息将配置文件保存至/root/.s3cfg之...

  • cos兼容s3express使用指南(修改endpoint)

    1.下载安装s3express工具 注意: s3express需要购买,但有21天的试用期 2.配置信息 配置用户...

  • cos兼容aws-.net-sdk(c#)使用指南

    1、获取aws . net sdk aws官网sdk 下载msi安装程序 或者直接使用NuGet下载这两个库 2、...

  • cos上传

    使用vue 结合 腾讯cos实现上传图片 cos cos客户端:https://cloud.tencent.com...

  • Gson使用指南 2017-08-15

    Gson使用指南系列其它文章你真的会用Gson吗?Gson使用指南(一)你真的会用Gson吗?Gson使用指南(二...

  • cosplay 哈哈哈哈

    今天看了网络上的cos. 妈耶,笑抽了。 笑抽了,也不忘总结怎么cos,cos也是有套路的。 cos就是模仿,把虚...

  • 常用三角函数公式

    1.诱导公式 sin(-a) = - sin(a) cos(-a) = cos(a) sin(π/2 - a) =...

网友评论

      本文标题:cos兼容aws-sdk-c++使用指南

      本文链接:https://www.haomeiwen.com/subject/ylamhxtx.html