15

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

Kikanye
  • 1,198
  • 1
  • 14
  • 33

5 Answers5

2

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://learn.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"
    }
  ],
}
Mike S
  • 3,058
  • 1
  • 22
  • 12
  • 1
    my function.json is fine but I want to use the filename in my code with python, so you know how I can do that? – Kikanye Jul 19 '17 at 19:52
1

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.

Jaro
  • 141
  • 1
  • 3
0

There is not any information about what trigger you used in your description. But fortunately, there is a sample project yokawasa/azure-functions-python-samples on GitHub for Azure Function using Python which includes many samples using different triggers like queue trigger or blob trigger. I think it's very helpful for you now, and you can refer to these samples to write your own one to satisfy your needs。

Hope it helps.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • 1
    I am using an blob trigger, but the trigger is not the problem, I understand that I just want to know if there is a way to retrieve the actual name of the file and not just 'inputBlob'. – Kikanye Jul 20 '17 at 16:09
0

Getting the name of the inputBlob is not currently possible with Python Azure-Functions. There are open issues about it in azure-webjobs-sdk and azure-webjobs-sdk-script GitHub:

https://github.com/Azure/azure-webjobs-sdk/issues/1090

https://github.com/Azure/azure-webjobs-sdk-script/issues/1339

Toni Nurmi
  • 355
  • 4
  • 20
  • That's crazy! Still the case? – jtlz2 May 16 '19 at 14:58
  • @jtlz2 with Azure Functions V2 generally available this might be fixed. Last I've checked Python V2 functions were still in preview but improved a lot many aspects including things not possible in V1 and performance improvements. – Toni Nurmi May 16 '19 at 17:22
  • Thanks for the info - I am not sure it is fixed yet though! – jtlz2 May 16 '19 at 19:24
0

Unfortunatelly it's still not possible. In Python, you can do:

import azure.functions as func
import os
def main(blobin: func.InputStream):
    filename=os.path.basename(blobin.name)