次の方法で共有


Python を使用した Azure Data Lake Storage Gen1 に対するアカウント管理操作

Azure Data Lake Storage Gen1 用 Python SDK を使用して、Data Lake Storage Gen1 アカウントの作成、Data Lake Storage Gen1 アカウントの一覧表示などの基本的なアカウント管理操作を実行する方法を説明します。Python を使用して Data Lake Storage Gen1 に対するファイルシステム操作を実行する方法については、Python を使用した Data Lake Storage Gen1 に対するファイルシステム操作に関する記事をご覧ください。

前提条件

  • Python。 Python は、ここからダウンロードできます。 この記事では、Python 3.6.2 を使用します。

  • Azure サブスクリプションAzure 無料試用版の取得に関するページを参照してください。

  • Azure リソース グループ。 手順については、Azure リソース グループの作成に関するページを参照してください。

モジュールをインストールする

Python を使用して Data Lake Storage Gen1 を操作するには、3 つのモジュールをインストールする必要があります。

モジュールをインストールするには、次のコマンドを使用します。

pip install azure-identity
pip install azure-mgmt-resource
pip install azure-mgmt-datalake-store
pip install azure-datalake-store

新しい Python アプリケーションを作成する

  1. 任意の IDE で、mysample.py などの新しい Python アプリケーションを作成します。

  2. 次のスニペットを追加して、必要なモジュールをインポートします。

    # Acquire a credential object for the app identity. When running in the cloud,
    # DefaultAzureCredential uses the app's managed identity (MSI) or user-assigned service principal.
    # When run locally, DefaultAzureCredential relies on environment variables named
    # AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID.
    from azure.identity import DefaultAzureCredential
    
    ## Required for Data Lake Storage Gen1 account management
    from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient
    from azure.mgmt.datalake.store.models import CreateDataLakeStoreAccountParameters
    
    ## Required for Data Lake Storage Gen1 filesystem management
    from azure.datalake.store import core, lib, multithread
    
    # Common Azure imports
    import adal
    from azure.mgmt.resource.resources import ResourceManagementClient
    from azure.mgmt.resource.resources.models import ResourceGroup
    
    # Use these as needed for your application
    import logging, getpass, pprint, uuid, time
    
  3. mysample.py に対する変更を保存します。

認証

このセクションでは、Microsoft Entra ID で認証を行うためのさまざまな方法について説明します。 次の方法を使用できます:

クライアントと Data Lake Storage Gen1 アカウントの作成

次のスニペットは、まず Data Lake Storage Gen1 アカウントのクライアントを作成します。 これは、クライアント オブジェクトを使用して、Data Lake Storage Gen1 アカウントを作成します。 最後に、スニペットは、ファイル システム クライアント オブジェクトを作成します。

## Declare variables
subscriptionId = 'FILL-IN-HERE'
adlsAccountName = 'FILL-IN-HERE'
resourceGroup = 'FILL-IN-HERE'
___location = 'eastus2'
credential = DefaultAzureCredential()

## Create Data Lake Storage Gen1 account management client object
adlsAcctClient = DataLakeStoreAccountManagementClient(credential, subscription_id=subscriptionId)

## Create a Data Lake Storage Gen1 account
adlsAcctResult = adlsAcctClient.accounts.begin_create(
    resourceGroup,
    adlsAccountName,
    CreateDataLakeStoreAccountParameters(
        ___location=___location
    )
)

Data Lake Storage Gen1 アカウントの一覧表示

## List the existing Data Lake Storage Gen1 accounts
result_list_response = adlsAcctClient.accounts.list()
result_list = list(result_list_response)
for items in result_list:
    print(items)

Data Lake Storage Gen1 アカウントの削除

## Delete an existing Data Lake Storage Gen1 account
adlsAcctClient.accounts.begin_delete(resourceGroup, adlsAccountName)

次のステップ

こちらもご覧ください