aws-sdk-s3 C++操作minio进行文件上传和下载

#pragma once
#include "TDPreDefine.h"
#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;
class TDANALYSIS_EXPORT TDAWSOSSTools{
public:
	TDAWSOSSTools()
	{
		Aws::InitAPI(m_options);
		Aws::Client::ClientConfiguration cfg;
		cfg.endpointOverride = "10.1.3.148: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("RPW421T9GSIO4A45Y9ZR", "2owKYy9emSS90Q0pXuyqpX1OxBCyEDYodsiBemcq"), cfg, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Always,false);
	}
	~TDAWSOSSTools()
	{
		if (m_client != nullptr)
		{
			delete m_client;
			m_client = NULL;
		}
		Aws::ShutdownAPI(m_options);
	}
public:
	bool uploadfile(std::string BucketName, std::string objectKey,std::string pathkey);
	bool downloadfile(std::string BucketName, std::string objectKey, std::string pathkey);
private:
	S3Client * m_client = { NULL };
	Aws::SDKOptions m_options;
};



#include "TDAWSOSSTools.h"
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <iostream>
#include <fstream>
 
bool TDAWSOSSTools::uploadfile(std::string BucketName,std::string objectKey, std::string pathkey)
{
	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;
	}
}
bool TDAWSOSSTools::downloadfile(std::string BucketName, std::string objectKey, std::string pathkey)
{
	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;
	}
}
### C++代码示例用于向MINIO对象存储上传文件 为了通过C++程序与MinIO服务器交互并完成文件上传操作,可以利用`libcurl`库来处理HTTP请求以及JSON解析库(如RapidJSON)。下面提供了一个简单的例子说明如何构建一个多部分表单数据POST请求到MinIO服务端。 #### 准备工作 首先安装必要的依赖项: - `libcurl`: 用来发起网络请求。 - `openssl/ssl.h`, `openssl/crypto.h`: 提供加密支持。 - JSON库 (可选): 如果需要解析返回的结果或构造复杂的API调用参数。 #### 安全认证配置 当连接至私有部署的MinIO实例时,通常会涉及到访问密钥(Access Key) 秘密密钥(Secret Key),这些信息应该被安全地保存,并仅限于可信的应用组件内部使用。对于生产环境中敏感凭证的安全管理,请遵循最佳实践指南[^1]。 #### 文件上传流程概述 1. 初始化客户端环境; 2. 设置目标桶名(bucket name)对象键(object key); 3. 构建预签名URL(pre-signed URL); 4. 使用多部分上传协议(multipart upload protocol)分片传输大文件; 5. 处理响应消息; 以下是具体的C++代码片段: ```cpp #include <iostream> #include <string> #include <curl/curl.h> // ...省略其他头文件引入... static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) { ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; } bool UploadFileToMinio(const std::string& endpoint, const std::string& accessKey, const std::string& secretKey, const std::string& bucketName, const std::string& objectKey, const std::string& filePath) { CURLcode res; CURL* curl = curl_easy_init(); if (!curl) { std::cerr << "Failed to initialize libcurl." << std::endl; return false; } struct curl_slist* headers = NULL; // 添加必要的头部字段... headers = curl_slist_append(headers, ("Authorization: Bearer YOUR_PRE_SIGNED_TOKEN").c_str()); std::string readData; FILE* fp = fopen(filePath.c_str(), "rb"); fseek(fp, 0L, SEEK_END); long fileSize = ftell(fp); rewind(fp); char buffer[BUFSIZ]; size_t bytesRead; while ((bytesRead = fread(buffer, 1, sizeof(buffer), fp)) > 0){ readData.append(buffer, bytesRead); } fclose(fp); curl_easy_setopt(curl, CURLOPT_URL, ("https://" + endpoint + "/" + bucketName + "/" + objectKey).c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(readData.size())); curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, readData.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseString); res = curl_easy_perform(curl); bool success = res == CURLE_OK; curl_easy_cleanup(curl); curl_global_cleanup(); return success; } ``` 此函数实现了基本的功能需求,但实际应用中可能还需要考虑更多的细节,比如错误重试机制、进度条显示等功能扩展。此外,在真实场景下应当采用更严谨的方式获取授权令牌而不是硬编码在源码里。
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值