赞
踩
MinIO——分布式对象存储服务器。
它是一个是一个高性能的对象存储服务器,用于构建云存储解决方案,出于工作需求用到了这个MinIO来管理文件。
但,我用的是Qt,c++语言,并且使用环境是windows,可MinIO的C++ SDK只能Linux使用,不支持Windows,如果非要自己编译Windows版本的话估计得踩不少坑,放过自己吧。
最后只能折中于使用AWS SDK for C++,好在MinIO是兼容AWS SDK的?
先安装AWS SDK for C++,需要先安装vcpkg:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.bat
等安装完成后,将vcpkg的环境加入系统环境变量,这样才能在cmd窗口随意使用vcpkg命令。
右键“我的电脑” >> 属性 >> 高级系统设置 >> 环境变量 >> 系统变量>> path
双击“path”把vcpkg的环境加入其中。
添加完成后,一路点击“确定”按钮退出界面。
按下win+R输入cmd回车,打开窗口后,输入以下命令:
vcpkg search aws-sdk-cpp
vcpkg install aws-sdk-cpp[s3]:x64-windows
这里可能会因为网络问题下载包失败,失败后重来,总会成功的。
等成功安装后,会在vcpkg中看到这样一个路径:D:\Soft\vcpkg\vcpkg\installed\x64-windows
这里面lib、include、bin文件夹中的内容,就是我们需要的。
随便创建一个qt项目,在pro文件中加入:
INCLUDEPATH += D:/Soft/vcpkg/vcpkg/installed/x64-windows/include
LIBS += -LD:/Soft/vcpkg/vcpkg/installed/x64-windows/lib -laws-cpp-sdk-core -laws-c-auth -laws-c-cal -laws-c-common \
-laws-c-compression -laws-c-event-stream -laws-checksums -laws-c-http -laws-c-io \
-laws-cpp-sdk-dynamodb -laws-cpp-sdk-kinesis -laws-cpp-sdk-s3 -laws-crt-cpp
使用aws-sdk-cpp的接口,连接MinIO服务器,要确保服务器已打开。
完成功能:连接服务器、创建Bucket、列出Buckets的名字、上传文件、下载文件、删除文件、删除Bucket。
#include <QFile> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/GetObjectRequest.h> #include <aws/s3/model/PutObjectRequest.h> #include <aws/s3/model/DeleteObjectRequest.h> #include <aws/s3/model/CreateBucketRequest.h> #include <aws/s3/model/DeleteBucketRequest.h> #include <aws/core/auth/AWSCredentialsProviderChain.h> void DemoQML::initMinIO() { // 连接服务器 Aws::SDKOptions options; options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); Aws::Client::ClientConfiguration cfg; cfg.endpointOverride = "127.0.0.1:9000"; cfg.scheme = Aws::Http::Scheme::HTTP; cfg.verifySSL = false; Aws::Auth::AWSCredentials cred("hualongxunda", "hualongxunda"); Aws::S3::S3Client client(cred, cfg, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Always, false, Aws::S3::US_EAST_1_REGIONAL_ENDPOINT_OPTION::NOT_SET); std::string fileName = "2104616062021073019firstpush_00004893_OK.bmp"; std::string BucketName = "mybucket"; // 创建Bucket Aws::S3::Model::CreateBucketRequest create_bucket_request; create_bucket_request.SetBucket(BucketName); create_bucket_request.SetCreateBucketConfiguration({}); auto create_bucket_outcome = client.CreateBucket(create_bucket_request); if (create_bucket_outcome.IsSuccess()) { qDebug() << "Bucket created successfully."; } else { qDebug() << "Failed to create bucket: " << create_bucket_outcome.GetError().GetMessage().c_str(); Aws::ShutdownAPI(options); return; } // 列出Buckets的名字 auto response = client.ListBuckets(); if (response.IsSuccess()) { auto buckets = response.GetResult().GetBuckets(); for (auto iter = buckets.begin(); iter != buckets.end(); ++iter) { qDebug() << "--" << iter->GetName().c_str() << iter->GetCreationDate().ToLocalTimeString(Aws::Utils::DateFormat::ISO_8601).c_str(); } } // 上传文件 QFile file(QString::fromStdString(fileName)); file.open(QIODevice::ReadOnly); Aws::S3::Model::PutObjectRequest putObjectRequest; putObjectRequest.WithBucket(BucketName.c_str()).WithKey(fileName.c_str()); std::shared_ptr<Aws::IOStream> input_data = Aws::MakeShared<Aws::StringStream>("PutObjectInputStream"); *input_data << file.readAll().toStdString(); file.close(); putObjectRequest.SetBody(input_data); auto putObjectResult = client.PutObject(putObjectRequest); if (putObjectResult.IsSuccess()) { qDebug() << "upload file success!"; } else { qDebug() << "upload file error: " << putObjectResult.GetError().GetExceptionName().c_str() << " " << putObjectResult.GetError().GetMessage().c_str(); Aws::ShutdownAPI(options); return; } // 下载文件 Aws::S3::Model::GetObjectRequest object_request; object_request.WithBucket(BucketName.c_str()).WithKey(fileName.c_str()); auto get_object_outcome = client.GetObject(object_request); if (get_object_outcome.IsSuccess()) { Aws::IOStream &local_file = get_object_outcome.GetResult().GetBody(); if (!local_file) { qDebug() << "Error opening file"; Aws::ShutdownAPI(options); return; } std::istreambuf_iterator<char> begin(local_file); std::istreambuf_iterator<char> end; std::string buffer(begin, end); // 读取整个文件内容 QFile file_(QString::fromStdString("./test/"+fileName)); if(file_.open(QIODevice::ReadWrite | QIODevice::Truncate)){ file_.write(QByteArray::fromStdString(buffer)); file_.close(); } qDebug() << "download success!"; } else { std::cout << "GetObject error: " << get_object_outcome.GetError().GetExceptionName() << " " << get_object_outcome.GetError().GetMessage() << std::endl; } // 删除文件 Aws::S3::Model::DeleteObjectRequest delete_object_request; delete_object_request.WithBucket(BucketName.c_str()).WithKey(fileName.c_str()); auto delete_object_outcome = client.DeleteObject(delete_object_request); if (delete_object_outcome.IsSuccess()) { qDebug() << "File deleted successfully."; } else { qDebug() << "Failed to delete file: " << delete_object_outcome.GetError().GetMessage().c_str(); } // 删除Bucket,一定要先删除文件,再删除Bucket Aws::S3::Model::DeleteBucketRequest delete_bucket_request; delete_bucket_request.SetBucket(BucketName.c_str()); auto delete_bucket_outcome = client.DeleteBucket(delete_bucket_request); if (delete_bucket_outcome.IsSuccess()) { qDebug() << "Bucket deleted successfully."; } else { qDebug() << "Failed to delete bucket: " << delete_bucket_outcome.GetError().GetMessage().c_str(); } Aws::ShutdownAPI(options); }
编译release版本后,如果运行失败,可以将D:\Soft\vcpkg\vcpkg\installed\x64-windows\bin 目录下有用到的dll文件,复制到qt程序的exe同级目录下。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。