Search code examples
pythonflaskazure-web-app-servicexmltodict

File cannot be parsed and uploaded to Azure in the same function


I have this code snippet in Python/Flask where the user can upload multiple files and I want to parse the XML file first and then upload the file to Azure:

import xmltodict
def function1():
        files = request.files.getlist("file")
        for file in files:
               doc = xmltodict.parse(file)
               blobname = azure.blob_upload(file)

The file object becomes messed up somehow after either the parsing or the upload happens. When I parse it first (like in the code sample), the upload doesn't stop even for a very small file. When I upload it first, a parsing error occurs: "No element found: line 1, column 0".

The parsing line works perfectly if I run it on its own. The file is also uploaded properly and quickly to Azure if it's run on its own.

Here's the code snippet for blob_upload:

from azure.storage.blob import BlobServiceClient
service = BlobServiceClient(account_url="url", credential="key")
container = service.get_container_client("container")

def blob_upload(data):
    blob = container.get_blob_client("example")
    blob.upload_blob(data)
    return blob_name

I could probably upload it first then pull the file from Azure after, but that sounds awfully inefficient. Besides, I would want to parse the XML file first anyway to determine if it's properly formatted before I upload to Azure.

Is there a way to make this work?


Solution

  • You have to manually go back to the beginning of the file after either of these operations. I added file.seek(0) after the parse and the file can now be uploaded to Azure properly after.

    for file in files:
        doc = xmltodict.parse(file)
        file.seek(0)
        blobname = azure.blob_upload(file)