Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update content of an existing Azure blob with Python

I am looking to update/overwrite the contents of a simple azure blob that holds a txt file with simple a date string inside. I am using it as a way to store the last run time of a certain process.

I have tried a few methods and had no luck also struggling to find any documentation that will help my cause. Any help or recommendations are appreciated.

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)

I have tried the above simple reuploading the same blob but get an error saying blob already exists.

Time:2021-08-18T11:53:00.0692411Z
ErrorCode:BlobAlreadyExists
Error:None
like image 529
Harvey Avatar asked Oct 25 '25 04:10

Harvey


1 Answers

Please try by changing the following line of code:

blob_client.upload_blob(data)

to

blob_client.upload_blob(data, overwrite=True)

and that should work.

like image 169
Gaurav Mantri Avatar answered Oct 26 '25 16:10

Gaurav Mantri