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签名,但是到目前为止还没有调通。