Mahesh Meena
Mahesh Meena

Reputation: 11

There is currently a lease on the blob and no lease ID was specified in the request

I am using python sdk for this and want to perform move to blob from one container to another but lease creating problem after breaking the lease also.

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.

Upvotes: 1

Views: 923

Answers (1)

Venkatesan
Venkatesan

Reputation: 10322

I tried in my environment and got below results:

In my environment I have an two containers with name

  1. Test
  2. Test1

Portal: enter image description here

In test container,I have a blob with leased state and also normal state.

Portal(test container):

enter image description here

After I tried with below code and the file is broken with lease and successfully copied from one container to another container.

Code:

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)

Console: enter image description here

Portal: enter image description here

Reference: azure.storage.blob.BlobLeaseClient class | Microsoft Learn

Upvotes: 2

Related Questions