[英]Storage Account app authentication through Azure Function and auth0
[英]Python code on Azure Function app to convert csv files to excel on Azure storage account
我是 Python 代码的新手,不得不为工作中的项目编写它。 如果有人帮助我更正以下代码,我将不胜感激。
我有一个带有多个容器的 Azure 存储帐户。 我正在使用 HttpTrigger Function 在 Azure 数据工厂上触发 Azure function 应用程序。 当 ADF 上的管道创建 CSV 文件时,管道应该能够运行 function 应用程序以获取文件名作为参数并将 csv 文件转换为 Azure 存储帐户上的 excel 文件。
我在本地成功运行了代码,但是当我将它部署到 Function 应用程序时,出现错误。 我已经尝试运行代码数周但没有成功。
--- __init__.py file
import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import pandas as pd
from io import StringIO,BytesIO
import azure.functions as func
import xlsxwriter
import logging
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name= "1893_Item_20220206_err.csv" #'"' ,req.params.get('name'),'"'
if name:
connect_str = os.getenv('AzureWebJobsStorage')
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = "test"
blob_client = blob_service_client.get_blob_client(container=container_name, blob=name)
excel_blob_client = blob_service_client.get_blob_client(container=container_name, blob="1893_Item_20220206_err.xlsx")
blob = blob_client.download_blob()
read_file = pd.read_csv (StringIO(blob.content_as_text()),sep="|")
print(read_file)
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
read_file.to_excel (writer)
writer.save()
output.seek(0)
workbook = output.read()
excel_blob_client.upload_blob(workbook, overwrite=True)
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
-- function.json file
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"path":"test/{blobname}.csv",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
查看您的代码,我最好的猜测是您硬编码的name
参数的值包含.csv
扩展名,而路径中的name
参数不会。 您在定义中将其作为 static 字符串: "path":"test/{blobname}.csv"
。
将 append 或 csv 添加到name
参数,或将其从path
规范中删除。
接下来, path
中的参数名称是blobname
而不是name
。
这种情况可以使用Azure Blob 存储触发器来实现 Azure 功能。 这样,添加的 blob 就已经可以用作InputStream
了。
当检测到新的或更新的 blob 时,Blob 存储触发器启动 function。 blob 内容作为输入提供给 function。
这是一个例子(取自上面链接的文章
Function.json:
{
"scriptFile": "__init__.py",
"disabled": false,
"bindings": [
{
"name": "myblob",
"type": "blobTrigger",
"direction": "in",
"path": "samples-workitems/{name}",
"connection":"MyStorageAccountAppSetting"
}
]
}
和
import logging
import azure.functions as func
def main(myblob: func.InputStream):
logging.info('Python Blob trigger function processed %s', myblob.name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.