Python を使用して Microsoft OneLake のファイルとフォルダーを管理する

この記事では、Azure Storage Python SDK を使用して OneLake 内のファイルとディレクトリを管理する方法について説明します。 このチュートリアルでは、「Python を使用して Azure Data Lake Storage Gen2 でディレクトリとファイルを管理する」と同じ内容について取り上げ、OneLake に接続するときの違いを強調して説明します。

前提条件

プロジェクトを開始する前に、次の前提条件を満たしていることを確認してください。

  • 共同作成者のアクセス許可を持つ Fabric テナント内のワークスペース。
  • ワークスペース内のレイクハウス。 必要に応じて、Python を使用して読み取るためにデータを事前に読み込みます。

プロジェクトの設定

プロジェクト ディレクトリから、Azure Data Lake Storage と Azure ID のクライアント ライブラリのパッケージをインストールします。 OneLake は Azure Data Lake Storage (ADLS) Gen2 と同じ SDK をサポートしており、azure-identity パッケージによって提供される 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 の 'Connect-AzAccount' コマンドレットを使用する方法があります。

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 for Python を使用して 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