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

5

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

Babu answered 19/7, 2017 at 16:38 Comment(4)
Any luck in figuring this out? If so, would you mind showing us your answer below?Antidromic
@Antidromic I was unable to do this with Python unfortunatelyBabu
@Babu Is it still the case that this is not possible??!Ellett
@Ellett I found a way around it by passing the name of the file through a queue message, but no it is still not possible so far as I knowBabu
A
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"
    }
  ],
}
Analgesia answered 19/7, 2017 at 17:5 Comment(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?Babu
H
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.

Hin answered 29/10, 2020 at 10:19 Comment(0)
M
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.

Mind answered 20/7, 2017 at 8:40 Comment(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'.Babu
T
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

Thereinto answered 25/10, 2017 at 6:49 Comment(3)
That's crazy! Still the case?Ellett
@Ellett 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.Thereinto
Thanks for the info - I am not sure it is fixed yet though!Ellett
K
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)
Kinin answered 10/6, 2019 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.