【问题标题】:Azure function (python) write output to dynamic naming of the output blob pathAzure函数(python)将输出写入动态命名的输出blob路径
【发布时间】:2021-06-01 22:07:45
【问题描述】:

我使用 Azure 函数作为 Webhook Consumer 以 JSON 形式接收 http 事件并将其存储在 Azure 存储中。 我想根据日期对输出 blob 路径进行动态命名,如下所示。我尝试了很多选项,但无法获得所需的输出。 我关注了这个post 但没有运气。

例外的写入路径:

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()))

【问题讨论】:

  • 什么是绑定表达式 {systemtime} 假设我以前没见过

标签: python azure datetime azure-functions azure-storage


【解决方案1】:

动态 blob 名称需要您以 json 格式发布请求。

例如,如果想将 blob 输出到test/{testdirectory}/test.txt,那么,您需要发布如下请求:

{
   "testdirectory":"nameofdirectory"
}

之后绑定azure函数就可以得到目录名了。

顺便说一句,我不建议对复杂的 blob 操作进行绑定。我建议使用 SDK 而不是绑定。

【讨论】:

  • 您是否有任何 SDK (Python) 示例以使用动态路径写入 azure 存储
  • @nilesh1212 你可以看看这个官方文档:docs.microsoft.com/en-us/azure/storage/blobs/…
  • @nilesh1212 或者你可以看看这个:stackoverflow.com/questions/62953148/… 只需将"MyFirstBlob.txt" 更改为动态值。
  • 我不能完全做到,但接近解决方案。非常感谢您的帮助{“type”:“blob”,“name”:“outputblob”,“path”: "raw/yotpo/{DateTime:yyyy}/{DateTime:MM}/{DateTime:dd}/event_{DateTime}.json", "direction": "out", "connection": "MyStorageConnectionAppSetting" }
  • 是的,我只在 function.json 绑定部分进行了更改。
【解决方案2】:

我可以通过以下更改来实现动态路径

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"
    }    
  ]
}

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 2023-02-23
    • 1970-01-01
    • 2020-12-16
    • 2020-09-17
    • 2017-09-29
    • 1970-01-01
    • 2016-07-25
    • 2020-07-23
    相关资源
    最近更新 更多