python - 如何使用 Azure 函数和 python 写入 blob 容器中的文本文件?

标签 python azure azure-functions azure-blob-storage

我正在尝试在 Azure 函数中运行计时器触发器,该触发器将作为 log.txt 写入保存在 Blob 存储容器输出中的文件。下面是我的 init.py -

import datetime
import logging

import azure.functions as func


def main(mytimer: func.TimerRequest, outputblob : func.Out[str]) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    
    outputblob.set('some text')

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)

下面是 function.json 的绑定(bind) -

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    },
    {
      "name": "outputblob",
      "type": "blob",
      "dataType": "string",
      "path": "output/log.txt",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }

  ]
}

这也是我今天检查的日志流的输出,也许这可以帮助 log_output

该函数正在运行,但没有向 log.txt 写入任何内容。我是 azure 函数的初学者,所以请原谅愚蠢的错误。

最佳答案

从我们这边复制后,效果很好。确保在 local.settings.json 中添加存储帐户的连接字符串。此外,计时器函数 0 */5 * * * * 中的 corn 表达式每小时运行 12 次,这意味着每 5 分钟就会发生一次触发。因此请确保等待该函数被触发。

enter image description here

只是为了检查结果是否得到反射(reflect),我已将 corn 表达式更改为 5-7 * * * * *,每分钟运行三次。

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<YOUR_CONNECTION_STRING>",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}

结果:

enter image description here

更新答案

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "constr":"<CONNECTION STRING>"
  }
}

函数.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "5-7 * * * * *"
    },
    {
      "name": "outputblob",
      "type": "blob",
      "dataType": "string",
      "path": "container1/log.txt",
      "connection": "constr",
      "direction": "out"
    }
  ]
}

下面是我的函数应用程序中的文件结构。

enter image description here

关于python - 如何使用 Azure 函数和 python 写入 blob 容器中的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72904046/

相关文章:

python - 绘制非多项式多元隐式方程

Azure存储帐户: general purpose vs blob storage

azure - 无法导入模块诊断。未找到 list

azure - 用于生成 Azure 函数成功和失败计数线图的 Kusto 查询

python - 使用 XLWINGS 打开工作簿而不使其可见

python - 是否有可能捕获 python 中 try block 中引发的所有警告?

java - 将从 Python UDP 流接收的字符串与 Java 字符串进行比较

python - 将 PyTorch 模型 state_dict 保存到 redis 缓存中

c# - 无法从 Visual Studio 2017 调试和发布 Azure Function

c# - 如何使用 .NET Core 在 Azure Function V3 中使用 IOptions 模式