问题描述:
需要用C++进行minIO上传和下载,结果发现minIO SDK不支持C++,可以采用aws SDK进行上传和下载,因为minIO是遵循aws S3协议的。
解决办法
- 下载aws sdk for c++源码:
我下的是: https://github.com/aws/aws-sdk-cpp/archive/1.0.164.tar.gz 最新已经到1.7了,但是1.7需要从网上下依赖包,由于内网环境,因此下1.0就够了
- windowns环境下安装cmake和git
自行下载和安装 - 解压aws-sdk-cpp-1.0.164
- 创建编译路径,并编译生成项目
mkdir D:\tmp\build
cd D:\tmp\build
cmake D:xxx\xxx\aws-sdk-cpp-1.0.164 -G "Visual Studio 12 2013 Win64"
-G 是指定用visual studio进行编译,默认是会选择本机最新的vs环境编译,并且默认是x86的
- 编译完成后打开build下面的
AWSSDK.sln
- 选择aws-cpp-sdk-s3工程进行编译,生成如下文件
aws-cpp-sdk-core.lib
aws-cpp-sdk-s3.lib
aws-cpp-sdk-core.dll
aws-cpp-sdk-s3.dll
这就是项目中需要的库
- 把aws-sdk-cpp-1.0.164里include中aws-cpp-sdk-s3和aws-cpp-sdk-core的头文件考到项目中
- 实现上传下载
#define USE_IMPORT_EXPORT
#include <iostream>
#include <fstream>
#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;
#include "aws\s3\model\PutObjectRequest.h"
#include "aws\s3\model\GetObjectRequest.h"
bool uploadfile(std::string BucketName, std::string objectKey, std::string pathkey)
{
Aws::SDKOptions m_options;
S3Client * m_client = { NULL };
Aws::InitAPI(m_options);
Aws::Client::ClientConfiguration cfg;
cfg.endpointOverride = "192.168.1.1:9000"; // S3服务器地址和端口
cfg.scheme = Aws::Http::Scheme::HTTP;
cfg.verifySSL = false;
//Aws::Auth::AWSCredentials cred("RPW421T9GSIO4A45Y9ZR", "2owKYy9emSS90Q0pXuyqpX1OxBCyEDYodsiBemcq"); // 认证的Key
m_client = new S3Client(Aws::Auth::AWSCredentials("test", "12345678"), cfg, false, false);
PutObjectRequest putObjectRequest;
putObjectRequest.WithBucket(BucketName.c_str()).WithKey(objectKey.c_str());
auto input_data = Aws::MakeShared<Aws::FStream>("PutObjectInputStream", pathkey.c_str(), std::ios_base::in | std::ios_base::binary);
putObjectRequest.SetBody(input_data);
auto putObjectResult = m_client->PutObject(putObjectRequest);
if (putObjectResult.IsSuccess())
{
std::cout << "Done!" << std::endl;
return true;
}
else
{
std::cout << "PutObject error: " <<
putObjectResult.GetError().GetExceptionName() << " " <<
putObjectResult.GetError().GetMessage() << std::endl;
return false;
}
if (m_client != nullptr)
{
delete m_client;
m_client = NULL;
}
Aws::ShutdownAPI(m_options);
}
bool downloadfile(std::string BucketName, std::string objectKey, std::string pathkey)
{
Aws::SDKOptions m_options;
S3Client * m_client = { NULL };
Aws::InitAPI(m_options);
Aws::Client::ClientConfiguration cfg;
cfg.endpointOverride = "192.168.1.1:9000"; // S3服务器地址和端口
cfg.scheme = Aws::Http::Scheme::HTTP;
cfg.verifySSL = false;
//Aws::Auth::AWSCredentials cred("RPW421T9GSIO4A45Y9ZR", "2owKYy9emSS90Q0pXuyqpX1OxBCyEDYodsiBemcq"); // 认证的Key
m_client = new S3Client(Aws::Auth::AWSCredentials("test", "12345678"), cfg, false, false);
Aws::S3::Model::GetObjectRequest object_request;
object_request.WithBucket(BucketName.c_str()).WithKey(objectKey.c_str());
auto get_object_outcome = m_client->GetObject(object_request);
if (get_object_outcome.IsSuccess())
{
Aws::OFStream local_file;
local_file.open(pathkey.c_str(), std::ios::out | std::ios::binary);
local_file << get_object_outcome.GetResult().GetBody().rdbuf();
std::cout << "Done!" << std::endl;
return true;
}
else
{
std::cout << "GetObject error: " <<
get_object_outcome.GetError().GetExceptionName() << " " <<
get_object_outcome.GetError().GetMessage() << std::endl;
return false;
}
if (m_client != nullptr)
{
delete m_client;
m_client = NULL;
}
Aws::ShutdownAPI(m_options);
return true;
}
遇到的问题
- 在下载的时候编译报错如下:
error LNK2001: 无法解析的外部符号 "char const * const Aws::Http::CONTENT_TYPE_HEADER" (?CONTENT_TYPE_HEADER@Http@Aws@@3PEBDEB)
解决办法:
必须#define USE_IMPORT_EXPORT
在应用程序中也包含任何内容之前进行定义, 定义需要在aws头文件引入之前。
之后,AWS_CORE_API 在Core_EXPORTS.h中正确定义为_declspec(dllimport)
不知道为什么这是必要的,为什么没有在文档中提及。