5

I'm trying to upload a streams with azure-storage, but the method CreateBlockBlobFromStream needs the stream length. I not sure where to geth the length.

My code

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) => {});
user49126
  • 1,825
  • 7
  • 30
  • 53

2 Answers2

10

Instead of createBlockBlobFromStream try using createWriteStreamToBlockBlob.

The following code example pushes the letters a-z into 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'));

If you want to read a file with a readable stream, the code will look like this:

var fs = require("fs");
// ...
var stream = fs.createReadStream('test.xml');
stream.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer', 'test.xml'));
Aaron Chen
  • 9,835
  • 1
  • 16
  • 28
0

Please see another question related to this. (get a stream's content-length)

Or you can use any npms to get the length of the file stream. (Ex. https://github.com/joepie91/node-stream-length)

Community
  • 1
  • 1
Rajan Panneer Selvam
  • 1,279
  • 10
  • 24