Python Script to list files in an Azure Blob Storage container

One9twO
Jan 8, 2024

I asked ChatGPT to “write python script to list files in an Azure storage container using SAS url”. It generated a script that almost worked (the part that prevented it from working was it was using sas url as the account url).

Here’s the script after fixing the error:

from azure.storage.blob import BlobServiceClient

def list_files_with_sas(sas_url, account_url, container_name):
blob_service_client = BlobServiceClient(account_url=account_url, credential=sas_url)

container_client = blob_service_client.get_container_client(container_name)

# List files in the container
blob_list = container_client.list_blobs()

# Print the names of the files
print(f"Files in the '{container_name}' container:")
for blob in blob_list:
print(blob.name)

# Replace values with your actual SAS URL, account URL and container name
sas_url = 'sp=r&st=XXXXXXXXXXXXXXXXXXX'
container_name = 'foobarcontainer'
account_url = 'https://foobarstorageaccount.blob.core.windows.net'
list_files_with_sas(sas_url, account_url, container_name)

--

--