简体   繁体   English

Azure function (python) 将 output 写入动态命名的 Z78E6221F6393D14CE56DZ 路径

[英]Azure function (python) write output to dynamic naming of the output blob path

I am using an Azure function as Webhook Consumer to receive http events as JSON and storing it on Azure Storage. 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.我想根据日期对 output blob 路径进行动态命名,如下所示。 I tried lots of options, but not able to get the desired output.我尝试了很多选项,但无法获得所需的 output。 I am followed this post but no luck.我关注了这篇文章,但没有运气。

Excepted write path:例外写入路径:

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

function.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初始化.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()))

Dynamic blob name needs you to post a request in json format.动态 blob 名称需要您以 json 格式发布请求。

For example, if want to output blob to test/{testdirectory}/test.txt , then, you need to post a request like:例如,如果想 output blob 到test/{testdirectory}/test.txt ,那么,您需要发布如下请求:

{
   "testdirectory":"nameofdirectory"
}

After that, the binding of azure function will be able to get the directory name.之后,azure function 的绑定就能得到目录名。

By the way, I don't recommend binding for complex blob operations.顺便说一句,我不建议为复杂的 blob 操作绑定。 I recommend using SDK more than binding.我建议使用 SDK 而不是绑定。

I am able to achieve the dynamic path by making below changes to我可以通过以下更改来实现动态路径

function.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}/event_{DateTime}.json",
      "direction": "out",
      "connection": "MyStorageConnectionAppSetting"
    }    
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM