MinIO——分布式對象存儲服務器。
它是一個是一個高性能的對象存儲服務器,用于構建云存儲解決方案,出于工作需求用到了這個MinIO來管理文件。
但,我用的是Qt,c++語言,并且使用環(huán)境是windows,可MinIO的C++ SDK只能Linux使用,不支持Windows,如果非要自己編譯Windows版本的話估計得踩不少坑,放過自己吧。
最后只能折中于使用AWS SDK for C++,好在MinIO是兼容AWS SDK的?
安裝vcpkg
先安裝AWS SDK for C++,需要先安裝vcpkg:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.bat
等安裝完成后,將vcpkg的環(huán)境加入系統(tǒng)環(huán)境變量,這樣才能在cmd窗口隨意使用vcpkg命令。
右鍵“我的電腦” >> 屬性 >> 高級系統(tǒng)設置 >> 環(huán)境變量 >> 系統(tǒng)變量>> path
雙擊“path”把vcpkg的環(huán)境加入其中。添加完成后,一路點擊“確定”按鈕退出界面。
安裝AWS SDK for C++
按下win+R輸入cmd回車,打開窗口后,輸入以下命令:
vcpkg search aws-sdk-cpp
vcpkg install aws-sdk-cpp[s3]:x64-windows
這里可能會因為網(wǎng)絡問題下載包失敗,失敗后重來,總會成功的。
等成功安裝后,會在vcpkg中看到這樣一個路徑:D:\Soft\vcpkg\vcpkg\installed\x64-windows
這里面lib、include、bin文件夾中的內(nèi)容,就是我們需要的。
創(chuàng)建Qt項目
隨便創(chuàng)建一個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
對接MinIO
使用aws-sdk-cpp的接口,連接MinIO服務器,要確保服務器已打開。
完成功能:連接服務器、創(chuàng)建Bucket、列出Buckets的名字、上傳文件、下載文件、刪除文件、刪除Bucket。文章來源:http://www.zghlxwxcb.cn/news/detail-787530.html
#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";
// 創(chuàng)建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); // 讀取整個文件內(nèi)容
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同級目錄下。文章來源地址http://www.zghlxwxcb.cn/news/detail-787530.html
到了這里,關于Windows下Qt使用AWS SDK for C++連接MinIO服務器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!