python - Azure 函数 (python) 将输出写入输出 blob 路径的动态命名

标签 python azure datetime azure-functions azure-storage

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

异常(exception)的写入路径:

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

最佳答案

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

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

{
   "testdirectory":"nameofdirectory"
}

之后绑定(bind)azure函数就可以获取目录名了。

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

关于python - Azure 函数 (python) 将输出写入输出 blob 路径的动态命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66455070/

相关文章:

python - Pandas 散点图未显示所有数据

Azure AD Powershell : Extract the User's last Logon Time

sql - 在Oracle sql中转换日期格式

python - 将 pandas 列按一个数字(包含时间)一分为二

python - selenium python - 无法滚动ajax无限页面

python - 如何让 python 在 Visual Studio Code MacOS 中运行

.net - 获取 AzureRmResource : A parameter cannot be found that matches parameter name 'Tag'

azure - 通过 ARM 模板对 EndpointType 为 AzureFunction 的主题进行事件订阅

PHP fatal error : Call to a member function format() on boolean

c# - 反序列化日期时间时忽略时区偏移