1

I am implementing an Azure Function in Python which is triggered by a file uploaded to blob storage. I want to specify the pattern of the filename and use its parts inside my code as follows:

function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "inputblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "dev/sources/{filename}.csv",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

The executed __init__.py file looks as follows:

import logging
import azure.functions as func


def main(inputblob: func.InputStream):
    logging.info('Python Blob trigger function processed %s', inputblob.filename)

The error message that I get is: AttributeError: 'InputStream' object has no attribute 'filename'. As a reference, I used this documentation.

Did I do something wrong or is it not possible to achieve what I want in Python?

5
  • The documentation you point to uses inputblob.name, not .filename. Commented Jun 24, 2020 at 7:19
  • Please notice that it should be name instead of filename. Commented Jun 24, 2020 at 7:22
  • myblob.name in my case gives me /dev/sources/myfile.csv, but I would like to extract only myfile. Commented Jun 24, 2020 at 7:34
  • Please have a look of this blog: medium.com/@loopjockey/… Azure blob storage is flat, So there is no built-in method to get what you want. Commented Jun 24, 2020 at 8:09
  • I have update the answer again, please let me know whether it is what you want.:) Commented Jun 24, 2020 at 8:48

2 Answers 2

2

Your function code should be this:

import logging
import os

import azure.functions as func


def main(myblob: func.InputStream):
    head, filename = os.path.split(myblob.name)
    name = os.path.splitext(filename)[0]
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name without extension: {name}\n"
                 f"Filename: {filename}")

It should be name instead of filename.:)

Sign up to request clarification or add additional context in comments.

10 Comments

I actually changed this variable on purpose for testing. Because executing the function as you suggested, doesn't give me the actual string that is between the path and the extension, but it gives me the full path including the extension. myblob.name would be /dev/sources/myfile.csvand not only myfile.
@elyptikus Please notice that the blob storage is flat, it it not a hierarchical storage. So you can not get the filename directly.
@elyptikus Please have a look of this: learn.microsoft.com/en-us/rest/api/storageservices/…
@elyptikus Your idea will lead to this situation: dev/sources/1.csv, dev/sources/1/2.csv and dev/sources/1/2/3.csv will both trigger this function and in your situation the filename will be 1, 1/2, 1/2/3.
I see what you want to tell me but this is not what I am asking for. My problem is, that I can not extract the filename at all. How would you proceed to extract the filename 1, 1/2, or 1/2/3? Could you please share some code that gives these outputs?
|
1

I know its really late but I was going through the same problem and I got a getaway so I decided to answer you here.

You can just do reassemble the string in python.

inside init.py -

filenameraw = inputblob.name
filenameraw = filenameraw.split('/')[-1]
filenameraw = filenameraw.replace(".csv","")

with this you'll get your desired output. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.