[Python] Handle DNS timeout with call to blob_client.upload_blob

Discussão em 'Python' iniciado por Stack, Novembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    While using the Azure storage SDK in Python, I have been unable to override what appears to be a default 90-second timeout to catch a DNS exception occurring within a call to blob_client.upload_blob(). I am looking for a way to override this with a shorter time interval (i.e. 5 seconds).

    The following code illustrates this issue using a fictitious account name which DNS cannot resolve. I am using a timeout argument in the call to upload_blob, and I understand from reviewing documentation this enforces a server-side threshold, not a client-side threshold. I have not been successful in getting a client-side threshold to be enforced.

    This issue appears similar to this unanswered question: How to handle timeout for Uploading a blob in Azure Storage using Python SDK?. The one (not accepted) solution suggests using a timeout threshold within the call to upload_blob. As noted above (and shown within the code below), this is not producing the desired effect.

    from azure.core.exceptions import AzureError
    from azure.storage.blob import BlobServiceClient

    # Define Azure Storage Blob connection details
    connection_string = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=removed==;EndpointSuffix=core.windows.net"
    container_name = "containername"
    blob_name = "blobname"
    local_file_path = "c:/temp/test.txt"


    # Create the BlobServiceClient
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)

    # Function to perform the blob upload
    def upload_blob_process():
    try:
    with open(local_file_path, "rb") as data:
    blob_client = blob_service_client.get_blob_client(container_name, blob_name)
    blob_client.upload_blob(data, timeout=5)
    print("Blob uploaded successfully!")
    except AzureError as e:
    print(f"Azure error occurred: {e}")
    except Exception as e:
    print(f"An error occurred: {e}")


    upload_blob_process()

    Continue reading...

Compartilhe esta Página