Python Script to list files in an Azure Blob Storage container

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 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:

# Replace values with your actual SAS URL, account URL and container name
sas_url = 'sp=r&st=XXXXXXXXXXXXXXXXXXX'
container_name = 'foobarcontainer'
account_url = ''
list_files_with_sas(sas_url, account_url, container_name)