【问题标题】:How to update content of an existing Azure blob with Python如何使用 Python 更新现有 Azure Blob 的内容
【发布时间】:2021-10-20 04:50:30
【问题描述】:

我希望更新/覆盖一个简单的 azure blob 的内容,该 blob 包含一个包含简单日期字符串的 txt 文件。我用它来存储某个进程的最后运行时间。

我尝试了一些方法,但也没有运气,也很难找到任何对我的事业有帮助的文档。感谢您提供任何帮助或建议。

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

maxdate = datetime.datetime.now()
now = maxdate.strftime("%m-%d-%YT%H:%M:%S")

def upload_to_blob(data):

  conn_str = "<conn_str>"
  blob_service_client = BlobServiceClient.from_connection_string(conn_str)

  blob_client = blob_service_client.get_blob_client(container="vstscontainerreleases", blob="last_run.txt")

  blob_client.upload_blob(data)

upload_to_blob(now)

我已尝试上述简单的重新上传相同的 blob,但收到错误提示 blob 已存在。

Time:2021-08-18T11:53:00.0692411Z
ErrorCode:BlobAlreadyExists
Error:None

【问题讨论】:

  • 请编辑您的问题并包含您编写的代码以及您遇到的任何错误。
  • 它与错误无关,我要求帮助我找到解决方案的有用资源
  • 您只需重新上传 blob。它会覆盖内容。你不必做任何特别的事情。
  • 尝试过并添加代码和错误以供参考仍然是 blob 已经存在的问题

标签: python azure azure-blob-storage


【解决方案1】:

请尝试更改以下代码行:

blob_client.upload_blob(data)

blob_client.upload_blob(data, overwrite=True)

这应该可行。

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 2017-03-17
    • 2018-06-27
    • 2021-07-18
    • 2022-11-06
    • 2021-11-08
    • 2021-08-02
    • 2021-11-29
    • 2020-06-25
    相关资源
    最近更新 更多