python - 如何使用python从容器中下载所有blob,其中blob是子目录样式

标签 python azure azure-functions azure-blob-storage

下面的代码将通过提供 blob 名称来下载特定的 blob

import constants
import os
import tempfile
from azure.storage.blob import BlobServiceClient

temp_dir = tempfile.TemporaryDirectory()
print(temp_dir.name)
Local_path = os.path.join(temp_dir.name, constants.BLOB_NAME)


class AzureBlob:
    def __init__(self, CONNECTION_STRING, BLOB_CONTAINER,
                 BLOB_PATH, BLOB_NAME):
        self.blob_service_client = self.activate_blob_service()
        self.container_client = self.initialize_container()
        self.BLOB_CONTAINER = BLOB_CONTAINER
        self.CONNECTION_STRING = CONNECTION_STRING
        self.BLOB_PATH = BLOB_PATH
        self.BLOB_NAME = BLOB_NAME

        # Initialize a BlobServiceClient object

    def activate_blob_service(self):
        self.blob_service_client = BlobServiceClient.from_connection_string(self.CONNECTION_STRING)
        # print(self.CONNECTION_STRING)
        return self.blob_service_client

        # Initialize a container from its name

    def initialize_container(self):
        self.container_client = self.blob_service_client.get_container_client(self.BLOB_CONTAINER)
        # print(container_client)
        return self.container_client

        # Download Blob to local

    def download_file(self):
        with open(Local_path, 'wb+') as f:
            f.write(self.container_client.download_blob(os.path.join(self.BLOB_PATH, self.BLOB_NAME)).readall())
        return Local_path


# AzureBlob().download_file()
a = AzureBlob(constants.CONNECTION_STRING, constants.BLOB_CONTAINER,
              constants.BLOB_PATH, constants.BLOB_NAME)

我实际上想要实现的是从 blob 位于子目录中的容器中下载所有 blob。我将提供 blob 的目录路径,并且我需要下载该目录内的所有信息。

最佳答案

要实现上述要求,您可以尝试以下解决方法从容器中下载所有文件,

# download_blobs.py
# Python program to bulk download blob files from azure storage
# Uses latest python SDK() for Azure blob storage
# Requires python 3.6 or above
import os
from azure.storage.blob import BlobServiceClient, BlobClient
from azure.storage.blob import ContentSettings, ContainerClient
 
# IMPORTANT: Replace connection string with your storage account connection string
# Usually starts with DefaultEndpointsProtocol=https;...
MY_CONNECTION_STRING = "REPLACE_THIS"
 
# Replace with blob container
MY_BLOB_CONTAINER = "myimages"
 
# Replace with the local folder where you want files to be downloaded
LOCAL_BLOB_PATH = "REPLACE_THIS"
 
class AzureBlobFileDownloader:
  def __init__(self):
    print("Intializing AzureBlobFileDownloader")
 
    # Initialize the connection to Azure storage account
    self.blob_service_client =  BlobServiceClient.from_connection_string(MY_CONNECTION_STRING)
    self.my_container = self.blob_service_client.get_container_client(MY_BLOB_CONTAINER)
 
 
  def save_blob(self,file_name,file_content):
    # Get full path to the file
    download_file_path = os.path.join(LOCAL_BLOB_PATH, file_name)
 
    # for nested blobs, create local path as well!
    os.makedirs(os.path.dirname(download_file_path), exist_ok=True)
 
    with open(download_file_path, "wb") as file:
      file.write(file_content)
 
  def download_all_blobs_in_container(self):
    my_blobs = self.my_container.list_blobs()
    for blob in my_blobs:
      print(blob.name)
      bytes = self.my_container.get_blob_client(blob).download_blob().readall()
      self.save_blob(blob.name, bytes)
 
# Initialize class and upload files
azure_blob_file_downloader = AzureBlobFileDownloader()
azure_blob_file_downloader.download_all_blobs_in_container()

欲了解更多信息,请参阅此blog post & SO THREAD

关于python - 如何使用python从容器中下载所有blob,其中blob是子目录样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71968805/

相关文章:

Python Django easy-maps 不呈现谷歌地图

Python图像旋转角度计算

Azure B2C 日期选择器(日历)

python - 使用 websockets 和 asyncio 监听多个套接字

python - 如何根据特定条件转换和创建具有0和1的pandas列

azure - Ansible Azure 信用

c# - 将设备添加到 Azure IoT 中心时出现协议(protocol)版本无效错误

azure - 在 Visual Studio 2017 中自动生成 OpenAPI json 文件

azure - 使用 Azure Functions 和存储帐户的托管身份

azure - 在本地运行 Azure Functions 时使用 stub