i am getting an error while using azure stprage blob sdk while undeleting a container which is deleted

100 Views Asked by At

i am getting an error while using azure stprage blob sdk while undeleting a container which is deleted.

These are the package versions i am using:

azure-storage-blob = "=12.18.3" azure-mgmt-storage = "=21.1.0"

i have created the blobserviceclient and calling undelete_container function to recover a deleted container.

Create the BlobServiceClient object

blob_service_client = BlobServiceClient(storage_account_url, credential=default_credential)
recovered_containers = blob_service_client.undelete_container(container_name, deleted_container_version = '')

the issue i am facing is "deleted_container_version" is a mandatory parameter for undelete_container. but i dont know what is the value to be provided. This is defined as a string parameter.

What should be the value i neeed to pass?

this is the properties i see on deleted container. enter image description here

please guide me in fixing this. Thank you in advance

i did tried with deleted_container_version as an empty string, latest values, without passing.

while not passing deleted_container_version getting an error stating deleted_container_version is a mandatory parameter and needs to be passed.

while passing them as empty string or latest, getting an error stating the header version was an invalid value or something like that,

2

There are 2 best solutions below

0
On

I am getting an error while using Azure storage blob sdk while undeleting a container that is deleted.

I agree with KKI's comment to get the deleted containers first list the container with include_deleted=True from that you can get the version of your container.

To undelete particular container test from Azure blob storage you can use the below Python code.

Code:

from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential

storage_account_url="https://xxxx.blob.core.windows.net/"
default_credential=DefaultAzureCredential()
blob_service_client = BlobServiceClient(storage_account_url, credential=default_credential)

for container in blob_service_client.list_containers(include_metadata=True, include_deleted=True):
    if container.name == "<container name>":
        version_id = container.version
        break


blob_service_client.undelete_container("test", deleted_container_version=version_id)

Output: enter image description here

If you need to undelete all container you can use the below code.

Code:

from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential

storage_account_url="https://venkat901.blob.core.windows.net/"
default_credential=DefaultAzureCredential()
blob_service_client = BlobServiceClient(storage_account_url, credential=default_credential)

for container in blob_service_client.list_containers(include_metadata=True, include_deleted=True):
    if container.deleted:
        version_id = container.version
        container_name = container.name
        
        blob_service_client.undelete_container(container_name, deleted_container_version=version_id)

Output: enter image description here

Reference:

List blob containers with Python - Azure Storage | Microsoft Learn

0
On

This piece of code works.

def recover_soft_deleted_containers_on_storage_account(storage_account: str, container_name: str):
    """
    recover soft deleted containers.
    Operation will only be successful if used within the specified number of days set in the delete retention policy.

    Args:
        storage_account:
        container_name:

    Returns:

    """
    blob_service_client = create_blob_service_client(storage_account=storage_account)
    container_list = list(blob_service_client.list_containers(include_deleted=True))
    assert len(container_list) >= 1
    for container in container_list:
        # Find the deleted container and restore it
        if container.deleted and container.name == container_name:
            restored_container_client = blob_service_client.undelete_container(
                deleted_container_name=container.name, deleted_container_version=container.version)
    print(f"Container {container_name} recovered from {storage_account}")