Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I get the Name of The inputBlob That Triggered My Azure Function With Python

I have an azure function which is triggered by a file being put into blob storage and I was wondering how (if possible) to get the name of the blob (file) which triggered the function, I have tried doing:

fileObject=os.environ['inputBlob']
message = "Python script processed input blob'{0}'".format(fileObject.fileName)

and

fileObject=os.environ['inputBlob']
message = "Python script processed input blob'{0}'".format(fileObject.name)

but neither of these worked, they both resulted in errors. Can I get some help with this or some suggesstions?

Thanks

like image 272
Kikanye Avatar asked Jul 19 '17 16:07

Kikanye


People also ask

How does Azure function blob trigger work?

The function is triggered by the creation of a blob in the test-samples-trigger container. It reads a text file from the test-samples-input container and creates a new text file in an output container based on the name of the triggered file.


2 Answers

The blob name can be captured via the Function.json and provided as binding data. See the {filename} token below. Function.json is language agnostic and works in all languages.

See documentation at https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings for details.

{
  "bindings": [
    {
      "name": "image",
      "type": "blobTrigger",
      "path": "sample-images/{filename}",
      "direction": "in",
      "connection": "MyStorageConnection"
    },
    {
      "name": "imageSmall",
      "type": "blob",
      "path": "sample-images-sm/{filename}",
      "direction": "out",
      "connection": "MyStorageConnection"
    }
  ],
}
like image 188
Mike S Avatar answered Sep 23 '22 07:09

Mike S


If you want to get the file name of the file that triggered your function you can to that:

Use {name} in function.json :

{
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "path": "MyBlobPath/{name}",
      "direction": "in",
      "connection": "MyStorageConnection"
    }
  ]
}

The function will be triggered by changes in yout blob storage.

Get the name of the file that triggered the function in python (init.py):

def main(myblob: func.InputStream):
    filemane = {myblob.name}

Will give you the name of the file that triggered your function.

like image 36
Jaro Avatar answered Sep 22 '22 07:09

Jaro