栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

在cpp上使用aws-cpp-sdk-s3连接minio的工具代码

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在cpp上使用aws-cpp-sdk-s3连接minio的工具代码

目录

使用docker创建minio分布式存储服务器配置aws-cpp-sdk-s3实现的功能s3_utils.hpps3_utils.cpp

使用docker创建minio分布式存储服务器

拉取minio

docker pull minio/minio

创建minio容器

docker run -p 9000:9000 -p 9001:9001 --name minio -e "MINIO_ROOT_USER=hongyaohongyao" -e "MINIO_ROOT_PASSWORD=hongyaohongyao123" -v /d/cache/docker/minio/data:/data -d minio/minio server /data --console-address ":9001"

MINIO_ROOT_USER也对应后面MinioClient初始化参数中的accessKey
MINIO_ROOT_PASSWORD对应secretKey

/d/表示D盘,即D://,docker的语法。

配置aws-cpp-sdk-s3

详见我的另一篇博客

实现的功能

直接上传文本内容直接下载文本内容上传opencv的图片

因为个人的项目只需要在内存中操作数据,所以暂不支持本地文件的上传下载,有需要可以自己实现,思路差不多。

s3_utils.hpp

里面还加了直接上传opencv图像的方法,如果不需要可以删掉。

#ifndef S3_UTILS_HPP
#define S3_UTILS_HPP

#include 
#include 
#include 
#include 

namespace s3_utils {
    static void initAwsAPI(bool shutdown = false);

    class MinioClient {
        std::shared_ptr client_;
        Aws::SDKOptions options_;
    public:
        MinioClient(const std::string &endpoint, const std::string &accessKey, const std::string &secretKey);

        bool upload(const cv::Mat &img, const std::vector &vecCompression_params, const std::string &bucketName,
                    const std::string &objectKey);

        bool upload(const std::string &contents, const std::string &bucketName,
                    const std::string &objectKey);

        std::string download(const std::string &bucketName, const std::string &objectKey);
    };
}

#endif //S3_UTILS_HPP

s3_utils.cpp

#include 提供的INFO方法,可以自己换成其他的logger

注意Aws::InitAPI和Aws::ShutdownAPI是全局的,影响的是整个程序,所以启动和关闭都在整个程序中都只需要一次。之前没理解以为是一个Aws::SDKOptions控制一个,init和shutdown了多次,结果程序就崩了嵐。

#include "common/s3_utils.hpp"

#include "awscoreauthAWSCredentialsProvider.h"

#include "awss3modelPutObjectRequest.h"
#include "awss3modelGetObjectRequest.h"
#include 

#undef GetObject
#undef GetMessage

namespace s3_utils {

    static void initAwsAPI(bool shutdown) {
        static bool isInited = false;
        static Aws::SDKOptions options;
        if (not isInited && not shutdown) {
            Aws::InitAPI(options);
            isInited = true;
        } else if (isInited && shutdown) {
            Aws::ShutdownAPI(options);
            isInited = false;
        }
    }

    MinioClient::MinioClient(const std::string &endpoint, const std::string &accessKey, const std::string &secretKey) {
        initAwsAPI();
        Aws::Client::ClientConfiguration cfg;
        cfg.endpointOverride = endpoint;  // S3服务器地址和端口
        INFO("endpoint %s access_key %s secret_key %s", endpoint.c_str(), accessKey.c_str(), secretKey.c_str());
        cfg.scheme = Aws::Http::Scheme::HTTP;
        cfg.verifySSL = false;
        client_ = std::make_shared(Aws::Auth::AWSCredentials(accessKey, secretKey), cfg,
                                                      Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false);
    }

    bool MinioClient::upload(const cv::Mat &img, const std::vector &vecCompression_params,
                             const std::string &bucketName,
                             const std::string &objectKey) {
        std::vector vec_Img;
        imencode(".jpg", img, vec_Img, vecCompression_params);
        Aws::S3::Model::PutObjectRequest putObjectRequest;
        putObjectRequest.WithBucket(bucketName).WithKey(objectKey);
        auto img_stream = Aws::MakeShared("PutObjectInputStream");
        img_stream->write((char *) vec_Img.data(), (long long) vec_Img.size());
        putObjectRequest.SetBody(img_stream);
        auto putObjectResult = client_->PutObject(putObjectRequest);
        if (not putObjectResult.IsSuccess()) {
            INFO(putObjectResult.GetError().GetMessage().c_str());
            return false;
        }
        return true;
    }

    bool MinioClient::upload(const std::string &contents, const std::string &bucketName, const std::string &objectKey) {
        Aws::S3::Model::PutObjectRequest putObjectRequest;
        putObjectRequest.WithBucket(bucketName).WithKey(objectKey);
        auto contents_stream = Aws::MakeShared("PutObjectInputStream", contents);
        putObjectRequest.SetBody(contents_stream);
        auto putObjectResult = client_->PutObject(putObjectRequest);
        if (not putObjectResult.IsSuccess()) {
            INFO(putObjectResult.GetError().GetMessage().c_str());
            return false;
        }
        return true;
    }

    std::string MinioClient::download(const std::string &bucketName, const std::string &objectKey) {
        INFO("bucket: %s dir: %s", bucketName.c_str(), objectKey.c_str());
        Aws::S3::Model::GetObjectRequest object_request;
        object_request.WithBucket(bucketName).WithKey(objectKey);
        auto get_object_outcome = client_->GetObject(object_request);
        if (get_object_outcome.IsSuccess()) {
            std::ostringstream tmp;
            tmp << get_object_outcome.GetResult().GetBody().rdbuf();
            return tmp.str();
        } else {
            INFO(get_object_outcome.GetError().GetMessage().c_str());
            return "";
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/748835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号