【问题标题】:How to create directories in Azure storage container without creating extra files?如何在 Azure 存储容器中创建目录而不创建额外文件?
【发布时间】:2021-04-30 21:27:00
【问题描述】:

我创建了 Python 代码来在 Azure 存储容器中创建一系列文件夹和子文件夹(用于数据湖)。该代码有效并且基于 Microsoft Azure 上的文档。但有一件事是我在文件夹中创建了一个虚拟的“txt”文件以创建目录(我可以稍后清理)。我想知道是否有一种方法可以在不创建文件的情况下创建文件夹和子文件夹。我了解 Azure 容器存储中的文件夹不是分层的,而是元数据,我所要求的可能是不可能的?

connection_string = config['azure_storage_connectionstring']
gen2_container_name = config['gen2_container_name']
container_client = ContainerClient.from_connection_string(connection_string, gen2_container_name)
blob_service_client = BlobServiceClient.from_connection_string(connection_string)

# blob_service_client.create_container(gen2_container_name)


def create_folder(folder, sub_folder):
    blob_client = container_client.get_blob_client('{}/{}/start_here.txt'.format(folder, sub_folder)) 

    with open ('test.txt', 'rb') as data:
        blob_client.upload_blob(data)



def create_all_folders():
    config = load_config()
    folder_list = config['folder_list']
    sub_folder_list = config['sub_folder_list']
    for folder in folder_list:
        for sub_folder in sub_folder_list:
            try:
                create_folder(folder, sub_folder)
            except Exception as e:
                print ('Looks like something went wrong here trying to create this folder structure {}/{}. Maybe the structure already exists?'.format(folder, sub_folder))

【问题讨论】:

  • 对于 storage sdk 是不可能的,你可以使用 datalake sdk。

标签: python azure azure-storage azure-blob-storage


【解决方案1】:

我创建了 python 代码来创建一系列文件夹和子文件夹 (用于数据湖)在 Azure 存储容器中。该代码有效并且是 基于 Microsoft Azure 上的文档。不过有一件事是 我正在文件夹中创建一个虚拟的“txt”文件以创建 目录(我可以稍后清理)。我想知道是否有 一种无需创建文件即可创建文件夹和子文件夹的方法。一世 了解 Azure 容器存储中的文件夹不是 分层的,而是元数据,我要求的可能不是 有可能吗?

不,对于 blob 存储,这是不可能的。没有办法创建所谓的“文件夹”

但是你可以像这样使用data-lake SDK来创建目录:

from azure.storage.filedatalake import DataLakeServiceClient 
connect_str = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net"
datalake_service_client = DataLakeServiceClient.from_connection_string(connect_str)
myfilesystem = "test"
myfolder     = "test1111111111"
myfile       = "FileName.txt"

file_system_client = datalake_service_client.get_file_system_client(myfilesystem)            
directory_client = file_system_client.create_directory(myfolder)    

【讨论】:

    【解决方案2】:

    只是为了添加一些上下文,在 Blob 存储中不可能做到这一点的原因是文件夹/目录不是“真实的”。文件夹不作为独立对象存在,它们仅被定义为 blob 名称的一部分。

    例如,如果您有一个文件夹“mystuff”,其中包含一个文件(blob)“somefile.txt”,则该 blob 名称实际上包括文件夹名称和“/”字符,如 mystuff/somefile.txt时间>。 Blob 直接存在于容器内,而不是文件夹内。这种命名约定可以多次嵌套在像 folder1/folder2/mystuff/anotherfolder/somefile.txt 这样的 blob 名称中,但该 blob 仍然只直接存在于容器中。

    文件夹可能会出现在某些工具中(例如Azure Storage Explorer),因为 SDK 允许对 blob 名称进行过滤:如果您在“/”字符上这样做,您可以模仿文件夹及其内容的外观。但是为了使文件夹看起来存在,容器中必须有具有适当名称的 blob。如果您想“强制”一个文件夹存在,您可以创建一个名称中包含正确文件夹路径的 0 字节 blob,但该 blob 工件仍然需要存在。

    例外是Azure Data Lake Storage (ADLS) Gen 2,它是实现Hierarchical Namespace 的Blob 存储。这使它更像一个文件系统,因此尊重目录作为独立对象的概念。 ADLS 是建立在 Blob 存储之上的,因此两者之间有很多奇偶性。如果您绝对必须有空目录,那么 ADLS 就是要走的路。

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2019-04-29
      • 2011-02-26
      • 2017-12-28
      • 1970-01-01
      相关资源
      最近更新 更多