如何使用Python使用Azure函数在BLOB容器中写入文本文件?

发布于 2025-02-14 00:53:35 字数 1298 浏览 1 评论 0原文

我正在尝试在Azure函数中运行一个计时器触发器,该计时器触发器将写入我的文件中,保存在blob存储容器output AS log.txt中。以下是我的 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的绑定 -

{
  "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。我是Azure功能的初学者,所以请原谅愚蠢的错误。

I am trying to run a timer trigger in Azure function which will write into my file saved in the blob storage container output as log.txt. Below is my 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)

below are the bindings for 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"
    }

  ]
}

Also this is the output of the log stream which I checked today, maybe this could help
log_output

The function is running but it is not writing anything to the log.txt. I am a beginner in the azure functions, so please pardon silly mistakes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子是时光的心 2025-02-21 00:53:35

从我们的末端繁殖后,这很好。确保您正在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>"
  }
}

函数。

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

下面是我的功能应用程序中的文件结构。

This was working fine after reproducing from our end. Make sure you are adding the connection string of your storage account in local.settings.json. Also the corn expression in your timer function 0 */5 * * * * runs 12 times an hour which means for every 5 mins a trigger is going to occur. so make sure you wait until the function gets triggered.

enter image description here

just to check if the result is getting reflected I have changed the corn expression to 5-7 * * * * * which runs Three times a minute.

local.settings.json

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

Results:

enter image description here

Updated Answer

local.settings.json

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

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

Below is the file structure in my Function App.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文