elyptikus
elyptikus

Reputation: 1148

Blob name patterns of Azure Functions in Python

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?

Upvotes: 1

Views: 1654

Answers (2)

mantavya.x
mantavya.x

Reputation: 11

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. :)

Upvotes: 1

suziki
suziki

Reputation: 14080

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.:)

Upvotes: 2

Related Questions