共用方式為


使用 Python 管理 Microsoft OneLake 中的檔案和資料夾

本文說明如何使用 Azure 儲存體 Python SDK 來管理 OneLake 中的檔案和目錄。 本逐步解說涵蓋與 使用 Python 來管理 ADLS Gen2 中的目錄和檔案相同的內容,並醒目提示連線到 OneLake 時的差異。

必要條件

開始專案之前,請確定您有下列必要條件:

  • 您 Fabric 租使用者中具有參與者許可權的工作區。
  • 工作區中的 Lakehouse。 或者,讓數據預先載入以使用 Python 讀取。

設定您的專案

從您的項目目錄中,安裝 Azure Data Lake 儲存體 和 Azure 身分識別客戶端連結庫的套件。 OneLake 支援與 Azure Data Lake 儲存體 (ADLS) Gen2 相同的 SDK,並支援 Azure 身分識別套件所提供的 Microsoft Entra 驗證。

pip install azure-storage-file-datalake azure-identity

接下來,將必要的 import 語句新增至您的程式代碼檔案:

import os
from azure.storage.filedatalake import (
    DataLakeServiceClient,
    DataLakeDirectoryClient,
    FileSystemClient
)
from azure.identity import DefaultAzureCredential

授權存取 OneLake

下列範例會建立連線至 OneLake 的服務用戶端,讓您可用來建立文件系統用戶端以進行其他作業。 為了向 OneLake 進行驗證,此範例會使用 DefaultAzureCredential 來自動偵測認證並取得正確的驗證令牌。 提供 Azure SDK 認證的常見方法包括使用 Azure 命令行介面中的 'az login' 命令,或 Azure PowerShell 中的 '連線-AzAccount' Cmdlet。

def get_service_client_token_credential(self, account_name) -> DataLakeServiceClient:
    account_url = f"https://{account_name}.dfs.fabric.microsoft.com"
    token_credential = DefaultAzureCredential()

    service_client = DataLakeServiceClient(account_url, credential=token_credential)

    return service_client

若要深入瞭解如何使用DefaultAzureCredential來授權存取數據,請參閱 概觀:使用 Azure SDK 向 Azure 驗證 Python 應用程式。

使用目錄

若要在 OneLake 中使用目錄,請建立檔案系統客戶端和目錄用戶端。 您可以使用此目錄用戶端來執行各種作業,包括重新命名、移動或列出路徑(如下列範例所示)。 您也可以使用 FileSystemClient.create_directory 方法建立目錄時,建立目錄用戶端。

def create_file_system_client(self, service_client, file_system_name: str) : DataLakeServiceClient) -> FileSystemClient:
    file_system_client = service_client.get_file_system_client(file_system = file_system_name)
    return file_system_client

def create_directory_client(self, file_system_client : FileSystemClient, path: str) -> DataLakeDirectoryClient: directory_client 
    directory_client = file_system_client.GetDirectoryClient(path)
    return directory_client


def list_directory_contents(self, file_system_client: FileSystemClient, directory_name: str):
    paths = file_system_client.get_paths(path=directory_name)

    for path in paths:
        print(path.name + '\n')

上傳檔案

您可以使用 DataLakeFileClient.upload_data 方法,將內容上傳至新的或現有的檔案

def upload_file_to_directory(self, directory_client: DataLakeDirectoryClient, local_path: str, file_name: str):
    file_client = directory_client.get_file_client(file_name)

    with open(file=os.path.join(local_path, file_name), mode="rb") as data:
        file_client.upload_data(dataW, overwrite=True)

範例

下列程式代碼範例會列出 OneLake 中任何資料夾的目錄內容。

#Install the correct packages first in the same folder as this file. 
#pip install azure-storage-file-datalake azure-identity

from azure.storage.filedatalake import (
    DataLakeServiceClient,
    DataLakeDirectoryClient,
    FileSystemClient
)
from azure.identity import DefaultAzureCredential

# Set your account, workspace, and item path here
ACCOUNT_NAME = "onelake"
WORKSPACE_NAME = "<myWorkspace>"
DATA_PATH = "<myLakehouse>.Lakehouse/Files/<path>"

def main():
    #Create a service client using the default Azure credential

    account_url = f"https://{ACCOUNT_NAME}.dfs.fabric.microsoft.com"
    token_credential = DefaultAzureCredential()
    service_client = DataLakeServiceClient(account_url, credential=token_credential)

    #Create a file system client for the workspace
    file_system_client = service_client.get_file_system_client(WORKSPACE_NAME)
    
    #List a directory within the filesystem
    paths = file_system_client.get_paths(path=DATA_PATH)

    for path in paths:
        print(path.name + '\n')

if __name__ == "__main__":
    main()

若要執行此範例,請將上述程式代碼儲存到檔案 listOneLakeDirectory.py 中,並在相同的目錄中執行下列命令。 請記得將工作區和路徑取代為範例中的您自己的值。

python listOneLakeDirectory.py