Home > other >  How to access JSON Blob contents in python in Azure function App
How to access JSON Blob contents in python in Azure function App

Time:06-17

I am using HTTP trigger in Azure function app.I want to do some data transformation on JSON data which is stored in Blob storage. I am no able to extract JSON file data in function.

Can anyone tell me how to access JSON file in function? End to end flow.

Thanks in advance.

CodePudding user response:

After reproducing from our end, You can access a file in blob storage by passing the filename in the HTTP API call and making changes in the function.json file. Below is the code that worked for me.

init.py

import logging

import azure.functions as func

def main(req: func.HttpRequest, inputblob: func.InputStream):
    input_file = inputblob.read()

    print("JSON Body",input_file)

    return func.HttpResponse(input_file)

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "storageacc_STORAGE": "<Connection_String>"
  }
}

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "type": "blob",
      "direction":"in",
      "name": "inputblob",
      "path": "upload/{filename}",
      "connection": "storageacc_STORAGE"
    }
  ]
}

RESULT:

enter image description here

CodePudding user response:

When we are reading blob from blob storage its type is Stack in python so that I am not able to perform any data transformation on data.

For eg : As you given example if I want to search abc value or any other operations on data.

I am not able to do any data transformations.

It will be great if you guide me.

Thank you in advance.

  • Related