skip to Main Content

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:

enter image description here

Query-param:

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I tried the approach above but getting this error

    PS C:Users15442DesktopFinalFun> func host start
    

    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:

        HttpTrigger1: [GET,POST] http://localhost:7071/api/HttpTrigger1
    

    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.")


  2. 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.

    • Created a sample XML file and uploaded to Storage Container.

    enter image description here

    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.

    enter image description here

    init.py function

    import logging
    import azure.functions as func
    import xml.etree.ElementTree as ET
    from azure.storage.blob import BlobServiceClient
    import csv
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
    
        connection_string = "<Storage_Connection_String>"
        container_name = 'test'
    
    
        # query parameters    
        xml_file_name = req.params.get('xml_file_name')
        csv_file_name = req.params.get('csv_file_name')
    
    
        # Create a BlobServiceClient object
        blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    
        # Get a BlobClient object for the XML file
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=xml_file_name)
    
        # Download the XML file to a local file
        with open("local_xml_file.xml", "wb") as my_blob:
            download_stream = blob_client.download_blob()
            my_blob.write(download_stream.readall())
    
        # Parse the XML file and write the CSV file
        root = ET.parse("local_xml_file.xml").getroot()
        with open("local_csv_file.csv", "w", newline="") as my_csv:
            writer = csv.writer(my_csv)
            # Write the header row
            writer.writerow(["Title", "Author", "Genre", "Price", "PublishDate", "Description"])
            # Write the data rows
            for child in root:
                title = child.find("title")
                author = child.find("author")
                genre = child.find("genre")
                price = child.find("price")
                publish_date = child.find("publish_date")
                description = child.find("description")
                if title is not None and author is not None and genre is not None and price is not None and publish_date is not None and description is not None:
                 writer.writerow([title.text, author.text, genre.text, price.text, publish_date.text, description.text])
            else:
                print("Error: Missing element in XML file")
    
        # Upload the CSV file to Azure Blob Storage
        with open("local_csv_file.csv", "rb") as my_csv:
            blob_client = blob_service_client.get_blob_client(container=container_name, blob=csv_file_name)
            blob_client.upload_blob(my_csv)
        return func.HttpResponse(f"XML file converted to CSV file  successfully.")
    

    enter image description here

    enter image description here

    enter image description here

    Portal:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search