A E
A E

Reputation: 1454

NodeJs Microsoft Azure Storage SDK Download File to Stream

I just started working with the Microsoft Azure Storage SDK for NodeJS (https://github.com/Azure/azure-storage-node) and already successfully uploaded my first pdf files to the cloud storage.

However, now I started looking at the documentation, in order to download my files as a node_buffer (so I dont have to use fs.createWriteStream), however the documentation is not giving any examples of how this works. The only thing they are writing is "There are also several ways to download files. For example, getFileToStream downloads the file to a stream:", but then they only show one example, which is using the fs.createWriteStream, which I dont want to use.

I was also not able to find anything on Google that really helped me, so I was wondering if anybody has experience with doing this and could share a code sample with me?

Upvotes: 0

Views: 754

Answers (1)

ufxmeng
ufxmeng

Reputation: 2600

The getFileToStream function need a writable stream as param. If you want all the data wrote to a Buffer instead of a file, you just need to create a custom writable stream.

const { Writable } = require('stream');

let bufferArray = [];
const myWriteStream = new Writable({
    write(chunk, encoding, callback) {
        bufferArray.push(...chunk)
        callback();
    }
});

myWriteStream.on('finish', function () {
    // all the data is stored inside this dataBuffer
    let dataBuffer = Buffer.from(bufferArray);
 })

then pass myWriteStream to getFileToStream function

fileService.getFileToStream('taskshare', 'taskdirectory', 'taskfile', myWriteStream, function(error, result, response) {
  if (!error) {
    // file retrieved
  }
});

Upvotes: 1

Related Questions