3

In a Django (Python) project, I'm using Azure blobs to store photos uploaded by users. The code simply goes something like this:

from azure.storage.blob import BlobService

blob_service = BlobService(account_name=accountName, account_key=accountKey)
blob_service.put_blob(
            'pictures',
            name, # including folder
            content_str, # image as stream
            x_ms_blob_type='BlockBlob',
            x_ms_blob_content_type=content_type,
            x_ms_blob_cache_control ='public, max-age=3600, s-maxage=86400'
        )

My question is: what's the equivalent method to delete an uploaded photo in my particular scenario? I'm writing a task to periodically clean up my data models, and so want to get rid of images associated to them as well.

Hassan Baig
  • 15,055
  • 27
  • 102
  • 205

1 Answers1

2

You should be able to use:

blob_service.delete_blob(container_name, blob_name)

You can also delete an entire container:

blob_service.delete_container(container_name)

There are a few extra parameters which will be helpful to you if you're trying to delete snapshots, deal with leases, etc.

Note that put_blob() is defined in blockblobservice.py, while delete_blob() is defined in baseblobservice.py (deletes are going to be the same, whether page, block, or append blob).

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • Thanks for the assist David. I checked out the `delete_blob` and `delete_container` methods. There's no way to delete selective blob objects in **bulk** is there? I have to delete multiple objects (but not everything in the container); currently I'm looping through a for loop to do it. – Hassan Baig Jun 20 '17 at 16:13