1
votes

I am using an Azure function as Webhook Consumer to receive http events as JSON and storing it on Azure Storage. I want to do Dynamic naming of the output blob path based on date as shown below. I tried lots of options, but not able to get the desired output. I am followed this post but no luck.

Excepted write path:

source/
      ctry/
          yyyy/
              mm/ 
                date/
                    hrs/
                        event_{systemtime}.json

function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "type": "blob",
      "name": "outputblob",
      "path": "source/ctry/{datetime:yyyy}/{datetime:MM}/{datetime:dd}/{datetime:hh}/event_{systemtime}.json",
      "direction": "out",
      "connection": "MyStorageConnectionAppSetting"
    }    
  ]
}

init.py

import logging

import azure.functions as func

def main(req: func.HttpRequest,outputblob: func.Out[str]) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = 'some_name'
    if not name:
        try:
            req_body = 'req_body_test'#req.get_json()
        except ValueError:
            pass
    else:
        name = 'name'#req_body.get('name')
    
    print(str(req.get_json()))

    outputblob.set(str(req.get_json()))
2
What is the binding expression {systemtime} suppose to be I have not seen it beforeBjorne

2 Answers

1
votes

Dynamic blob name needs you to post a request in json format.

For example, if want to output blob to test/{testdirectory}/test.txt, then, you need to post a request like:

{
   "testdirectory":"nameofdirectory"
}

After that, the binding of azure function will be able to get the directory name.

By the way, I don't recommend binding for complex blob operations. I recommend using SDK more than binding.

1
votes

I am able to achieve the dynamic path by making below changes to

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "type": "blob",
      "name": "outputblob",
      "path": "source/ctry/{DateTime:yyyy}/{DateTime:MM}/{DateTime:dd}/event_{DateTime}.json",
      "direction": "out",
      "connection": "MyStorageConnectionAppSetting"
    }    
  ]
}