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);

前提:需要申请桶信息等

相关推荐
fpcc13 小时前
跟我学C++中级篇——std::is_invocable的分析应
c++
Code Slacker15 小时前
LeetCode Hot100 —— 滑动窗口(面试纯背版)(四)
数据结构·c++·算法·leetcode
阿里云云原生15 小时前
深度解析云监控 2.0 日志审计:统一采集、实体建模与告警溯源能力
阿里云·云原生·云监控·可观测
SHERlocked9316 小时前
摄像头 RTSP 流视频多路实时监控解决方案实践
c++·后端·音视频开发
tang&17 小时前
哈希碰撞攻防战:C++闭散列与开散列实现全解析
c++·哈希算法
眠りたいです17 小时前
现代C++:C++11并发支持库
开发语言·c++·多线程·c++11·c++并发支持库
小灰灰搞电子17 小时前
Rust可以取代C++么?
开发语言·c++·rust
微笑倾城18 小时前
Windows平台下CMake工程中使用protobuf
c++·cmake
AA陈超18 小时前
枚举类 `ETriggerEvent`
开发语言·c++·笔记·学习·ue5