gpt4 book ai didi

python - Blob 当前有租约,但请求中未指定租约 ID

转载 作者:行者123 更新时间:2023-12-03 01:17:13 24 4
gpt4 key购买 nike

我为此使用 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.

最佳答案

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

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

  1. 测试
  2. 测试1

门户: enter image description here

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

门户(测试容器):

enter image description here

在我尝试使用下面的代码后,文件因租约而被破坏,并成功从一个容器复制到另一个容器。

代码:

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)

控制台: enter image description here

门户: enter image description here

引用: azure.storage.blob.BlobLeaseClient class | Microsoft Learn

关于python - Blob 当前有租约,但请求中未指定租约 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74217563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com