2016-11-04 151 views
0

我已将Azure包添加到Anaconda发行版,并且还安装了适用于Python的Azure存储SDK。我试图使用读取已上传至特定BLOB容器中的文件:如何使用Azure存储SDK for Python读取Blob的内容?

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) 

当我运行此脚本时,我收到以下错误:

ImportError: No module named 'azure.storage' 

请问该如何解决这个问题,所以我可以读取我的blob容器中的文本文件和PDF文件?

回答

1

不太清楚你如何安装存储SDK,或者是什么版本您正在使用,但你应该只需要做到以下几点:

安装:

pip install azure-storage 

导入和实例化blob服务:

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

在这一点上,你应该能够列出斑点(或下载斑点或任何你需要做的)。

+0

我在命令行中使用git从Github下载了存储SDK。然后我运行pip install azure-storage。我尝试使用Anaconda安装它,但该软件包不可用,只是Azure软件包。 – SidC

+1

'azure-storage'需要密码学。如果你使用的是linux,你也需要这样做: https://cryptography.io/en/latest/installation/#building-cryptography-on-linux –

+0

我们如何读取blob的内容?像文件流? –