gpt4 book ai didi

python - AzureBlob上传错误:The specified blob already exists

转载 作者:行者123 更新时间:2023-12-03 15:52:58 26 4
gpt4 key购买 nike

我每天都尝试将文件上传到 Azure 容器。

上传具有相同文件的文件时出现错误:“指定的 blob 已存在”(我想覆盖该文件)

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

conn_str = yml['AZURE_BLOB']['CONN_STR']
container_name = yml['AZURE_BLOB']['CONTAINER_NAME']

# Create the BlobServiceClient that is used to call the Blob service for the storage account
blob_service_client = BlobServiceClient.from_connection_string(conn_str=conn_str)

# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=destination_file_name)

# Upload the created file
data = fs.open(source_path,mode='rb').read()
blob_client.upload_blob(data)

print(destination_file_name+'\t......[DONE]')

错误消息:

azure.core.exceptions.ResourceExistsError: The specified blob already exists.
RequestId:13d062cd-801e-00a4-77c7-a81c56000000
Time:2019-12-02T04:18:06.0826908Z
ErrorCode:BlobAlreadyExists
Error:None

最佳答案

如果您想使用Blob storage client library v12覆盖现有的blob ,只需在 upload_blob 方法中添加 overwrite=True 即可。

这里是示例代码:

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

conn_str = "xxx"
container_name = "test6"

blob_service_client = BlobServiceClient.from_connection_string(conn_str=conn_str)
blob_client = blob_service_client.get_blob_client(container=container_name,blob="a1.txt")

with open("F:\\temp\\a1.txt","rb") as data:
blob_client.upload_blob(data,overwrite=True)

print("**completed**")

执行代码后,将上传新的 blob,并且可以覆盖现有的 blob。截图如下:

enter image description here

关于python - AzureBlob上传错误:The specified blob already exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59132689/

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