简体   繁体   中英

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 . 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日志输出

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.

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.

在此处输入图像描述

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:

在此处输入图像描述

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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