Search code examples
node.jsazureazure-blob-storageazure-node-sdk

Read content from Azure blob storage in node API


I am new to azure and working on the storage account for one my application.Basically I have json files stored in azure blob storage.

I want to read the data from these files in Node JS application and do some filtering on the data, which is eventually secured REST end point to view data in the UI/Client as HTTP response.

I have gone through the docs about different operations on the blob storage which is exposed as NODE SDK, we can see find them in below link,

https://github.com/Azure/azure-storage-node

But the question I have is "How to read the json files". I see one method getBlobToStream. Is this going to give me json content in the callback, so that I can do further processing on the data and send as response to clients who requested.

Please some one explain how to do this in better way or is this the only option we have.

Thanks for the help.


Solution

  • To use getBlobToStream, you have to define a writable stream.

    So I recommend you to use getBlobToText to avoid trouble. If no error occurs, this method will get blob content into text in callback. You can then parse it to a JSON string. A simple example is as below.

    blobService.getBlobToText(container, blobname, function(error, text){
        if(error){
            console.error(error);
            res.status(500).send('Fail to download blob');
        } else {
           var data = JSON.parse(text);
           res.status(200).send('Filtered Data you want to send back');
        }
    });