Я добавляю пример кода для перебора структуры контейнера и папки и, наконец, загружаю файлы 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)
Также вы упомянули о загрузке файла с помощью Azure cli, поэтому я пишу пример синтаксиса для этого
az storage file download \
--account-name $STORAGEACCT \
--account-key $STORAGEKEY \
--share-name "myshare" \
--path "myDirectory/SampleUpload.txt" \
--dest "~/clouddrive/SampleDownload.txt"
Пример кода
# Create a directory to store all the blobs
mkdir /downloaded-container && cd /downloaded-container
# Get all the blobs
BLOBS=$(az storage blob list -c $CONTAINER \
--account-name $ACCOUNT_NAME --sas-token "$SAS_TOKEN" \
--query [*].name --output tsv)
# Download each one
for BLOB in $BLOBS
do
echo "********Downloading $BLOB"
az storage blob download -n $BLOB -f $BLOB -c $CONTAINER --account-name $ACCOUNT_NAME --sas-token "$SAS_TOKEN"
done
Дополнительную информацию см. Ниже: doc
https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/storage/files/storage-how-to-use-files-cli.md