简体   繁体   English

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

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

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 .我正在尝试在 Azure 函数中运行计时器触发器,该触发器将写入保存在 blob 存储容器output中的我的文件中,输出为log.txt Below is my init .py -下面是我的初始化.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 -以下是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这也是我今天检查的日志流的输出,也许这会有所帮助日志输出

The function is running but it is not writing anything to the log.txt .该函数正在运行,但它没有向log.txt写入任何内容。 I am a beginner in the azure functions, so please pardon silly mistakes.我是天蓝色函数的初学者,所以请原谅愚蠢的错误。

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.确保在 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.此外,计时器函数0 */5 * * * *中的玉米表达式每小时运行 12 次,这意味着每 5 分钟就会发生一次触发器。 so make sure you wait until the function gets triggered.所以一定要等到函数被触发。

在此处输入图像描述

just to check if the result is getting reflected I have changed the corn expression to 5-7 * * * * * which runs Three times a minute.只是为了检查结果是否得到反映,我已将玉米表达式更改为5-7 * * * * * ,它每分钟运行 3 次。

local.settings.json local.settings.json

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

Results:结果:

在此处输入图像描述

Updated Answer更新的答案

local.settings.json local.settings.json

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

function.json函数.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.下面是我的 Function App 中的文件结构。

在此处输入图像描述

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

相关问题 如何使用 Python 从 Azure Blob 容器读取文件 - How to read a file from Azure Blob Container using Python 如何将 Azure Function 绑定到 Python 中的 blob 容器? - How to bind Azure Function to a blob container in Python? 如何使用 Python 在 Azure 中创建 Blob 容器? - How to create a blob container in Azure using Python? 在PYTHON中使用SAS URI从AZURE BLOB CONTAINER下载文件 - Download file from AZURE BLOB CONTAINER using SAS URI in PYTHON 使用 Azure-Storage-Blob Python 读取 Blob 容器目录中每个 Blob 的文件大小 - Reading the File size for each blob inside a directory of a Blob Container using Azure-Storage-Blob Python 如何在 Blob 触发器中使用 Python 打开 Blob 触发文件 Azure Function - How to open a blob triggered file using Python in an blob trigger Azure Function 如何在 python 中使用 Azure 函数读取 Azure blob 文件? - how to read the Azure blob file with Azure function in python? 如何使用 python 和 pandas read_fwf ZC1C42542074E68384F5D1 处理位于 Azure blob 存储中的文件 - How to process a file located in Azure blob Storage using python with pandas read_fwf function 如何从Azure Function Python动态读取blob文件 - How to dynamically read blob file from Azure Function Python 如何在 python 应用程序中的 azure function 应用程序中写入文件? - How to write a file in azure function app in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM