Azure / azure-storage-python

Microsoft Azure Storage Library for Python
https://azure-storage.readthedocs.io
MIT License
339 stars 241 forks source link

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

Open muhammadhaseeb7 opened 1 year ago

muhammadhaseeb7 commented 1 year ago

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

buchs commented 4 months ago

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.