【问题标题】:How to find out the stream length for CreateBlockBlobFromStream method如何找出 CreateBlockBlobFromStream 方法的流长度
【发布时间】:2016-12-22 14:30:04
【问题描述】:

我正在尝试使用 azure-storage 上传流,但方法 CreateBlockBlobFromStream 需要流长度。我不知道在哪里得到长度。

我的代码

const Readable = require('stream').Readable;
const rs = Readable();

rs._read = () => {     
    //here I read a file, loop through the lines and then generate some xml
};

const blobSvc = azure.createBlobService(storageName, key);
blobSvc.createBlockBlobFromStream ('data','test.xml', rs, ???, (err, r, resp) => {});

【问题讨论】:

  • 你有什么更新吗?
  • 今天或明天试试这个。我一直在度假:-)

标签: node.js azure stream


【解决方案1】:

尝试使用createWriteStreamToBlockBlob,而不是createBlockBlobFromStream

以下代码示例将字母 a-z 推送到 myblob.txt

var azure = require('azure-storage');
const Readable = require('stream').Readable;
const rs = Readable();

var c = 97;
rs._read = () => {     
  rs.push(String.fromCharCode(c++));
  if (c > 'z'.charCodeAt(0)) rs.push(null);
};

var accountName = "youraccountname";
var accessKey = "youraccountkey";
var host = "https://yourhost.blob.core.windows.net";
var blobSvc = azure.createBlobService(accountName, accessKey, host);

rs.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer', 'myblob.txt'));

如果你想读取具有可读流的文件,代码将如下所示:

var fs = require("fs");
// ...
var stream = fs.createReadStream('test.xml');
stream.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer', 'test.xml'));

【讨论】:

【解决方案2】:

请查看与此相关的另一个问题。 (get a stream's content-length)

或者您可以使用任何 npms 来获取文件流的长度。 (例如https://github.com/joepie91/node-stream-length

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-08
    • 2017-09-24
    • 2010-10-15
    • 2020-09-19
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2011-03-07
    相关资源
    最近更新 更多