当前位置: 首页 > 工具软件 > s3-signer > 使用案例 >

C# 调用 aws-s3-api 上传下载数据到minio上

邢弘业
2023-12-01

C++ 封装动态库

#pragma once
#include "TDGeoAnalysis/TDPreDefine.h"
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
using namespace Aws::S3;
using namespace Aws::S3::Model;
class TDANALYSIS_EXPORT TDAWSOSSTools {
public:
	TDAWSOSSTools();
	~TDAWSOSSTools();
public:
	bool uploadfile(char * BucketName, char * objectKey, char * pathkey);
	bool downloadfile(char * BucketName, char * objectKey, char * pathkey);
private:
	S3Client * m_client;
	Aws::SDKOptions m_options;
};
extern "C" __declspec(dllexport) bool __stdcall uploadfile(char * BucketName, char * objectKey, char * pathkey);
extern "C" __declspec(dllexport) bool __stdcall downloadfile(char * BucketName, char * objectKey, char * pathkey);

#include "S3OSSApi.h"
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <iostream>
#include <fstream>
TDAWSOSSTools::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::~TDAWSOSSTools()
{
	if (m_client != nullptr)
	{
		delete m_client;
		m_client = NULL;
	}
	Aws::ShutdownAPI(m_options);
}
bool TDAWSOSSTools::uploadfile(char * BucketName, char * objectKey, char * pathkey)
{
	PutObjectRequest putObjectRequest;
	putObjectRequest.WithBucket(BucketName).WithKey(objectKey);
	auto input_data = Aws::MakeShared<Aws::FStream>("PutObjectInputStream",
		pathkey, 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(char * BucketName, char * objectKey, char * pathkey)
{
	Aws::S3::Model::GetObjectRequest object_request;
	object_request.WithBucket(BucketName).WithKey(objectKey);
	auto get_object_outcome = m_client->GetObject(object_request);
	if (get_object_outcome.IsSuccess())
	{


		Aws::OFStream local_file;
		local_file.open(pathkey, 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;
	}
}
bool uploadfile(char * BucketName, char * objectKey, char * pathkey)
{
	return TDAWSOSSTools().uploadfile(BucketName, objectKey, pathkey);
}
bool downloadfile(char * BucketName, char * objectKey, char * pathkey)
{
	return TDAWSOSSTools().downloadfile(BucketName, objectKey, pathkey);
}

C# 工具类封装

  using Amazon;
    using Amazon.S3;
    using Amazon.S3.Model;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TDPreprocessTool.Common.OSS
    {
        class OSSAmazonTools
        {
            [DllImport("S3OssApiDll.dll")]
            private static extern bool downloadfile(string BucketName, string objectKey, string pathkey);
            public static bool mydownloadfile(string BucketName, string objectKey, string pathkey)
            {
                return downloadfile(BucketName, objectKey, pathkey);
            }
            [DllImport("S3OssApiDll.dll")]
            private static extern bool uploadfile(string BucketName, string objectKey, string pathkey);
            public static bool myuploadfile(string BucketName, string objectKey, string pathkey)
            {
                return uploadfile(BucketName, objectKey, pathkey);
            }
        }
    }
 类似资料: