簡體   English   中英

如何列出另一個訂閱中的另一個 Azure 數據湖 gen2 存儲帳戶中的所有文件和子目錄

[英]How to list all files and subdirectories inside another Azure Data lake gen2 storage account which is in different subscription

我正在嘗試從不同訂閱的 Azure 存儲帳戶中的容器中獲取所有文件及其子目錄,並且業務要求是使用 abfss url。 abfss://@.dfs.core.windows.net//。 我嘗試為訂閱導入 spark 配置,並使用以下代碼返回文件列表。 然而失敗了。

import os
from fnmatch import fnmatch
root_list="abfss://staging@bdoibgedpsadlssandbox.dfs.core.windows.net/staging/"
files_list = []
pattern = "*.*"
print(pattern)
for path, subdirs, files in os.walk(root_list):
  for name in files:
    if fnmatch(name.upper(), pattern.upper()):
      files_list.append(path+"/"+name)

這將打印“[]”空列表。

您可以將以下代碼用於此用例。

from pyspark.sql.functions import col
from azure.storage.blob import BlockBlobService
from datetime import datetime
import os.path

account_name='accountname'
container_name ='container_name'
second_conatainer_name ='data'
account_key = 'storage-account-key'
prefix_val = second_conatainer_name+'/'

block_blob_service = BlockBlobService(account_name='%s'%(account_name), account_key='%s'%(account_key))

#block_blob_service.create_container(container_name)
generator = block_blob_service.list_blobs(container_name,prefix="%s"%(prefix_val))
report_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')


Target_file = "/target2/%s.csv" % (container_name)
print(Target_file)

Target_file = open("%s"%(Target_file), 'w')

for blob in generator:
    length = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name)
    last_modified = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.last_modified
    file_size = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.content_length
    blob_type = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.blob_type
    creation_time = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.blob_tier_change_time
    if file_size != 0:
       line = account_name+'|'+container_name+'|'+blob.name+'|'+ str(file_size) +'|'+str(last_modified)[:10]+'|'
       print(line)
       Target_file.write(line+'\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM