Search code examples
azureazure-storageazure-python-sdk

Is there an equivalent class or module for powershell AzStorageAccount in azure python SDK?


I'm currently converting Powershell scripts to python scripts using azure python SDK. Is there an equivalent class or module to AzStorageAccount to get the list of blob urls using azure python sdk? I check the library azure.mngt.storage doesn't provide me the info i needed.


Solution

  • The package you would want to use to work with data stored in Azure Blob Storage would be azure-storage-blob (https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python). azure.mngt.storage is the SDK for managing storage account themselves and do not offer data management capabilities.

    The code would be something like:

    from azure.identity import DefaultAzureCredential
    from azure.storage.blob import BlobServiceClient
    
    account_url = "https://<storageaccountname>.blob.core.windows.net"
    default_credential = DefaultAzureCredential()
    
    blob_service_client = BlobServiceClient(account_url, credential=default_credential)
    container_client = blob_service_client.get_container_client(container_name)
    blob_list = container_client.list_blobs()
    for blob in blob_list:
        print("\t" + blob.name)
    

    You can find more code samples here: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/storage/azure-storage-blob/samples.