I've developed and successfully tested locally a function that converts xlsb files to xlsx. Once I tried to deploy and run on Azure Portal, I got the following failure Result: Failure Exception: OSError: [Errno 30] Read-only file system: 'TEST.xlsx' So I tried to research and read that due to Azure Function Python being linux based, files could only be saved to temp directory. I tried to modify my function to include temp directory but I get new error Result: Failure Exception: FileNotFoundError: [Errno 2] No such file or directory: '/tmp/TEST.xlsb'. Any suggestions on how I can achieve this result: blob triggered azure function that converts xlsb file (in blob container) to xlsx and saves in blob container? Below are my first attempt and subsequent changes with the temp directory discovery:
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)
I tried the following code to convert a .xlsb blob to a .xlsx blob in an Azure storage container using the Azure Function app.
Code :
local.settings.json :
Output :
The blob trigger function code is running, and I uploaded the kamb.xlsb file to the Azure blob storage container as shown below:
I received the message output: "blob kamb.xlsb converted to kamb.xlsx" as shown below:
Afterward, I successfully deployed my project to the Azure function app, as shown below:
I uploaded the kamb.xlsb file to the Azure blob storage container, and it ran successfully in the Function app on the Azure Portal, as shown below:
The blob kamb.xlsb was converted to kamb.xlsx in the storage container, as shown below.
kamb.xlsx data :
Function app Monitor Logs :