了解如何使用適用於 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 Management module reference (Azure Data Lake Storage Gen1 管理模組參考)。 -
azure-datalake-store
模組,含有 Azure Data Lake Storage Gen1 檔案系統作業。 如需關於此模組的詳細資訊,請參閱 azure-datalake-store Filesystem module reference (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)