问题描述
我可以检查内部缓冲区以查看是否存在我的文本数据吗?我在正确使用node.js的Stream.read()吗?
I can I check the internal buffer to see if my text data is present? Am I using node.js' Stream.read() correctly?
我有一个文本文件作为blob存储在azure存储中.当我下载Blob时,我得到了可读的流以及有关Blob的信息.返回数据的contentLength为11,这是正确的.
I have a text file as a blob stored on azure-storage. When I download the blob I get readable stream as well as info about the blob. The return data has a contentLength of 11 which is correct.
我看不懂蒸汽.它始终返回null. node.js文档说,
I am unable to read the steam. It always returns null. The node.js docs say,
根据Node.js,没有可用数据.
According to Node.js there is no data available.
async function downloadData(){
const textfile = "name.txt"
const containerURL = ContainerURL.fromServiceURL(serviceURL, "batches")
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, textfile );
let baseLineImage = await blockBlobURL.download(aborter, 0)
console.log(baseLineImage.readableStreamBody.read())
return
}
方法blobBlobURL.download
下载数据.更特定于Azure it,
The method blobBlobURL.download
downloads the data. More specific to Azure it,
在Node.js中,数据以Readable流readStreamStreamBody返回在浏览器中,数据以promise blobBody的形式返回
In Node.js, data returns in a Readable stream readableStreamBodyIn browsers, data returns in a promise blobBody
推荐答案
根据您的代码,我看到您正在使用适用于JavaScript的Azure存储SDK V10 .
According to your code, I see you were using Azure Storage SDK V10 for JavaScript.
在此软件包 @azure/storage-blob
的npm页面中,是示例代码中名为streamToString
的异步函数,可以帮助您从可读流中读取内容,如下所示.
In the npm page of this package @azure/storage-blob
, there is an async function named streamToString
in the sample code which can help you to read the content from readable stream, as below.
// A helper method used to read a Node.js readable stream into string
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", data => {
chunks.push(data.toString());
});
readableStream.on("end", () => {
resolve(chunks.join(""));
});
readableStream.on("error", reject);
});
}
然后,您的代码将如下所示编写.
Then, your code will be writen like below.
async function downloadData(){
const textfile = "name.txt"
const containerURL = ContainerURL.fromServiceURL(serviceURL, "batches");
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, textfile );
let baseLineImage = await blockBlobURL.download(aborter, 0);
let content = await streamToString(baseLineImage.readableStreamBody);
console.log(content)
return content
}
希望有帮助.
这篇关于无法从下载的Blob中读取readyStreamBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!