I have a pointer to a const *char
buffer as well as it's length, and am trying to use an API (in this case, the AWS S3 C++ upload request ) that accepts an object of type:
std::basic_iostream <char, std::char_traits <char>>
Is there a simple standard C++11 way to convert my buffer into a compatible stream, preferably without actually copying over the memory?
Thanks to Igor's comment, this seems to work:
func(const * char buffer, std::size_t buffersize)
{
auto sstream = std::make_shared<std::stringstream>();
sstream->write(buffer, buffersize);
...
uploadRequest.SetBody(sstream);
....
As a fairly obvious corollary to your solution, you can create an empty basic_iostream with code like this. This example creates a 0-byte pseudo-directory S3 key:
Aws::S3::Model::PutObjectRequest object_request;
// dirName ends in /.
object_request.WithBucket(bucketName).WithKey(dirName);
// Create an empty input stream to create the 0-byte directory file.
auto empty_sstream = std::make_shared<std::stringstream>();
object_request.SetBody(empty_sstream);
auto put_object_outcome = s3_client->PutObject(object_request);
If you do not want to make a copy of your data, and assuming using boost is an option, you can use basic_bufferstream from boost:
#include <boost/interprocess/streams/bufferstream.hpp>
char* buf = nullptr; // get your buffer
size_t length = 0;
auto input = Aws::MakeShared<boost::interprocess::basic_bufferstream<char>> (
"PutObjectInputStream",
buf,
length);
Then your s3 client can use it:
Aws::S3::Model::PutObjectRequest req;
req.WithBucket(bucket).WithKey(key);
req.SetBody(input);
s3.PutObject(req);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.