aws s3 sdk c++使用指南、适配阿里云oss和aws

通过介绍PutObject这个使用流程来介绍使用:大家好好看即可

1、sdk for aws的用法如下:

cpp 复制代码
	Aws::SDKOptions options;

	Aws::InitAPI(options);

	{
		const Aws::Auth::AWSCredentials credential("dadsadas111", "sadadsaddasdas");
		Aws::Client::ClientConfiguration clientConfig;
		clientConfig.endpointOverride = "https://s3.ap-southeast-2.amazonaws.com"; // da江区 endpoint
		clientConfig.region = "ap-southeast-2";                                  // dsadas区 region id

		Aws::S3::S3Client s3Client(credential, nullptr, clientConfig);

		Aws::S3::Model::PutObjectRequest putObjectRequest;
		putObjectRequest.SetBucket("asddas");
		putObjectRequest.SetKey("asdas.log");

		std::shared_ptr<Aws::IOStream> requestBody = Aws::MakeShared<Aws::FStream>("PutObjectAllocationTag", "D:/dasdas.log", std::ios_base::in | std::ios_base::binary);
		if (!*requestBody)
		{
			Aws::ShutdownAPI(options);
		}
		putObjectRequest.SetBody(requestBody);
		Aws::S3::Model::PutObjectOutcome putObjectOutcome = s3Client.PutObject(putObjectRequest);
		if (!putObjectOutcome.IsSuccess())
		{
			std::cerr << "Error: PutObject: " << putObjectOutcome.GetError().GetMessage() << std::endl; // 上传错误,输出错误信息
		}
		else
		{
			std::cout << "ETag: " << putObjectOutcome.GetResult().GetETag() << std::endl; // 上传成功,打印 ETag
		}
	}
	Aws::ShutdownAPI(options);

2、sdk for alioss的用法如下:

复制代码
Aws::SDKOptions options;

	Aws::InitAPI(options);

	{
		const Aws::Auth::AWSCredentials credential("LTAI5sdasdaq", "Fxg7H2XFasdasH21dsasdA0H");
		Aws::Client::ClientConfiguration clientConfig;
		clientConfig.endpointOverride = "https://oss-cn-beijing.aliyuncs.com"; // 华东-浙江区 endpoint
		clientConfig.region = "oss-cn-beijing";                                  // 华东-浙江区 region id
		auto provider = Aws::MakeShared<Aws::S3::S3EndpointProvider>(Aws::S3::S3Client::GetAllocationTag());
		provider->AccessClientContextParameters().SetForcePathStyle(false);
		// Aws::S3::S3Client s3Client(credential, provider,clientConfig);
		Aws::S3::S3Client s3Client(credential,provider ,clientConfig);
		
		Aws::S3::Model::PutObjectRequest putObjectRequest;
		putObjectRequest.SetBucket("buadsdasbu12");
		putObjectRequest.SetKey("badsdsa1253.log");
		std::shared_ptr<Aws::IOStream> requestBody = Aws::MakeShared<Aws::FStream>("PutObjectAllocationTag", "D:/tessdadast.log", std::ios_base::in | std::ios_base::binary);
		if (!*requestBody)
		{
			Aws::ShutdownAPI(options);
			return;
		}
		putObjectRequest.SetBody(requestBody);
		Aws::S3::Model::PutObjectOutcome putObjectOutcome = s3Client.PutObject(putObjectRequest);
		if (!putObjectOutcome.IsSuccess())
		{
			std::cerr << "Error: PutObject: " << putObjectOutcome.GetError().GetMessage() << std::endl; // 上传错误,输出错误信息
		}
		else
		{
			std::cout << "ETag: " << putObjectOutcome.GetResult().GetETag() << std::endl; // 上传成功,打印 ETag
		}
	}
	Aws::ShutdownAPI(options);

前提:需要申请桶信息等

相关推荐
沐怡旸14 小时前
【穿越Effective C++】条款02:尽量以const, enum, inline替换#define
c++·面试
给大佬递杯卡布奇诺15 小时前
FFmpeg 基本API avcodec_alloc_context3函数内部调用流程分析
c++·ffmpeg·音视频
QT 小鲜肉15 小时前
【个人成长笔记】Qt 中 SkipEmptyParts 编译错误解决方案及版本兼容性指南
数据库·c++·笔记·qt·学习·学习方法
看到我,请让我去学习15 小时前
Qt 控件 QSS 样式大全(通用属性篇)
开发语言·c++·qt
筱砚.15 小时前
【STL——vector容器】
开发语言·c++
相偎16 小时前
用观察者模式通知UI刷新数据
c++
CoderCodingNo16 小时前
【GESP】C++四级真题 luogu-B4040 [GESP202409 四级] 黑白方块
开发语言·c++
小欣加油17 小时前
leetcode 143 重排链表
数据结构·c++·算法·leetcode·链表
给大佬递杯卡布奇诺17 小时前
FFmpeg 基本API avio_open函数内部调用流程分析
c++·ffmpeg·音视频
爱吃生蚝的于勒17 小时前
【Linux】深入理解进程(一)
java·linux·运维·服务器·数据结构·c++·蓝桥杯