将uint8_t *缓冲区上传到AWS S3,而无需通过文件系统 [英] Upload uint8_t* buffer to AWS S3 without going via filesystem
问题描述
免责声明: 我不是c ++程序员,请从我自己身上救出来.
Disclaimers: I am not a c++ programmer, please save me from myself.
我正在尝试使用AWS开发工具包在c ++中创建一个PutObjectRequest.
I'm trying to create a PutObjectRequest in c++ using the AWS SDK.
我有一个'uint8_t *'(在Java领域,从这里开始,我称其为byte [],我相信在c + +上这是一个缓冲区),我需要将其放入Aws :: IOStream不知何故.
I have a 'uint8_t*' (in Java-land from whence I hail we call this a byte[], I believe on planet c++ this is a buffer), and I need to get it into an Aws::IOStream somehow.
所有示例都显示了直接来自文件系统的数据.
All of the examples show the data coming directly from the filesystem.
我看过几个类似(但不是真的)的问题,它们的答案指向另一个称为Boost的第三方库,但是确定这是一个常见用例吗?为什么我需要第三方库来做一些可以使用AWS开发工具包完成的事情?:
I've seen a couple of similar-ish (but not really) questions with answers that point to another third party library called Boost, but surely this is a common usecase? Why would I need a third party library to do something that should just be possible using the AWS SDK?:
我有数据,我想将其放在S3上.不,它不在文件系统中,是的,我在内存中创建了它."
uint8_t* buf; //<-- How do I get this...
...
Aws::S3::Model::PutObjectRequest object_request;
object_request.WithBucket(output_bucket).WithKey(key_name);
object_request.SetBody(data); //<-- ...into here
我真的很感谢这里的任何帮助或指点(无双关语).
I really appreciate any help or pointers (no pun intended) here.
更新
我已经尝试了评论中的所有内容,并且:
Update
I've tried everything in the comments, and this:
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>("PutObjectInputStream");
*objectStream << data;
objectStream->flush();
object_request.SetBody(objectStream);
和这个:
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>("PutObjectInputStream");
std::istringstream is((char*) data);
*objectStream << is.rdbuf();
objectStream->flush();
object_request.SetBody(objectStream);
可以编译,但每个只能上传2个字节的数据.
which compile, but each only uploads 2 bytes of data.
我尝试过的其他无法编译的内容是:
Other thing I've tried that don't compile are:
auto input_data = Aws::MakeShared<Aws::IOStream>("PutObjectInputStream", std::istringstream((char*) data), std::ios_base::in | std::ios_base::binary);
object_request.SetBody(input_data);
和
object_request.SetBody(std::make_shared<std::istringstream>( std::istringstream( (char*) spn ) ));
这些将在S3上创建对象,但具有0个字节:
and these ones creates the object on S3, but with 0 bytes:
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>("PutObjectInputStream");
objectStream->rdbuf()->pubsetbuf(static_cast<char*>(reinterpret_cast<char*>(data)), length);
objectStream->rdbuf()->pubseekpos(length);
objectStream->seekg(0);
object_request.SetBody(objectStream);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>("PutObjectInputStream");
objectStream->rdbuf()->pubsetbuf(reinterpret_cast<char*>(data), length);
objectStream->rdbuf()->pubseekpos(length);
objectStream->seekg(0);
object_request.SetBody(objectStream);
推荐答案
在小时被黑客入侵之后,答案是:
And after hours of hacking, here is the answer:
Aws::S3::Model::PutObjectRequest object_request;
object_request.WithBucket(output_bucket).WithKey(key_name);
auto data = Aws::MakeShared<Aws::StringStream>("PutObjectInputStream", std::stringstream::in | std::stringstream::out | std::stringstream::binary);
data->write(reinterpret_cast<char*>(buffer), length);
object_request.SetBody(data);
感谢 Ben Voigt 指出,当您make_shared
(或本例中的MakeShared
)时, ),那么您实际上并没有将数据传递给它.您只是告诉它要共享什么T
.
Thanks to Ben Voigt for pointing out that when you make_shared
(or MakeShared
in my case), you're not actually passing it the data at that point. You're just telling it what T
you're making shared.
也由nbubis回答了以下问题: const char *到std :: basic_iostream
Also helped by nbubis' answer to this question: const char * to std::basic_iostream
这篇关于将uint8_t *缓冲区上传到AWS S3,而无需通过文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!