Azure/azure-storage-python

How to handle timeout for Uploading a blob in Azure Storage using Python SDK?

muhammadhaseeb7 opened this issue · 1 comments

Which version of the SDK was used? Please provide the output of pip freeze.

azure-storage-blob==12.13.1

What problem was encountered?

I am trying to upload a blob to Azure Storage using Python SDK but I am unable to manage the timeout. The behavior of all timeout values are same.

from azure.storage.blob import BlobServiceClient, ContentSettings
from datetime import datetime


STORAGE_CONNECTION_STRING = ''
container = ''
blobFileName = ''
contentType = ''


def upload_blob(localFilePath):
    blob_service_client = BlobServiceClient.from_connection_string(STORAGE_CONNECTION_STRING)
    blob_client = blob_service_client.get_blob_client(container=container, blob=blobFileName)
    content_setting = ContentSettings(content_type=contentType)
    with open(localFilePath, "rb") as data:
        blob_client.upload_blob(data, overwrite=True, content_settings=content_setting, timeout=30)


@retry(Exception, tries=5, delay=1)
def uploadImageToAzure(imagePath):
    try:
        upload_blob(imagePath)
    except Exception as ex:
        print(f'Exception: {ex}, Type:{type(ex)}, Time:{datetime.now()}')
        raise Exception(ex)

    print('Successfully Uploaded the File')
    return True

uploadImageToAzure(imagePath)

To check the behavior of timeout. I just turn off my internet during uploading the blob.

Test Cases:
I just pick the random values of timeout and saw the behavior of the above code

  • timeout=1
    Exception: <urllib3.connection.HTTPSConnection object at 0x7fe5d848e9a0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:25:42.543262 Exception: <urllib3.connection.HTTPSConnection object at 0x7fe5d83c30a0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:27:04.949785 Exception: <urllib3.connection.HTTPSConnection object at 0x7fe5d83da940>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:28:28.508035 Exception: <urllib3.connection.HTTPSConnection object at 0x7fe5d837a340>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:29:58.473318 Exception: <urllib3.connection.HTTPSConnection object at 0x7fe5d8393be0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:31:19.184696
  • timeout=2
    Exception: <urllib3.connection.HTTPSConnection object at 0x7f19f0164940>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:46:25.328502 Exception: <urllib3.connection.HTTPSConnection object at 0x7f19f009b0a0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:47:50.919091 Exception: <urllib3.connection.HTTPSConnection object at 0x7f19f00b2940>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:49:14.144449 Exception: <urllib3.connection.HTTPSConnection object at 0x7f19f0056340>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:50:43.947462 Exception: <urllib3.connection.HTTPSConnection object at 0x7f19f006ebe0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:52:05.272197
  • timeout=4
    Exception: <urllib3.connection.HTTPSConnection object at 0x7f2a08699910>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:57:27.401257 Exception: <urllib3.connection.HTTPSConnection object at 0x7f2a085d41f0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 09:58:55.699520 Exception: <urllib3.connection.HTTPSConnection object at 0x7f2a085ea910>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 10:00:18.315710 Exception: <urllib3.connection.HTTPSConnection object at 0x7f2a096ce310>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 10:01:37.800908 Exception: <urllib3.connection.HTTPSConnection object at 0x7f2a096e6bb0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 10:03:02.293558
  • timeout=16
    Exception: <urllib3.connection.HTTPSConnection object at 0x7ff8c83d7880>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 10:19:15.195291 Exception: <urllib3.connection.HTTPSConnection object at 0x7ff8c830f130>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 10:20:47.769388 Exception: <urllib3.connection.HTTPSConnection object at 0x7ff8c8323880>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 10:22:14.996163 Exception: <urllib3.connection.HTTPSConnection object at 0x7ff8c82c8280>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 10:23:37.736138 Exception: <urllib3.connection.HTTPSConnection object at 0x7ff8c82ddb20>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution, Type:<class 'azure.core.exceptions.ServiceRequestError'>, Time:2023-03-28 10:25:00.846901

What do you mean by "I am unable to manage the timeout"? Are you saying that in testing the various timeout values you are seeing the actual timeout does not change? I doubt you are actually testing the network timeout here. With your network disconnected, it is failing to resolve a domain name. "Temporary failure in name resolution" is reported in each case. That is likely a different timeout value which you are not controlling with the timeout parameter you specify. That parameter probably controls the timeout once the connection is actually established.