Arnab Biswas
Arnab Biswas

Reputation: 4605

Azure Storage Blob (v12) Python API: Retry while acquiring lease

I am using Python API for Azure Storage v12.x (doc). I am trying to acquire lease for a blob using the following code:

credentials = ...
blob_client = BlobClient(account_url=ACCOUNT_URL, container_name=CONTAINER_NAME, blob_name=BLOB_NAME, credential=credentials)
lease_id_str = str(uuid.uuid4())
lease = blob_client.acquire_lease(lease_duration=60, lease_id=lease_id_str)

Now, I want to retry if the lease is not available. The question is "How should I acquire lease with retry?"

Following are the few things I have tried:

1.This documentation talks about properties retry_connect, retry_read, retry_status. Adding those as additional argument to the above code didn't help.

2.Following this test case, I have tried following:

from azure.storage.blob import ExponentialRetry

retry = ExponentialRetry(initial_backoff=1, increment_base=3, retry_total=3)
lease = BlobLeaseClient(blob_client, lease_id=lease_id_str, retry_policy=retry)

It returned the following:

TypeError: init() got an unexpected keyword argument 'retry_policy'

Upvotes: 0

Views: 1158

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136216

I believe the reason you're getting this error is because you're trying to pass unsupported retry_policy argument to BlobLeaseClient.

If you look at the test case link code you shared, there retry policy is passed to BlobServiceClient object. Can you try using that? Something like:

retry = ExponentialRetry(initial_backoff=1, increment_base=3, retry_total=3)
blob_service_client = BlobServiceClient(account_url=ACCOUNT_URL, credential=credentials, retry_policy=retry)
blob_client = blob_service_client.get_blob_client(container=CONTAINER_NAME, blob=BLOB_NAME)
lease = BlobLeaseClient(blob_client, lease_id=lease_id_str)

Or try something like the following. Here, the retry policy is passed when creating an instance of BlobClient.

credentials = ...
retry = ExponentialRetry(initial_backoff=1, increment_base=3, retry_total=3)
blob_client = BlobClient(account_url=ACCOUNT_URL, container_name=CONTAINER_NAME, blob_name=BLOB_NAME, credential=credentials, retry_policy=retry)
lease = BlobLeaseClient(blob_client, lease_id=lease_id_str)

Upvotes: 0

Related Questions