Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download blob storage and return Json object

I am trying to download a .json blob that I have stored in a container in the Azure Storage using Newtonsoft.Json to write it to an object.

I am doing this by calling:

(CloudBlockBlob) blob.DownloadToStream(stream);

However, instead of writing the stream to a file in the local app directory, I want to return the json object doing Json(result)

This is what I have tried:

using (var stream = new MemoryStream())
{
    blob.DownloadToStream(stream);

    var serializer = new JsonSerializer();

    using (var sr = new StreamReader(stream))
    {
        using (var jsonTextReader = new JsonTextReader(sr))
        {
            result = serializer.Deserialize(jsonTextReader);
        }
    }
}

At the end my jsonTextReader variable is empty and the object null

What can I do to accomplish this?

Thank you

like image 312
Luis Lavieri Avatar asked Jan 19 '16 19:01

Luis Lavieri


People also ask

Can JSON be stored as BLOB?

You can store JSON data in Oracle Database using columns whose data types are VARCHAR2 , CLOB , or BLOB .

Can we query data from BLOB storage?

The possibility to query information on blob storage and other sources easily with a Serverless Pool has many uses. One of them is ad-hoc analysis queries, and the UI feature to view the result in many different ways contributes even more to this.

How do I download an azure blob storage file by URL?

In order to download an Azure BLOB Storage item by its URL, you need to instantiate a CloudBlockBlob yourself using the item's URL: var blob = new CloudBlockBlob(new Uri(pdfFileUrl), cloudStorageAccount. Credentials); This blob can then be downloaded with the code you originally posted.


1 Answers

Please reset the stream's position to 0 after reading the blob into the stream. So your code would be:

        using (var stream = new MemoryStream())
        {
            blob.DownloadToStream(stream);
            stream.Position = 0;//resetting stream's position to 0
            var serializer = new JsonSerializer();

            using (var sr = new StreamReader(stream))
            {
                using (var jsonTextReader = new JsonTextReader(sr))
                {
                    var result = serializer.Deserialize(jsonTextReader);
                }
            }
        }
like image 73
Gaurav Mantri Avatar answered Sep 28 '22 00:09

Gaurav Mantri