简体   繁体   中英

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?

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

This is old question but become relevant due to latest deprecation of some code blocks in SDK.

Please follow the below to get list of blob files in a given container with modified date too.

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
storageconnectionstring='yourstorageconnectionstring'
blobclient=BlobServiceClient.from_connection_string(storageconnectionstring)
containerclient=blobclient.get_container_client('yourblobcontainername')
for blobs in containerclient.list_blobs():
    print (blobs['name'],": Modified: ",blobs['last_modified'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM