C++实现minio的文件上传下载

参考: https://www.jianshu.com/p/74f13cd08cc7

问题
需要用C++进行minIO上传和下载,结果发现minIO SDK不支持C++,可以采用aws SDK进行上传和下载,因为minIO是遵循aws S3协议的。

本人开发环境:vs2022,平台工具集vs2015(v140)

  1. 载aws sdk for c++源码: https://github.com/aws/aws-sdk-cpp/archive/1.0.164.tar.gz

  2. windows下需安装cmake,使用CMake打开,生成项目选择vs2015,x64,生成

  3. 打开build下的AWSSDK.sln使用vs2022,windowsSDK无升级,平台工具集无升级

  4. 选择aws-cpp-sdk-s3工程进行编译,生成如下文件,这就是项目中需要用到的库

    aws-cpp-sdk-core.lib
    aws-cpp-sdk-s3.lib
    aws-cpp-sdk-core.dll
    aws-cpp-sdk-s3.dll
    
  5. 把aws-sdk-cpp-1.0.164里include中aws-cpp-sdk-s3和aws-cpp-sdk-core的头文件考到项目中,并设置附加库目录以及附加依赖项

  6. 代码实现

#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;
}
  1. 本人项目生成的是dll,记得把aws-cpp-sdk-core.dll aws-cpp-sdk-s3.dll拷贝到exe文件目录下,即可完成minio上传下载文件

  2. 可能会出现的问题和解决方法
    https://github.com/aws/aws-sdk-cpp/issues/625
    https://github.com/aws/aws-sdk-cpp/issues/1235

posted @ 2023-03-30 10:35  SpringBreath  阅读(2295)  评论(0编辑  收藏  举报