#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;
}
}