Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the stream length for CreateBlockBlobFromStream method

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) => {});
like image 928
user49126 Avatar asked Dec 22 '16 14:12

user49126


1 Answers

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'));
like image 59
Aaron Chen Avatar answered Nov 20 '22 13:11

Aaron Chen