list and restore soft deleted blobs – azure python
Question:
I need to restore multiple blobs in my container.The storage account was enabled with soft delete for 10 days.
I see here on how to undelete , but I have multiple blobs recursively within directories which are soft deleted
However I failed to find here via python.
I need to do it in python to list and undelete the blobs
Answers:
Surely, you could find sdk method in Python Storage SDK corresponding to the REST API.
from azure.storage.blob.baseblobservice import (
BaseBlobService
)
accountName = "***"
accountKey = "***"
containerName = "***"
blobName = "***"
blobService = BaseBlobService(account_name=accountName, account_key=accountKey)
blobService.undelete_blob(containerName, blobName)
As Jay mentioned, currently you can’t tell Azure Storage REST API to return you only the soft deleted blobs. What you would need to do is list all blobs in a blob container and ask the REST API to include deleted blobs in the listing.
So you would be calling list_blobs
method and for include
parameter in that method, you would need to specify include.DELETED
. The blobs list returned will include deleted blobs.
You would then loop through the blobs and find out which of the blobs are in soft deleted state and will use undelete_blob
to undelete them.
You also mentioned that you have deleted blobs in subfolders as well. To list all blobs, you will need to specify delimiter
as empty string. You will then get blobs in sub folders as well.
To list and deleted you can use below piece of code. blob_service.list_blobs method with include=Include.DELETED will list all the deleted as well as undeleted blobs. To traverse the inside the directory use delimiter = ”.
Furthur more, if there are a lot more files in the blob then you can use a marker to page through all the results.
def restoreBlobs():
blob_service = BlockBlobService(account_name=STORAGEACCOUNTNAME, account_key=STORAGEACCOUNTKEY)
next_marker = None
while True:
blobs = blob_service.list_blobs(CONTAINERNAME, prefix=reverseSettledFolder, include=Include.DELETED, delimiter='', marker=next_marker, timeout=None)
for blob in blobs:
blob_service.undelete_blob(CONTAINERNAME, blob_name=blob.name, timeout=None)
next_marker = blobs.next_marker
if not next_marker:
break
print ("done")
Another approach, restoring everything soft-deleted:
from azure.storage.blob import ContainerClient
def main():
sas_url = f"https://{account}.blob.core.windows.net/{container}?{SAS}"
container = ContainerClient.from_container_url(sas_url)
blobs_list = container.list_blobs(
# name_starts_with="my_directory", # Uncomment if specific directory.
include="deleted"
)
count = 0
deleted = 0
for blob in blobs_list:
if blob.deleted:
blob_client = container.get_blob_client(blob)
blob_client.undelete_blob()
print("Undeleted", blob.name)
deleted += 1
count += 1
print("undeleted", deleted, "blobs out of", count)
if __name__ == "__main__":
main()
I need to restore multiple blobs in my container.The storage account was enabled with soft delete for 10 days.
I see here on how to undelete , but I have multiple blobs recursively within directories which are soft deleted
However I failed to find here via python.
I need to do it in python to list and undelete the blobs
Surely, you could find sdk method in Python Storage SDK corresponding to the REST API.
from azure.storage.blob.baseblobservice import (
BaseBlobService
)
accountName = "***"
accountKey = "***"
containerName = "***"
blobName = "***"
blobService = BaseBlobService(account_name=accountName, account_key=accountKey)
blobService.undelete_blob(containerName, blobName)
As Jay mentioned, currently you can’t tell Azure Storage REST API to return you only the soft deleted blobs. What you would need to do is list all blobs in a blob container and ask the REST API to include deleted blobs in the listing.
So you would be calling list_blobs
method and for include
parameter in that method, you would need to specify include.DELETED
. The blobs list returned will include deleted blobs.
You would then loop through the blobs and find out which of the blobs are in soft deleted state and will use undelete_blob
to undelete them.
You also mentioned that you have deleted blobs in subfolders as well. To list all blobs, you will need to specify delimiter
as empty string. You will then get blobs in sub folders as well.
To list and deleted you can use below piece of code. blob_service.list_blobs method with include=Include.DELETED will list all the deleted as well as undeleted blobs. To traverse the inside the directory use delimiter = ”.
Furthur more, if there are a lot more files in the blob then you can use a marker to page through all the results.
def restoreBlobs():
blob_service = BlockBlobService(account_name=STORAGEACCOUNTNAME, account_key=STORAGEACCOUNTKEY)
next_marker = None
while True:
blobs = blob_service.list_blobs(CONTAINERNAME, prefix=reverseSettledFolder, include=Include.DELETED, delimiter='', marker=next_marker, timeout=None)
for blob in blobs:
blob_service.undelete_blob(CONTAINERNAME, blob_name=blob.name, timeout=None)
next_marker = blobs.next_marker
if not next_marker:
break
print ("done")
Another approach, restoring everything soft-deleted:
from azure.storage.blob import ContainerClient
def main():
sas_url = f"https://{account}.blob.core.windows.net/{container}?{SAS}"
container = ContainerClient.from_container_url(sas_url)
blobs_list = container.list_blobs(
# name_starts_with="my_directory", # Uncomment if specific directory.
include="deleted"
)
count = 0
deleted = 0
for blob in blobs_list:
if blob.deleted:
blob_client = container.get_blob_client(blob)
blob_client.undelete_blob()
print("Undeleted", blob.name)
deleted += 1
count += 1
print("undeleted", deleted, "blobs out of", count)
if __name__ == "__main__":
main()