前言
最近在做项目流程优化,准备将之前的java对文件的操作转换到c++端,因此做了基于c++的minio操作的测试demo。期间的各种踩坑与问题,花了一天时间总算是成功了,当然还有一些小问题,等待后续其他大拿解决。
项目环境
vs2019个人版
minio操作的代码本来是计划自己下载源码编译的,但觉得它麻烦,就在csdn上下载了被人编译好的,s3 vs2019 debug(此处第一个坑),我自己刚开始的开发环境直接就是release版本,也是最近一直是release版本,所以配置完环境变量就没有注意,直接开始跑代码了,各种问题也没有仔细查,折腾了几个小时,才发现,这个坑我在最后给大家展示。
minio的编译好的链接是:链接: AWS S3 对象云存储_SDK msvc_x64下使用_vs2019编译 debug库。
测试代码
需要准备的头文件和宏定义等(请严格按照此复制,不要轻易重新):
#define USE_IMPORT_EXPORT
#include"iostream"#include "aws\s3\S3Client.h"
#include "aws\core\Aws.h"
#include "aws\core\auth\AWSCredentialsProvider.h"
#include <iostream>
#include <fstream>
using namespace Aws::S3;
using namespace Aws::S3::Model;
using namespace std;#include "aws\s3\model\PutObjectRequest.h"
#include "aws\s3\model\GetObjectRequest.h"
#include <aws/s3/model/ListObjectsRequest.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.111.111.111:901"; // S3服务器地址和端口cfg.scheme = Aws::Http::Scheme::HTTP;cfg.verifySSL = false;Aws::Auth::AWSCredentials cred("2222", "23666");m_client = new S3Client(cred, cfg,Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Always, false);PutObjectRequest putObjectRequest;//BucketName桶的名称,如cplus;objectKeyoss上文件的名称,如11.txt,此处要设置好文件的后缀类型,因此其是唯一的,不然不同文件不就重名了;pathkey,待上传的数据的本地路径,即文件本身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 downloadfile1(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.111.111.111:901"; // S3服务器地址和端口cfg.scheme = Aws::Http::Scheme::HTTP;cfg.verifySSL = false;Aws::Auth::AWSCredentials cred("123", "2222");m_client = new S3Client(cred, cfg,Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Always, 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;
}
关键的调用方法:
int main()
{downloadfile1("cplus","logo.png", "C:\\Users\\OUR\\Desktop\\新建文件夹\\1.png"); uploadfile("cplus","", "D:/11.txt");
}
其中的关键点
(1)上传时
函数定义:bool uploadfile(std::string BucketName, std::string objectKey, std::string pathkey)
函数调用:uploadfile(“cplus”,“csdn.txt”, “D:/11.txt”);
参数说明分别是:
name | Value |
---|---|
BucketName | minio云端的桶的名称,如下截图的cpus、dicom等,此处类似于的是文件夹,注:桶需要提前设置好,在minio上新建就行 |
objectKey | 传到云端的文件的名称和后缀(此处需要唯一性),如logo.png、csdn.txt名称,注:key必须是带有后缀的,否则上传后不会自动设置文件的后缀 |
pathkey | 待上传的本地文件的路径;本地准备上传的路径和地址;注:本地文件存在即可 |
MinIO上“桶”列表 ↑
cplsu桶中的文件情况↑
问题:下载数报错:
*Fatal error condition occurred in D:\SDK\aws-sdk-cpp\crt\aws-crt-cpp\crt\aws-c-io\source\event_loop.c:75: aws_thread_launch(&cleanup_thread, s_event_loop_destroy_async_thread_fn, el_group, &thread_options) == AWS_OP_SUCCESS
Exiting Application
at 0x7FFECBE41E6A: aws_byte_buf_append_null_terminator
at 0x7FFECBE41E6A: aws_byte_buf_append_null_terminator
at 0x7FFECBE41E6A: aws_byte_buf_append_null_terminator
at 0x7FFEBDAD1735: aws_retry_strategy_release
at 0x7FFECBE41E6A: aws_byte_buf_append_null_terminator
at 0x7FFEBDAD1735: aws_retry_strategy_release
at 0x7FFEBDAD1735: aws_retry_strategy_release
*
(2)下载时
std::string BucketName, std::string objectKey, std::string pathkey
“cplus”,“logo.png”, "C:\Users\OUR\Desktop\新建文件夹\1.png
分别是:
name | Value |
---|---|
BucketName | minio云端的桶的名称,如下截图的cpus、dicom等,此处类似于的是文件夹,注:桶需要提前设置好,在minio上新建就行 |
pathkey | Minio上的数据文件注:需要文件名和后缀 |
objectKey | 现在后文件的路径,如:C:\Users\OUR\Desktop\新建文件夹\1.png,注:不是设置下载文件接收的文件夹,而是文件,接收时需要自己设置文件后缀类型,否则无法下载 |
MinIO上“桶”列表 ↑
cplsu桶中的文件情况↑
下载到的文件↑
问题:下载数报错:
PutObject error: SDK failed to sign the request
Hello World!
Fatal error condition occurred in D:\SDK\aws-sdk-cpp\crt\aws-crt-cpp\crt\aws-c-io\source\event_loop.c:75: aws_thread_launch(&cleanup_thread, s_event_loop_destroy_async_thread_fn, el_group, &thread_options) == AWS_OP_SUCCESS
Exiting Application
at 0x7FFEE6CF1E6A: aws_byte_buf_append_null_terminator
at 0x7FFEE6CF1E6A: aws_byte_buf_append_null_terminator
at 0x7FFEE6CF1E6A: aws_byte_buf_append_null_terminator
at 0x7FFEC9AD1735: aws_retry_strategy_release
at 0x7FFEE6CF1E6A: aws_byte_buf_append_null_terminator
at 0x7FFEC9AD1735: aws_retry_strategy_release
at 0x7FFEC9AD1735: aws_retry_strategy_release
待后来人解决吧
结论
此代码可以实现对minio的文件上传与下载,是最简单的代码。使用的是vs2019、debug 、x64环境、时使用的是现成被人编译好的结果。
改进意见
(1)对minio的配置最好使用init()的方式进行统一配置,调用时在桶下建立自己的文件夹
(2)后续的报错还没有解决,计提原因未知,我就不查了
(3)现在只有debug,最终的生产环境一般都是release,所有孩子要去官网下载源码编译,才是成就之举。
(4)等我有时间了,项目有真正需求了,再专门写个帖子做minio的c++编译。
敬请期待哦!
参考资料:https://www.jianshu.com/p/74f13cd08cc7