0

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.

RAC
  • 115
  • 1
  • 1
  • 7

2 Answers2

1

I was able to solve the problem by using azure.mgmt.storage. The storageManagementClient has a method (storage_accounts.list()) that will list all of the storage accounts. Upon checking the data returned, I see that it will also provide the blob URL per account.

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
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
RAC
  • 115
  • 1
  • 1
  • 7
0

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.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Thank you for answering but I was looking for a way to get the account_urls of blobs before plugging it up to the code you provided. – RAC Jun 20 '23 at 04:41
  • 1
    If you know the account name and the region (Azure General, Azure China etc.) where your storage account is located, you can construct the URL using `https://{account-name}.{region-specific-end-point}` pattern. For example, your blob endpoint for storage accounts in Azure General region would always be something like `https://{account-name}.blob.core.windows.net`. However if you do not know the account name in advance, then you will need to use `azure.mngt.storage` to get this information. HTH. – Gaurav Mantri Jun 20 '23 at 05:19
  • Yes, that is what I'm trying to look how can I get the account-name but azure.mngt.storage doesn't provide any information on how. – RAC Jun 20 '23 at 05:23
  • You may find this link helpful: https://github.com/Azure-Samples/azure-samples-python-management/blob/main/samples/storage/manage_storage_account.py. – Gaurav Mantri Jun 20 '23 at 05:29