Hi @Dutta, Pijush
Thank you for posting this in Microsoft Q&A.
You are facing a permission restriction where you can only access a specific folder in an Azure Blob Storage container using a SAS token. Your previous approach using container_client.list_blobs()
is no longer feasible due to the limited permissions.
Here’s a sample code snippet
def delete_blobs(sas_token, container_name, folder_path, prefix=None):
"""Deletes blobs within a specified folder using a SAS token.
Args:
sas_token: The SAS token for the folder.
container_name: The name of the container.
folder_path: The path to the folder within the container.
prefix: Optional prefix for filtering blobs.
"""
base_url = f"https://{container_name}.blob.core.windows.net/{folder_path}"
sas_url = f"{base_url}?{sas_token}"
blob_client = BlobClient.from_blob_url(sas_url)
blobs_to_delete = []
for blob in blob_client.list_blobs_flat():
if prefix and not blob.name.startswith(prefix):
continue
blobs_to_delete.append(blob.name)
for blob_name in blobs_to_delete:
blob_client = BlobClient.from_blob_url(f"{sas_url}/{blob_name}")
blob_client.delete_blob()
I hope this helps!
Please mark as "Accept the answer" if the above steps helps you. Your suggestion will help others also!