我已经在本地开发并成功测试了一个将xlsb文件转换为xlsx的函数.一旦我试图在Azure门户上部署和运行,我得到了以下失败Result: Failure Exception: OSError: [Errno 30] Read-only file system: 'TEST.xlsx'
,所以我试图研究和阅读由于Azure函数Python是基于Linux的,文件只能保存到temp目录.我试图修改我的函数以包括临时目录,但我得到了新的错误Result: Failure Exception: FileNotFoundError: [Errno 2] No such file or directory: '/tmp/TEST.xlsb'
.有什么建议我可以实现这个结果:BLOB触发的Azure函数将xlsb文件(在BLOB容器中)转换为xlsx并保存到BLOB容器中?下面是我的第一次try 和对临时目录发现的后续更改:
import os
import logging
import pandas as pd
#from io import BytesIO
import azure.functions as func
from azure.storage.blob import BlobServiceClient, ContainerClient, BlobClient
app = func.FunctionApp()
@app.blob_trigger(arg_name="myblob", path="{containerName}/{name}.xlsb",
connection="BlobStorageConnectionString")
def blob_trigger(myblob: func.InputStream):
logging.info(f"Python blob trigger function processed blob"
f"Name: {myblob.name}"
f"Blob Size: {myblob.length} bytes")
accountName = "name"
accountKey = "key"
connectionString = f"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accountKey};EndpointSuffix=core.windows.net"
containerName = "{containerName}"
inputBlobname = myblob.name.replace({containerName}, "")
outputBlobname = inputBlobname.replace(".xlsb", ".xlsx")
blob_service_client = BlobServiceClient.from_connection_string(connectionString)
container_client = blob_service_client.get_container_client(containerName)
blob_client = container_client.get_blob_client(inputBlobname)
blob = BlobClient.from_connection_string(conn_str=connectionString, container_name=containerName, blob_name=outputBlobname)
df = pd.read_excel(blob_client.download_blob().readall(), engine="pyxlsb")
df.to_excel(outputBlobname, index=False)
with open(outputBlobname, "rb") as data:
blob.upload_blob(data, overwrite=True)
import os
import logging
import pandas as pd
#from io import BytesIO
import azure.functions as func
from azure.storage.blob import BlobServiceClient, ContainerClient, BlobClient
app = func.FunctionApp()
@app.blob_trigger(arg_name="myblob", path="{containerName}/{name}.xlsb",
connection="BlobStorageConnectionString")
def blob_trigger(myblob: func.InputStream):
logging.info(f"Python blob trigger function processed blob"
f"Name: {myblob.name}"
f"Blob Size: {myblob.length} bytes")
accountName = "name"
accountKey = "key"
connectionString = f"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accountKey};EndpointSuffix=core.windows.net"
containerName = "{containerName}"
inputBlobname = myblob.name.replace({containerName}, "")
localBlobname = "/tmp/" + inputBlobname
outputBlobname = inputBlobname.replace(".xlsb", ".xlsx")
blob_service_client = BlobServiceClient.from_connection_string(connectionString)
container_client = blob_service_client.get_container_client(containerName)
blob_client = container_client.get_blob_client(inputBlobname)
blob = BlobClient.from_connection_string(conn_str=connectionString, container_name=containerName, blob_name=outputBlobname)
df = pd.read_excel(blob_client.download_blob().readall(), engine="pyxlsb")
df.to_excel("/tmp/" + outputBlobname, index=False)
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
with open(file = os.path.join(ROOT_DIR, localBlobname), mode="rb") as data:
blob.upload_blob(data, overwrite=True)