Azure Function Read & Write to Blobs Storage throwing internal 500 error

Viewed 452

by no means am i an expert when it comes to python but i have tried my best. I have written a short code in python that reads a text file from a blob storage and appends it with some column names and outputs it to target folder. The code executes correctly when i run from VS Code.

When i try to run it via Azure Function i get it throws an error.

Can it be that i have wrong parameters in my function.json binding file?

{ "statusCode": 500, "message": "Internal server error" }

This is my original code:

import logging

import azure.functions as func

from azure.storage.blob import BlobClient,BlobServiceClient

def main():
logging.info(' start Python Main function processed a request.')

#CONNECTION STRING
blob_service_client = BlobServiceClient.from_connection_string(CONN_STR)

# MAP SOURCE FILE
blob_client = blob_service_client.get_blob_client(container="source", blob="source.txt")

#SOURCE CONTENTS
content=  blob_client.download_blob().content_as_text()
    
# WRITE HEADER TO A OUT PUTFILE
output_file_dest = blob_service_client.get_blob_client(container="target", blob="target.csv")

#INITIALIZE OUTPUT               
output_str = ""

#STORE COULMN HEADERS
data= list()
    
data.append(list(["column1", "column2", "column3", "column4"]))

output_str += ('"' + '","'.join(data[0]) + '"\n')

output_file_dest.upload_blob(output_str,overwrite=True)
logging.info(' END OF FILE UPLOAD')

if __name__ == "__main__":
main()

This is my function.json

{
"scriptFile": "__init__.py",
  
"bindings": [
    {
      "type": "blob",
      "direction": "in",
      "name": "blob_client",
      "path": "source/source.txt",
      "connection": "STORAGE_CONN_STR"
    },
    {
      "type": "blob",
      "direction": "out",
      "name": "output_file_dest",
      "path": "target/target.csv",
      "connection": "STORAGE_CONN_STR"
    }
  ]
}

This is my requirement.txt

azure-functions==1.7.2
azure-storage-blob==12.8.1
2 Answers

Try adding your connection string to the Application settings of your Function App. The Application settings can be found under Configuration (under Settings). Click on New application setting, enter a name and specify the value. Then connect to the blob with the following code:

from azure.storage.blob import BlobClient,BlobServiceClient
import os

#CONNECTION STRING
blob_service_client = BlobServiceClient.from_connection_string(os.getenv('the name of your connection string in the application settings')

There are two possible ways to get 500 Internal Server Error when you Test/Run the code from Azure Portal:

  1. Need to add all the tags mentioned in local.settings.json in Application Settings (Function App -> Configurations -> Application Settings). Mostly we need to add StorageConnections and AzureWebJobsStorage as below:

AppSettings

  1. Check CORS, if possible, add your respective storage link for the access. CORS = FunctionApp -> CORS

CORS

I tried reproducing 500 Internal Server Error when we Test/Run our code. (I did this by removing “*” in CORS

TestBeforeFix

Now the procedure for tracing this error is as follows:

Go to Log Stream: Here we will be displayed with the live metrics of our function app:

Try to Test/Run the function and in parallel have a look on the log stream and fetch for any errors

Below screenshot is after adding CORS

TestAfterFix

Also according to your code and function.json, you need to test the scenario by uploading blob source.txt in your source container, As soon as you upload it. Target.csv file will be created in target container.

Related