【问题标题】:Azure Storage Blob (v12) Python API: Retry while acquiring leaseAzure 存储 Blob (v12) Python API:在获取租约时重试
【发布时间】:2021-06-19 02:11:57
【问题描述】:

我正在为 Azure Storage v12.x (doc) 使用 Python API。我正在尝试使用以下代码获取 blob 的租约:

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)

现在,如果租约不可用,我想重试。问题是“我应该如何通过重试获得租约?”

以下是我尝试过的几件事:

1.This documentation 谈论属性retry_connectretry_readretry_status。将这些作为附加参数添加到上述代码没有帮助。

2.关注this test case,我尝试过关注:

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)

它返回以下内容:

TypeError: init() 得到了一个意外的关键字参数“retry_policy”

【问题讨论】:

    标签: python azure azure-storage azure-blob-storage


    【解决方案1】:

    我相信您收到此错误的原因是您试图将不受支持的 retry_policy 参数传递给 BlobLeaseClient

    如果您查看您共享的测试用例链接代码,则会将重试策略传递给BlobServiceClient 对象。你可以尝试使用它吗?比如:

    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)
    

    或者试试下面的方法。这里在创建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)
    

    【讨论】:

    • 这些方法都不起作用。 :-( 另外,get_blob_client 的参数是 containerblob(不是 container_nameblob_name
    • 仍然出现同样的错误?我尝试上传 blob 并没有收到任何错误。
    • 无错误。但是,根据日志,我没有看到任何重试。
    • 哦,这完全不同。请注意,默认情况下并非所有异常都是可重试的(例如,400 错误中没有一个是可重试的)。你返回的 HTTP 状态码是什么?
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 2020-11-30
    • 2021-01-04
    • 1970-01-01
    • 2016-06-25
    • 2021-04-22
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多