skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    I was able to solve the problem by using azure.mgmt.storage

    from azure.identity import DefaultAzureCredential
    from azure.mgmt.storage import StorageManagementClient
    
     storage_client = StorageManagementClient(credential=DefaultAzureCredential(), subscription_id=subscription_id)
      storage_accounts = storage_client.storage_accounts.list()
      # Get a list of all storage accounts in the subscription
      for account in storage_accounts:
           blob_url = account.primary_endpoints.blob
                    
                
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search