2

I have created a BlobTrigger in Azure Function App to read any new file getting inserted or updated in my Azure Blob storage.

Trigger is working properly to identify latest files inserted or updated in my blob container as well as I am able to print the json body of the file.

However when I am trying to store the json object in a variable to transform it, it throws an error.

I would like assign each key of the json to a variable. My json is

   {
    "name":"Saikat",
    "id":"1234"
   }

Below is code when I can print the json and error while trying to store it.

   import logging
   import azure.functions as func
   import json

   def main(myblob: func.InputStream):
       logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
        print("JSON Body",json.load(myblob))

enter image description here

   import logging
   import azure.functions as func
   import json

   def main(myblob: func.InputStream):
       logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
        print("JSON Body",json.load(myblob))
        #Store JSON file
        jsonData= json.load(myblob)
        print("****jsonData*****",jsonData)

enter image description here

1 Answer 1

2

Essentially you're getting this error is because you're reading from the stream twice. After the 1st read, the stream's read position is set at the end of the stream and that's why your 2nd read is failing.

Based on the comments below, since InputStream BytesIO object doesn't contain seek operation, the solution to your problem is to read the stream just once.

Try something like the following:

import logging
import azure.functions as func
import json

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                f"Name: {myblob.name}\n"
                f"Blob Size: {myblob.length} bytes")
    jsonData= json.load(myblob)
    print("JSON Body",jsonData)

    #Store JSON file
    print("****jsonData*****",jsonData)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for explaining the problem clearly. I am able to store the json file now by capturing it in the first instance. Whileusing seek, I am getting this error "Exception: UnsupportedOperation: seek". It seems InputStream BytesIO object doesn't contain seek operation. github.com/Azure/azure-functions-python-worker/issues/… thanks a ton
Thanks for additional information. I basically simulated this by reading from a local file.
Updated my answer based on your comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.