共用方式為


使用 Python 向 Azure Data Lake Storage Gen1 進行用戶驗證

在本文中,您將瞭解如何使用 Python SDK 搭配 Azure Data Lake Storage Gen1 進行用戶驗證。 使用者驗證可以進一步分割成兩個類別:

  • 沒有多重驗證的終端用戶驗證
  • 使用多因素驗證進行用戶驗證

本文將討論這兩個選項。 如需使用 Python 向 Data Lake Storage Gen1 進行服務對服務驗證,請參閱使用 Python 向 Data Lake Storage Gen1 進行服務對服務驗證。

先決條件

安裝模組

若要透過 Python 使用 Data Lake Storage Gen1,您需要安裝三個模組。

使用下列命令來安裝新模組。

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

建立新的 Python 應用程式

  1. 在您選擇的 IDE 中,建立新的 Python 應用程式,例如,mysample.py

  2. 新增下列代碼段以匯入必要的模組

    ## Use this for Azure AD authentication
    from msrestazure.azure_active_directory import AADTokenCredentials
    
    ## Required for Azure Data Lake Storage Gen1 account management
    from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient
    from azure.mgmt.datalake.store.models import DataLakeStoreAccount
    
    ## Required for Azure 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, pprint, uuid, time
    
  3. 將變更儲存至 mysample.py

使用多重要素驗證進行終端用戶驗證

用於帳戶管理

使用下列程式碼片段對 Data Lake Storage Gen1 帳戶進行帳戶管理操作的 Microsoft Entra ID 驗證。 下列代碼段可用來使用多重要素驗證來驗證您的應用程式。 為現有的Microsoft Entra ID 原生 應用程式提供下列值。

authority_host_url = "https://login.microsoftonline.com"
tenant = "FILL-IN-HERE"
authority_url = authority_host_url + '/' + tenant
client_id = 'FILL-IN-HERE'
redirect = 'urn:ietf:wg:oauth:2.0:oob'
RESOURCE = 'https://management.core.windows.net/'

context = adal.AuthenticationContext(authority_url)
code = context.acquire_user_code(RESOURCE, client_id)
print(code['message'])
mgmt_token = context.acquire_token_with_device_code(RESOURCE, code, client_id)
armCreds = AADTokenCredentials(mgmt_token, client_id, resource = RESOURCE)

針對文件系統作業

請使用此工具來透過 Microsoft Entra ID 驗證 Data Lake Storage Gen1 帳戶上的檔案系統操作。 下列代碼段可用來使用多重要素驗證來驗證您的應用程式。 提供下列值以供目前的 Microsoft Entra ID 原生 應用程式使用。

adlCreds = lib.auth(tenant_id='FILL-IN-HERE', resource = 'https://datalake.azure.net/')

缺乏多重因素驗證的最終用戶驗證

這已被取代。 如需詳細資訊,請參閱 Azure 驗證使用 Python SDK

後續步驟

在本文中,您已瞭解如何使用 Python 使用使用者驗證向 Azure Data Lake Storage Gen1 進行驗證。 您現在可以查看下列文章,討論如何使用 Python 來處理 Azure Data Lake Storage Gen1。