首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure C++库:“无效的streambuf对象”

Azure C++库:“无效的streambuf对象”
EN

Stack Overflow用户
提问于 2018-08-01 04:04:28
回答 2查看 339关注 0票数 1

我正在尝试使用C++ Azure客户端库下载一个可能很大的Azure block blob。它不工作,因为我不知道如何初始化一个具有缓冲区大小的concurrency::streams::streambuf对象。我的代码如下所示:

代码语言:javascript
复制
 // Assume blockBlob has been created correctly.
 concurrency::streams::istream blobStream = blockBlob.open_read();
 // I don't know how to initialize this streambuf:         
 concurrency::streams::streambuf<uint8_t> dlStreamBuf;
 size_t nBytesReturned = 0, nBytesToRead = 65536;
 do {
    // This gets the exception "Invalid streambuf object": 
    concurrency::task<size_t> returnedTask = blobStream.read(dlStreamBuf, nBytesToRead);
    nBytesReturned = returnedTask.get();
    bytesSoFar += nBytesReturned;
    // Process the data in dlStreamBuf here...
 } while(nBytesReturned > 0);
 blobStream.close();

请注意,不要将上面的streambuf与标准的C++ streambuf混淆。

有人能建议我如何正确地构造和初始化concurrency::streams::streambuf吗?

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2018-09-01 02:00:06

streambuf似乎是一个模板类。试着这样做:

代码语言:javascript
复制
    concurrency::streams::container_buffer<std::vector<uint8_t>> output_buffer;
    size_t nBytesReturned = 0, nBytesToRead = 65536;
    do {
        // This gets the exception "Invalid streambuf object": 
        concurrency::task<size_t> returnedTask = stream.read(output_buffer, nBytesToRead);
        nBytesReturned = returnedTask.get();
        bytesSoFar += nBytesReturned;
        // Process the data in dlStreamBuf here...
    } while (nBytesReturned > 0);
    stream.close();

示例代码在这里:https://github.com/Azure/azure-storage-cpp/blob/76cb553249ede1e6f05456d936c9a36753cc1597/Microsoft.WindowsAzure.Storage/tests/blob_streams_test.cpp#L192

票数 1
EN

Stack Overflow用户

发布于 2018-08-03 04:32:53

我没有使用过C++的流方法,但是在C++文档中提到了两种下载到文件或流的方法

download_to_stream方法示例:

代码语言:javascript
复制
    // Retrieve storage account from connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the blob client.
azure::storage::cloud_blob_client blob_client = storage_account.create_cloud_blob_client();

// Retrieve a reference to a previously created container.
azure::storage::cloud_blob_container container = blob_client.get_container_reference(U("my-sample-container"));

// Retrieve reference to a blob named "my-blob-1".
azure::storage::cloud_block_blob blockBlob = container.get_block_blob_reference(U("my-blob-1"));

// Save blob contents to a file.
concurrency::streams::container_buffer<std::vector<uint8_t>> buffer;
concurrency::streams::ostream output_stream(buffer);
blockBlob.download_to_stream(output_stream);

std::ofstream outfile("DownloadBlobFile.txt", std::ofstream::binary);
std::vector<unsigned char>& data = buffer.collection();

outfile.write((char *)&data[0], buffer.size());
outfile.close();  

替代,使用download_to_file:

代码语言:javascript
复制
    // Retrieve storage account from connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the blob client.
azure::storage::cloud_blob_client blob_client = storage_account.create_cloud_blob_client();

// Retrieve a reference to a previously created container.
azure::storage::cloud_blob_container container = blob_client.get_container_reference(U("my-sample-container"));

// Retrieve reference to a blob named "my-blob-2".
azure::storage::cloud_block_blob text_blob = container.get_block_blob_reference(U("my-blob-2"));

// Download the contents of a blog as a text string.
utility::string_t text = text_blob.download_text();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51621224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档