了解如何使用适用于 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,需要安装三个模块。
-
azure-mgmt-resource
模块,包括用于 Active Directory 的 Azure 模块,等等。 -
azure-mgmt-datalake-store
模块,包括 Azure Data Lake Storage Gen1 帐户管理操作。 有关本模块的详细信息,请参阅 Azure Data Lake Storage Gen1 管理模块参考。 -
azure-datalake-store
模块,包括 Azure Data Lake Storage Gen1 文件系统操作。 有关此模块的详细信息,请参阅 azure-datalake-store 文件系统模块参考。
使用以下命令安装这些模块。
pip install azure-identity
pip install azure-mgmt-resource
pip install azure-mgmt-datalake-store
pip install azure-datalake-store
创建新的 Python 应用程序
在所选的 IDE 中,创建新的 Python 应用程序,例如 ,mysample.py。
添加以下代码片段以导入所需的模块:
# 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
将更改保存到 mysample.py。
身份验证
本部分介绍有关使用 Microsoft Entra ID 进行身份验证的不同方式。 可用的选项有:
- 有关应用程序的最终用户身份验证,请参阅 使用 Python 通过 Data Lake Storage Gen1 进行最终用户身份验证。
- 有关应用程序的服务到服务身份验证,请参阅 使用 Python 通过 Data Lake Storage Gen1 进行服务到服务身份验证。
创建客户端和 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)