Python中Azure函数的文件操作

2024-04-18 15:22:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个运行Python代码的Azure函数应用程序。这个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文件保存在哪里吗?我似乎不能运行这个函数。在


Tags: 文件keyfromimportjsondatetimeloggingas
1条回答
网友
1楼 · 发布于 2024-04-18 15:22:35

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

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

相关问题 更多 >