微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node.js – 如何找出CreateBlockBlobFromStream方法的流长度

我正在尝试使用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) => {});

解决方法

而不是createBlockBlobFromStream尝试使用 createWriteStreamToBlockBlob.

以下代码示例将字母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'));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐