Search code examples
pythonpython-3.xazureazure-storageazure-batch

FileUploadMiscError while persisting output file from Azure Batch


I'm facing the following error while trying to persist log files to Azure Blob storage from Azure Batch execution - "FileUploadMiscError - A miscellaneous error was encountered while uploading one of the output files". This error doesn't give a lot of information as to what might be going wrong. I tried checking the Microsoft Documentation for this error code, but it doesn't mention this particular error code. Below is the relevant code for adding the task to Azure Batch that I have ported from C# to Python for persisting the log files.

Note: The container that I have configured gets created when the task is added, but there's no blob inside.

import datetime
import logging
import os

import azure.storage.blob.models as blob_model
import yaml
from azure.batch import models
from azure.storage.blob.baseblobservice import BaseBlobService
from azure.storage.common.cloudstorageaccount import CloudStorageAccount
from dotenv import load_dotenv

LOG = logging.getLogger(__name__)


def add_tasks(batch_client, job_id, task_id, io_details, blob_details):

    task_commands = "This is a placeholder. Actual code has an actual task. This gets completed successfully."

    LOG.info("Configuring the blob storage details")
    base_blob_service = BaseBlobService(
        account_name=blob_details['account_name'],
        account_key=blob_details['account_key'])
    LOG.info("Base blob service created")

    base_blob_service.create_container(
        container_name=blob_details['container_name'], fail_on_exist=False)
    LOG.info("Container present")

    container_sas = base_blob_service.generate_container_shared_access_signature(
        container_name=blob_details['container_name'],
        permission=blob_model.ContainerPermissions(write=True),
        expiry=datetime.datetime.now() + datetime.timedelta(days=1))
    LOG.info(f"Container SAS created: {container_sas}")

    container_url = base_blob_service.make_container_url(
        container_name=blob_details['container_name'], sas_token=container_sas)
    LOG.info(f"Container URL created: {container_url}")

    # fpath = task_id + '/output.txt'
    fpath = task_id

    LOG.info(f"Creating output file object:")
    out_files_list = list()

    out_files = models.OutputFile(
        file_pattern=r"../stderr.txt",
        destination=models.OutputFileDestination(
            container=models.OutputFileBlobContainerDestination(
                container_url=container_url, path=fpath)),
        upload_options=models.OutputFileUploadOptions(
            upload_condition=models.OutputFileUploadCondition.task_completion))

    out_files_list.append(out_files)
    LOG.info(f"Output files: {out_files_list}")

    LOG.info(f"Creating the task now: {task_id}")
    task = models.TaskAddParameter(
        id=task_id, command_line=task_commands, output_files=out_files_list)

    batch_client.task.add(job_id=job_id, task=task)
    LOG.info(f"Added task: {task_id}")

Solution

  • There is a bug in Batch's OutputFile handling which causes it to fail to upload to containers if the full container URL includes any query-string parameters other than the ones included in the SAS token. Unfortunately, the azure-storage-blob Python module includes an extra query string parameter when generating the URL via make_container_url.

    This issue was just raised to us, and a fix will be released in the coming weeks, but an easy workaround is instead of using make_container_url to craft the URL, craft it yourself like so: container_url = 'https://{}/{}?{}'.format(blob_service.primary_endpoint, blob_details['container_name'], container_sas).

    The resulting URL should look something like this: https://<account>.blob.core.windows.net/<container>?se=2019-01-12T01%3A34%3A05Z&sp=w&sv=2018-03-28&sr=c&sig=<sig> - specifically it shouldn't have restype=container in it (which is what the azure-storage-blob package is including)