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) => {});
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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With