为此,我使用python,并希望执行从一个容器到另一个容器的移动到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.
发布于 2022-11-04 09:32:07
我在我的环境中尝试过,得到了以下结果:
在我的环境中,有两个容器名为
门户:
在测试容器中,我有一个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)
控制台:
门户:
https://stackoverflow.com/questions/74217563
复制相似问题