Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding input from Azure blob in function app using python

I have a csv file in azure blob storage that I would like to open and get certain data based on user input. I have two problems:

1) Empty AzureWebJobsStorage value in local.settings.json

I tried to input binding using AzureWebJobsStorage but the value seems empty. I'm not sure how I can what value I should edit in local.settings.json. Hence, getting this error:

Executed 'Functions.HttpExample' (Failed, Id=3a9309d6-7019-4de6-a8d2-43c89921b0f6) [18-Feb-20 9:40:36 AM] System.Private.CoreLib: Exception while executing function: Functions.HttpExample. Microsoft.Azure.WebJobs.Host: Value cannot be null. [18-Feb-20 9:40:36 AM] Parameter name: connectionString.

I updated the value with connection string of my storage account. It still did not work.

function.json :

{
  "type": "blob",
  "direction": "in",
  "name": "container_name",
  "path": "blob_name/file_name.csv",
  "connection": "AzureWebJobsStorage"
}

Am I using the correct variable names above? As I also received another error :

The specified blob does not exist.

But the blob exist in the storage.

2) How do I display all data inside csv from this (assuming the above issue has resolved)

This is my current code in __init__.py :

def main(req: func.HttpRequest, inputblob: func.InputStream) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    section = req.params.get('section')
    bound = req.params.get('bound')
    km_location = req.params.get('km_location')
    location = req.params.get('location')
    if not section:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            section = req_body.get('section')

    if section and bound and km_location:
        #pass the following user input to the following function and use them to retrieve remaining info
        #from csv (stored in blob) to user.
        result(section, km_location, bound, location).getResult() 

        return func.HttpResponse(f"Hello {section}, {bound}!")
like image 520
cna Avatar asked Apr 15 '26 17:04

cna


1 Answers

About the definition about property in function.json you could refer to this doc: configuration.

name is the variable that represents the blob in function code. It should be same in the function code, and could be any variable you want.

The below is my test function.json, you could also refer to the sample in the link I provide.

{
      "name": "inputBlob",
      "type": "blob",
      "path": "test/test.csv",
      "connection": "AzureWebJobsStorage",
      "direction": "in"
    }

And about how to read csv file, you could refer to my function code.

import logging
import azure.functions as func
import csv
import codecs


def main(req: func.HttpRequest,inputBlob: func.InputStream) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    reader=csv.reader(codecs.iterdecode(inputBlob,'utf-8'))
    for line in reader:
        logging.info(line)

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

Here is my test result, hope this could help you, if you still have other problem please feel free to let me know.

enter image description here

like image 160
George Chen Avatar answered Apr 17 '26 08:04

George Chen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!