繁体   English   中英

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

[英]How to write to a text file in a blob container using Azure function with python?

我正在尝试在 Azure 函数中运行计时器触发器,该触发器将写入保存在 blob 存储容器output中的我的文件中,输出为log.txt 下面是我的初始化.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的绑定 -

{
  "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.txt写入任何内容。 我是天蓝色函数的初学者,所以请原谅愚蠢的错误。

从我们的末端复制后,这工作正常。 确保在 local.settings.json 中添加存储帐户的连接字符串。 此外,计时器函数0 */5 * * * *中的玉米表达式每小时运行 12 次,这意味着每 5 分钟就会发生一次触发器。 所以一定要等到函数被触发。

在此处输入图像描述

只是为了检查结果是否得到反映,我已将玉米表达式更改为5-7 * * * * * ,它每分钟运行 3 次。

local.settings.json

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

结果:

在此处输入图像描述

更新的答案

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

下面是我的 Function App 中的文件结构。

在此处输入图像描述

暂无
暂无

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

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