I want to convert an XML file to CSV which resides inside my Azure blob storage container using Azure function via Python code, but the problem is I need to pass the XML file name and the CSV file name inside the query parameter, while test/run. How can I do it?
I wrote a Python code but got a 500 error
from io import StringIO
import logging
from azure.storage.blob import BlobServiceClient
import azure.functions as func
import pandas as pd
import requests
constrin = "conn-str"
connection_string = constrin
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(r"func-data-01")
fileName = requests.params.get('fileName')
File_Name = container_client.get_blob_client(fileName)
blob = File_Name.download_blob().readall().decode("utf-8") #decode depends on the type of file. UTF is for CSV files.
data = pd.read_csv(StringIO(blob)) #the file will be accessed and the data will be moved to dataframe and can be used further
print(f"file is found an the name is: {File_Name}")
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse(
"This HTTP-triggered function executed successfully. Pass a name in the query string or the request body for a personalized response.",
status_code=200
)
Final-Output:
Query-param:
2
Answers
I tried the approach above but getting this error
Found Python version 3.11.3 (py).
Azure Functions Core Tools Core Tools Version: 4.0.5274 Commit hash: N/A (64-bit) Function Runtime Version: 4.23.0.20886
Functions:
For detailed output, run func with --verbose flag. [2023-08-24T07:58:28.646Z] Worker process started and initialized. [2023-08-24T07:58:30.400Z] Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=0f80a05f-130e-4b93-b952-cd463892be72) [2023-08-24T07:58:30.511Z] Python HTTP trigger function processed a request. [2023-08-24T07:58:30.623Z] Executed 'Functions.HttpTrigger1' (Failed, Id=0f80a05f-130e-4b93-b952-cd463892be72, Duration=244ms) [2023-08-24T07:58:30.625Z] System.Private.CoreLib: Exception while executing function: Functions.HttpTrigger1. System.Private.CoreLib: Result: Failure Exception: ValueError: Please specify a container name and blob name. Stack: File "C:Program FilesMicrosoftAzure Functions Core Toolsworkerspython3.11WINDOWSX64azure_functions_workerdispatcher.py", line 479, in _handle__invocation_request call_result = await self._loop.run_in_executor( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:Program FilesPython311Libconcurrentfuturesthread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:Program FilesMicrosoftAzure Functions Core Toolsworkerspython3.11WINDOWSX64azure_functions_workerdispatcher.py", line 752, in _run_sync_func
return ExtensionManager.get_sync_invocation_wrapper(context, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:Program FilesMicrosoftAzure Functions Core Toolsworkerspython3.11WINDOWSX64azure_functions_workerextension.py", line 215, in raw_invocation_wrapper result = function(**args) ^^^^^^^^^^^^^^^^ File "C:Users15442DesktopFinalFunHttpTrigger1_init.py", line 25, in main blob_client = blob_service_client.get_blob_client(container=container_name, blob=xml_file_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:Users15442AppDataRoamingPythonPython311site-packagesazurestorageblob_blob_service_client.py", line 777, in get_blob_client return BlobClient( # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:Users15442AppDataRoamingPythonPython311site-packagesazurestorageblob_blob_client.py", line 167, in init raise ValueError("Please specify a container name and blob name.")
I have reproduced your requirement in my environment.
I could read XML file which is available in Storage container and convert it to CSV file using Python Azure function.
My Python Azure function code:
This function code downloads the XML file to local, converts to CSV and then upload it to Azure Storage Container.
init.py function
Portal: