使用 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 使用 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)

下一步

另請參閱