Shashank Lothe
Shashank Lothe

Reputation: 41

Blob Trigger for Python Function App is not Firing

I am using Ubuntu 16.04.5 LTS local machine to create and publish Python Function App to Azure using CLI and Azure Functions Core Tools (Ref). I have configured Blob Trigger and my function.json file looks like this:

{
"disabled": false,
"scriptFile": "__init__.py",
"bindings": [
{
  "name": "<Blob Trigger Name>",
  "type": "blobTrigger",
  "direction": "in",
  "path": "<Blob Container Name>/{name}",
  "connection": "<Connection String having storage account and key>"
},
{
  "name": "outputblob",
  "type": "blob",
  "path": "<Blob Container Name>",
  "connection": "<Connection String having storage account and key>",
  "direction": "out"
}
]
}

My init.py function looks like this.

def main(<Blob Trigger Name>: func.InputStream, doc: func.Out[func.Document]):
logging.info(f"Python blob trigger function processed blob \n"
             f"Name: {<Blob Trigger Name>.name}\n"
             f"Blob Size: {<Blob Trigger Name>.length} bytes")
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

# Write text to the file.
file = open("QuickStart.txt",  'w')
file.write("Hello, World!")
file.close()

# Create the BlockBlockService that is used to call the Blob service for the storage account
block_blob_service = BlockBlobService(account_name='<Storage Account Name>', account_key='<Storage Account Key>')
container_name='<Blob Container Name>'

# Set the permission so the blobs are public.
block_blob_service.set_container_acl(container_name, public_access=PublicAccess.Container)

# Upload the created file, use local_file_name for the blob name
block_blob_service.create_blob_from_path(container_name, 'QuickStart.txt', '')

The Function App is "Always On" but when I upload a blob in the storage the function is not getting triggered. Another Reference Link is this (Ref).

What's going wrong?

Thanks and regards, Shashank

Upvotes: 4

Views: 3003

Answers (3)

Alistair McIntyre
Alistair McIntyre

Reputation: 414

In your main def of py script you have 2nd argument of doc: func.Out[func.Document], which is for cosmos db. This should be an output stream, as its of type blob

Upvotes: 0

rouhija
rouhija

Reputation: 241

Have you checked that the local.settings.json (connection strings for storage accounts) are also in the function app in Azure? They are not published from local machine by default.

You can configure them manually in the portal or use the publish-local-settings flag:

func azure functionapp publish "functionname" --publish-local-settings

Upvotes: 3

Mohit Verma
Mohit Verma

Reputation: 5294

I tried to reproduce this issue by creating a sample function app in python using Visual studio code with default template and finally deployed in Linux. It worked for me.

Here is the piece of code i have written in pyhton file.

import logging

import azure.functions as func


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")

and here is the function.json file from my function app.

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "samples-workitems/{name}",
      "connection": ""
    }
  ]
}

I am using 2.0 Azure function , python 3.6 and Azure Functions Core Tools version 2.2.70

this is the reference link i used :

https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-python

Please try to use this and see if it helps.

Upvotes: 1

Related Questions