python - Python 中 Azure Functions 上的文件操作
问题描述
我有一个运行 Python 代码的 Azure Function 应用程序。这个 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 文件保存在哪里吗?我似乎无法运行此功能。
解决方案
一旦函数执行结束(或者当您的实例因不活动而被逐出时),您在函数内部写入本地磁盘的任何内容都会丢失。如果要存储结果文件,则需要在外部存储系统上进行。这正是(输出)绑定的用途。例如 Azure Blob 存储。有关如何在 python 中使用它的示例,请参见此处:
推荐阅读
- android-studio - 构建 android OCR tesseract 时未找到数据文件错误,因此无法查看 OCR 结果
- java - 有没有办法解决 upi_pay: ^0.2.0 插件和 razorpay_flutter: ^1.2.2 插件之间的冲突,并在 android 应用程序中同时使用两者
- reactjs - 如何在反应中包含动画 CDN
- reinforcement-learning - DQN:在将观察空间转换为 Box 环境后访问原始观察结果?
- python - 如何将函数作为变量运行?
- python - 在列表理解中使用 if else 语句时出现语法错误
- apache-spark - Spark 在 Linux (EMR) 中将其方法存储在哪个目录中?
- python - 如何使用openCV python从文本文档中删除水印?
- javascript - 无法在服务器端 NodeJS 上启用 CORS
- python - 递归调用python类方法