我已经创建了python代码来在Azure存储容器中创建一系列文件夹和子文件夹(用于data lake)。该代码有效,并且基于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))
仅添加一些上下文,在Blob存储中不可能这样做的原因是文件夹/目录不是“真实的”。文件夹不作为独立对象存在,它们仅定义为blob名称的一部分
例如,如果您有一个文件夹“mystuff”,其中包含一个文件(blob)“somefile.txt”,则blob名称实际上包括文件夹名称和“/”字符,如mystuff/somefile.txt。blob直接存在于容器中,而不是文件夹中。此命名约定可以多次嵌套在blob名称中,如folder1/folder2/mystuff/anotherfolder/somefile.txt,但该blob仍然直接存在于容器中
文件夹可能会出现在某些工具中(如Azure Storage Explorer),因为SDK允许blob名称筛选:如果使用“/”字符进行筛选,则可以模拟文件夹及其内容的外观。但为了让文件夹看起来存在,容器中必须有具有适当名称的blob。如果您想要“强制”一个文件夹存在,您可以在名称中创建一个具有正确文件夹路径的0字节blob,但是blob工件仍然需要存在
例外情况是Azure Data Lake Storage (ADLS) Gen 2,它是实现Hierarchical Namespace的Blob存储。这使它更像一个文件系统,因此尊重目录作为独立对象的概念。ADL是建立在Blob存储上的,因此两者之间存在很多奇偶性。如果绝对必须有空目录,那么ADL就是一种选择
不,对于blob存储,这是不可能的。没有办法创建所谓的“文件夹”
但您可以像这样使用data lake SDK来创建目录:
相关问题 更多 >
编程相关推荐