【问题标题】:There is currently a lease on the blob and no lease ID was specified in the requestBlob 当前存在租约,请求中未指定租约 ID
【发布时间】:2022-11-05 01:32:07
【问题描述】:

我为此使用python sdk,并希望执行将blob从一个容器移动到另一个容器,但在打破租约后也会产生租约问题。

from azure.storage.blob import BlobLeaseClient, BlobServiceClient
    from app.models.remediation import RemediationRequest, RemediationType
    from app.shared.azure_storage_client import AzureStorageClient

    def remediate(self, remediation_request: RemediationRequest, account: dict, 
    file_object_metadata: dict,destination_bucket_name: str):
        file_type = file_object_metadata["file_type"]
        storage_client = AzureStorageClient(account_name=key, account_key=Value)
        if file_object_metadata['lease']['status'] == 'locked':
            connection_string = storage_client._get_connection_string()

            blob_service_client = 
                BlobServiceClient.from_connection_string(connection_string)

            container_client = 
                blob_service_client.get_container_client(source_bucket)

            blob_client = container_client.get_blob_client(blob_name)

            break_lease_result = BlobLeaseClient(blob_client).break_lease()

            storage_client.move_blob(blob_name, source_bucket, 
                 destination_bucket_name, destination_blob_name,file_type)
            
'''

blob should move with specify lease id else break the lease and move.

【问题讨论】:

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


    【解决方案1】:

    我在我的环境中尝试并得到以下结果:

    在我的环境中,我有一个名称为两个容器

    1. 测试
    2. 测试1

      门户网站:

      在测试容器中,我有一个处于租用状态和正常状态的 blob。

      门户(测试容器):

      在我尝试使用以下代码并且文件被租约破坏并成功从一个容器复制到另一个容器之后。

      代码:

      from azure.storage.blob import BlobLeaseClient, BlobServiceClient
      
      connect_strng="<connect string>"
      source_blob="https://<storage acc name>.blob.core.windows.net/test/file.json"
      blob_service_client = BlobServiceClient.from_connection_string(connect_strng)
      blob_client = blob_service_client.get_blob_client("test", "file.json")
      BlobLeaseClient(blob_client).break_lease()
      copied_blob = blob_service_client.get_blob_client("test1", 'file.json')
      copy = copied_blob.start_copy_from_url(source_blob)
      props = copied_blob.get_blob_properties()
      print(props.copy.status)
      

      安慰:

      门户网站:

      参考:azure.storage.blob.BlobLeaseClient class | Microsoft Learn

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 2022-01-19
      • 1970-01-01
      • 2018-12-14
      • 2021-06-19
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多