使用 Python 对 Azure Data Lake Storage Gen1 进行的帐户管理操作

了解如何使用 Python SDK for Azure Data Lake Storage Gen1 执行基本帐户管理操作(例如,创建 Data Lake Storage Gen1 帐户、列出 Data Lake Storage Gen1 帐户,等等)。有关如何使用 Python 对 Data Lake Storage Gen1 执行文件系统操作的说明,请参阅使用 Python 对 Data Lake Storage Gen1 执行的文件系统操作

先决条件

安装模块

若要通过 Python 使用 Data Lake Storage Gen1,需要安装三个模块。

使用以下命令安装这些模块。

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

创建新的 Python 应用程序

  1. 在所选的 IDE 中创建新的 Python 应用程序,例如,mysample.py

  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)

后续步骤

另请参阅