2016-05-06 16 views
0

コンテナ内のすべてのブロブを一覧表示するためのPythonスクリプトを作成しました。Azureストレージコンテナ内のすべてのブロブをダウンロード

import azure 
from azure.storage.blob import BlobService 
from azure.storage import * 

blob_service = BlobService(account_name='<CONTAINER>', account_key='<ACCOUNT_KEY>') 


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

私がダウンロードしたいブロブだけをリストしていると言いました。 AzureのCLIに移動して、それが私がしたいことを助けることができるかどうかを確認しました。

azure storage blob download [container] 

で1つのブロブをダウンロードできるようになりました。次に、私がpythonスクリプトから取得できるブロブを指定するように求められます。私がこれらのブロブをすべてダウンロードする必要があるのは、上記のコマンドの後にプロンプ​​トにコピーして貼り付けることです。私ができる方法はありますか:

です。コマンドを実行し、プロンプトで次のBLOB名を貼り付けることで、BLOBのリストを反復処理するbashスクリプトを作成します。

b。 PythonスクリプトまたはAzure CLIでコンテナをダウンロードするように指定します。私はコンテナ全体をダウンロードするのを見ていない何かがありますか?

+0

利用可能であるあなたは、 'blob_service.download_blob_to_path'を使用してブロブをダウンロードしようとしたことがありますか?下記の例をご覧ください:https://azure.microsoft.com/en-in/documentation/articles/storage-python-how-to-use-blob-storage/#download-blobs –

答えて

0

現在、1つのAPIを持つコンテナからすべてのブロブを直接ダウンロードすることはできないようです。そして、ブロブで利用可能なすべての操作をhttps://msdn.microsoft.com/en-us/library/azure/dd179377.aspxにすることができます。

このように、BLOBのListGeneratorをリストし、BLOBをループ内でダウンロードできます。例えば:

result = blob_service.list_blobs(container) 
for b in result.items: 
    r = blob_service.get_blob_to_path(container,b.name,"folder/{}".format(b.name)) 

更新

輸入blockblobサービスazure-storage-pythonを使用して:

ゲイリー・劉-MSFTソリューション@

from azure.storage.blob import BlockBlobService

+0

ありがとう!あなたの例で 'container'があるコンテナ名にコンテナ名を追加しましたが、何らかの理由でこのエラーが発生しました:' AttributeError: 'BlobEnumResults'オブジェクトに 'items'属性がありません – privateer35

+0

あなたは[azure- PythonでAzureストレージを処理するには、[azure-storage-python](https:// github)を使用してみてください。[Azure-storage-python](https://github.com/Azure/azure-sdk-for-python) .com/Azure/azure-storage-python)の代わりにSDKを使用してください。そして、インポートBLOBサービスは、もう一つと少し違います、私の更新を参照してください。 –

+0

更新していただきありがとうございます!私は別の問題に遭遇しました。私はAzure SDKをアンインストールしました(必要であればわかりません)。あなたが言ったようにAzure Storage Pythonをインストールしました。私が今持っている唯一のインポートは 'import azure'と' azure.storage.blob import BlockBlob Service'です。私のスクリプトの完全なコードは次のとおりです:http://pastebin.com/HbUygsq3 'python3 script.py'を実行すると、' NameError:name 'BlobService' not defined'というエラーが表示されます。これは、コンテナとアカウントキーでSDKを使用することを定義したときに発生しているようです。 – privateer35

1

が正しいです。私は同じものにいくつかの変更を加えました。今度はコンテナとその中のフォルダ構造を反復できるようになりました(PS - コンテナ内にフォルダはなく、パスのみです)。クライアントに同じディレクトリ構造が存在するかどうかを確認しますそのディレクトリ構造を作成し、そのパスにBLOBをダウンロードします。サブディレクトリが埋め込まれた長いパスをサポートします。

from azure.storage.blob import BlockBlobService 
from azure.storage.blob import PublicAccess 
import os 

#name of your storage account and the access key from Settings->AccessKeys->key1 
block_blob_service = BlockBlobService(account_name='storageaccountname', account_key='accountkey') 

#name of the container 
generator = block_blob_service.list_blobs('testcontainer') 

#code below lists all the blobs in the container and downloads them one after another 
for blob in generator: 
    print(blob.name) 
    print("{}".format(blob.name)) 
    #check if the path contains a folder structure, create the folder structure 
    if "/" in "{}".format(blob.name): 
     print("there is a path in this") 
     #extract the folder path and check if that folder exists locally, and if not create it 
     head, tail = os.path.split("{}".format(blob.name)) 
     print(head) 
     print(tail) 
     if (os.path.isdir(os.getcwd()+ "/" + head)): 
      #download the files to this directory 
      print("directory and sub directories exist") 
      block_blob_service.get_blob_to_path('testcontainer',blob.name,os.getcwd()+ "/" + head + "/" + tail) 
     else: 
      #create the diretcory and download the file to it 
      print("directory doesn't exist, creating it now") 
      os.makedirs(os.getcwd()+ "/" + head, exist_ok=True) 
      print("directory created, download initiated") 
      block_blob_service.get_blob_to_path('testcontainer',blob.name,os.getcwd()+ "/" + head + "/" + tail) 
    else: 
     block_blob_service.get_blob_to_path('testcontainer',blob.name,blob.name) 

同じコードがここにもhttps://gist.github.com/brijrajsingh/35cd591c2ca90916b27742d52a3cf6ba

関連する問題