python - 如何使用 Python 更新现有 Azure blob 的内容

标签 python azure azure-blob-storage

我正在寻找更新/覆盖一个简单的 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_client.upload_blob(data)

blob_client.upload_blob(data, overwrite=True)

这应该可行。

关于python - 如何使用 Python 更新现有 Azure blob 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68831573/

相关文章:

python - 我如何将新数据 append 到sql(使用python 2.7)

java - 如何在Java中基于SAS Token访问Azure WASB容器?

azure - 从 AKS 群集访问 Azure 存储 Blob

python - 从 Azure Databricks 将数据写入 Azure Blob 存储

python - 使用搅拌器创建平铺 map

python - pandas 用其他数据帧替换数据帧中的行

azure - MS Azure PaaS Web 应用服务 + 本地部署

azure - Azure 是否从原始发布\包自动恢复?

python - 将python作为脚本运行时导入失败,但在iPython中没有?

sql-server - 如何恢复我的 Azure SQL 数据库