Search code examples
pythonazureazure-blob-storageazure-sdk-python

How to read the contents of Blobs using Azure Storage SDK for Python?


I've added the Azure package to my Anaconda distribution and also installed the Azure Storage SDK for Python. I'm attempting to read the files already uploaded to a specific blob container using:

from azure.storage import BlobService
blob_service = BlobService(account_name='azure subscription name',   
account_key='azure subscription key')

blobs = []
marker = None
while True:
   batch = blob_service.list_blobs('vrc', marker=marker, prefix='VRC_')
  blobs.extend(batch)
  if not batch.next_marker:
    break
  marker = batch.next_marker
for blob in blobs:
print(blob.name)

When I run this script, I receive the following error:

ImportError: No module named 'azure.storage'

How do I resolve this issue so that I can read the text files and PDFs in my blob container?


Solution

  • Not quite sure how you installed the storage sdk, or what version you're using, but you should just need to do the following:

    Install:

    pip install azure-storage
    

    Import and instantiate blob service:

    from azure.storage.blob import BlockBlobService
    blob_service = BlockBlobService(account_name="<storagename>",account_key="<storagekey>")
    

    At that point, you should be able to list blobs (or download blobs, or whatever else you need to do).