Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to overwrite Azure Blob in Python

If I try to overwrite an existing blob:

blob_client = BlobClient.from_connection_string(connection_string, container_name, blob_name)
blob_client.upload_blob('Some text')

I get a ResourceExistsError.

I can check if the blob exists, delete it, and then upload it:

try:
    blob_client.get_blob_properties()
    blob_client.delete_blob()
except ResourceNotFoundError:
    pass
blob_client.upload_blob('Some text')

Taking into account both what the python azure blob storage API has available as well as idiomatic python style, is there a better way to overwrite the contents of an existing blob? I was expecting there to be some sort of overwrite parameter that could be optionally set to true in the upload_blob method, but it doesn't appear to exist.

like image 718
Robert Hickman Avatar asked Apr 09 '20 21:04

Robert Hickman


People also ask

How do you overwrite blob storage?

Upload(BinaryData, Boolean, CancellationToken)Setting overwrite to true allows updating the content of an existing block blob. Updating an existing block blob overwrites any existing metadata on the blob.

How do you update blobs?

You cannot update a Blob directly. You must create a new Blob, read the old Blob data into a buffer where you can edit or modify it, then write the modified data to the new Blob.

How do I upgrade blob storage in Azure?

Create a legacy Azure blob storage account Log in to the Azure portal and type “Storage accounts” in the search box of the Azure portal to directly jump to the storage accounts overview page. Click on the “Storage accounts” option, once it will display in the search drop-down list.


2 Answers

From this issue it seems that you can add overwrite=True to upload_blob and it will work.

like image 152
Daniel Geffen Avatar answered Nov 13 '22 13:11

Daniel Geffen


If you upload the blob with the same name and pass in the overwrite=True param, then all the contents of that file will be updated in place.

   blob_client.upload_blob(data, overwrite=True)

During the update readers will continue to see the old data by default.(until new data is committed).

I think there is also an option to read uncommitted data as well if readers wish to.

Below from the docs:

overwrite (bool) – Whether the blob to be uploaded should overwrite the current data. If True, upload_blob will overwrite the existing data. If set to False, the operation will fail with ResourceExistsError. The exception to the above is with Append blob types: if set to False and the data already exists, an error will not be raised and the data will be appended to the existing blob. If set overwrite=True, then the existing append blob will be deleted, and a new one created. Defaults to False.

like image 39
ns94 Avatar answered Nov 13 '22 13:11

ns94