python - Python 中 Azure Functions 上的文件操作

标签 python azure azure-functions

我有一个 Azure Function 应用,上面运行着 Python 代码。此 Python 应用使用 JSON 文件访问 BigQuery,读取一些数据,然后从中创建一个 JSON 文件。

我尝试了所有有意义的方法,但 Azure 生态系统异常复杂。

from google.cloud import bigquery
from google.oauth2 import service_account
import pandas as pd
import json
import datetime
import logging
import azure.functions as func


def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

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

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

    # RE01
    key_path = "bigquery-key.json"

    credentials = service_account.Credentials.from_service_account_file(
        key_path,
        scopes=["https://www.googleapis.com/auth/cloud-platform"],
    )

    client = bigquery.Client(
        credentials=credentials,
        project=credentials.project_id,
    )

    project_id = "test-database"

    sql = """
    SELECT *
    FROM test.dummy_table
    """
    # Create a dataframe and save first 10 rows to a list
    df = client.query(sql).to_dataframe()
    top_ten = df["column1"][1:10].to_list()

    with open('top_10.json', 'w') as json_file:
        json.dump(top_ten, json_file)

有人可以告诉我 JSON 文件保存在哪里吗?我似乎无法运行这个功能。

最佳答案

一旦函数执行结束(或者最近当您的实例因不活动而被驱逐时),您在函数内写入本地磁盘的任何内容都会丢失。如果您想存储结果文件,则需要在外部存储系统上执行此操作。这正是(输出)绑定(bind)的用途。例如 Azure Blob 存储。有关如何在 python 中使用它的示例,请参阅此处:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob#output---python-example

关于python - Python 中 Azure Functions 上的文件操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57831367/

相关文章:

python - 如何将多个 python 日期时间实例格式化为简单格式?

azure - 在 azure b2c 用户流程中使用已注册的电子邮件时,向用户显示未定义的错误

azure - 我可以自动扩大/缩小 Azure 应用服务计划吗?

azure - 每 3 天运行一次 Azure Function

azure-functions - 将 OpenIdConnect 与 AzureFunctions 结合使用

python - 为什么可以通过 import module 访问全局变量,而不是通过 module import var 访问全局变量?

python - 用于快速开发棋盘游戏动态编程解决方案的库

azure - 获取上次运行 Azure 函数的时间

python - 我如何获得日期之间的天数

windows - 如果 OnStart() 引发异常,我可以要求 Azure 运行时不要重新启动我的角色吗?