const char *到std :: basic_iostream

时间:2017-01-03 17:01:29

标签: c++ c++11 stream std aws-sdk

我有一个指向const *char缓冲区的指针及其长度,我正在尝试使用接受类型对象的API(在本例中为AWS S3 C++ upload request) :

std::basic_iostream <char, std::char_traits <char>>

是否有一种简单的标准C ++ 11方法将我的缓冲区转换为兼容的流,最好不要实际复制内存?

3 个答案:

答案 0 :(得分:3)

感谢伊戈尔的评论,这似乎有效:

func(const * char buffer, std::size_t buffersize)
{     
    auto sstream = std::make_shared<std::stringstream>();
    sstream->write(buffer, buffersize);
    ...
    uploadRequest.SetBody(sstream);     
    ....

答案 1 :(得分:2)

作为解决方案的一个相当明显的推论,您可以使用这样的代码创建一个空的basic_iostream。此示例创建一个0字节的伪目录S3密钥:

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

答案 2 :(得分:1)

如果您想要复制数据,并假设使用boost是一个选项,则可以使用boost中的basic_bufferstream:

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

然后您的s3客户端可以使用它:

 Aws::S3::Model::PutObjectRequest req;
 req.WithBucket(bucket).WithKey(key);
 req.SetBody(input);
 s3.PutObject(req);