gpt4 book ai didi

python - 在python中将文件从本地上传到Azure Blob存储时出现问题

转载 作者:行者123 更新时间:2023-12-03 00:57:05 25 4
gpt4 key购买 nike

我尝试使用以下代码将一个简单的文件上传到azure blob存储。当我第一次运行时,创建了“mu_blob”,但 samplesource.txt 未上传。第二次运行时,我收到此错误,并且文件未上传。

ErrorCode:BlobAlreadyExists
Error:None

from azure.storage.blob import BlobClient

blob = BlobClient.from_connection_string(conn_str="*****", container_name="test", blob_name="my_blob")

with open("./SampleSource.txt", "rb") as data:
blob.upload_blob(data)

enter image description here

最佳答案

这可能是因为您创建的 blob 名称与 SampleSource.txt 不同。查看下面的代码示例以更好地了解 blob 的上传:

# Create a local directory to hold blob data
local_path = "./data"
os.mkdir(local_path)

# Create a file in the local data directory to upload and download
local_file_name = str(uuid.uuid4()) + ".txt"
upload_file_path = os.path.join(local_path, local_file_name)

# Write text to the file
file = open(upload_file_path, 'w')
file.write("Hello, World!")
file.close()

# 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=local_file_name)

print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)

# Upload the created file
with open(upload_file_path, "rb") as data:
blob_client.upload_blob(data)

我进一步建议您浏览Quickstart: Manage blobs with Python v12 SDK .

关于python - 在python中将文件从本地上传到Azure Blob存储时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66291322/

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