【问题标题】:Problem to upload a file from local to Azure Blob Storage in python在 python 中将文件从本地上传到 Azure Blob 存储的问题
【发布时间】:2021-05-23 06:09:10
【问题描述】:

我尝试使用以下代码将简单文件上传到 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)

【问题讨论】:

  • upload_blob有一个参数是否要覆盖
  • @ChristianSloper 我的问题是 SampleSource.txt 没有上传到 blob。
  • 请注意,samplesource 没有上传。它上传到名为“my_blob”的 blob
  • @ChristianSloper 我知道,但它是空的。我添加了一个屏幕截图。
  • 添加 overwrite=True 和 blob_type='BlockBlob' ?

标签: python azure azure-storage


【解决方案1】:

这可能是因为您创建的 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

【讨论】:

  • 我遇到了这个问题,我的解决方案是使用“from_connection_string”与其他连接方法。提交原始问题的人应检查他们的连接字符串是否正确,并检查它来自正确的存储帐户。
猜你喜欢
  • 2012-03-22
  • 2018-02-18
  • 2021-03-07
  • 2017-01-24
  • 2017-08-19
  • 2021-06-20
  • 1970-01-01
  • 2015-12-10
  • 2023-04-05
相关资源
最近更新 更多