Selectively deleting images uploaded to Azure blob (Django/Python project)

岁酱吖の 提交于 2021-01-28 00:57:30

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/44643421/selectively-deleting-images-uploaded-to-azure-blob-django-python-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!