教學課程:使用 Linux VM 系統指派的受控識別來存取 Azure Data Lake Store

Azure 資源的受控識別是 Microsoft Entra ID 的功能。 每個支援適用於 Azure 資源的受控識別 Azure 服務均受限於其本身的時間表。 開始之前,請務必檢閱 資源受控識別的可用性 狀態和 已知問題

本教學課程說明如何使用 Linux 虛擬機器 (VM) 的系統指派受控識別來存取 Azure Data Lake Store。 您將學習如何:

在本教學課程中,您會了解如何:

  • 將您的 VM 存取權授與 Azure Data Lake Store。
  • 使用 VM 系統指派的受控識別來存取 Azure Data Lake Store,以取得存取權杖。

必要條件

授予存取權

本節說明如何將 VM 存取權授與 Azure Data Lake Store 中的檔案和資料夾。 在此步驟中,您可以使用現有的 Data Lake Store 實例或建立新的實例。 若要使用 Azure 入口網站 建立 Data Lake Store 實例,請遵循 Azure Data Lake Store 快速入門 。 Azure Data Lake Store 檔中 也有使用 Azure CLI 和 Azure PowerShell 的 快速入門。

在 Data Lake Store 中,建立新的資料夾,並授與 Linux VM 系統指派的受控識別許可權,以讀取、寫入和執行該資料夾中的檔案:

  1. 在Azure 入口網站中,選取 左窗格中的 [Data Lake Store ]。
  2. 選取您想要使用的 Data Lake Store 實例。
  3. 選取 命令列上的 [資料總 管]。
  4. 已選取 Data Lake Store 實例的根資料夾。 選取 命令列上的 [存取 ]。
  5. 選取新增。 在 [ 選取 ] 方塊中,輸入您的 VM 名稱--例如 DevTestVM 。 從搜尋結果中選取您的 VM,然後選取 [ 選取 ]。
  6. 選取 [ 選取許可權 ]。 選取 [讀取 ] 和 [ 執行 ],新增至 [此資料夾 ],並僅 新增為 [存取權限]。 選取 [確定]。 應該成功新增許可權。
  7. 關閉 [存取 ] 窗格。
  8. 在本教學課程中,建立新的資料夾。 選取 命令列上的 [新增資料夾 ],然後為新資料夾指定名稱,例如 TestFolder 。 選取 [確定]
  9. 選取您建立的資料夾,然後選取 命令列上的 [存取 ]。
  10. 類似于步驟 5,請選取 [ 新增 ]。 在 [ 選取] 方塊中,輸入 VM 的名稱。 從搜尋結果中選取您的 VM,然後選取 [ 選取 ]。
  11. 類似于步驟 6,請選取 [ 選取許可權 ]。 選取 [讀取 ]、 [寫入 ] 和 [執行 ],新增至 [此資料夾 ],然後新增為 [存取權限專案] 和 [預設許可權專案 ]。 選取 [確定]。 應該成功新增許可權。

Azure 資源的受控識別現在可以對您所建立資料夾中的檔案執行所有作業。 如需管理 Data Lake Store 存取權的詳細資訊,請參閱 Data Lake Store 中的存取控制。

取得存取權杖

本節說明如何取得存取權杖並呼叫 Data Lake Store 檔案系統。 Azure Data Lake Store 原生支援 Microsoft Entra 驗證,因此可以直接接受透過使用 Azure 資源的受控識別取得的存取權杖。 若要向 Data Lake Store 檔案系統進行驗證,您可以將 Microsoft Entra ID 所發出的存取權杖傳送至 Data Lake Store 檔案系統端點。 存取權杖的格式為 「Bearer < ACCESS_TOKEN_VALUE > 」。 若要深入瞭解 Data Lake Store 對 Microsoft Entra 驗證的支援,請參閱 使用 Microsoft Entra 識別碼 向 Data Lake Store 進行驗證。

在本教學課程中,您會使用 cURL 對 Data Lake Store 檔案系統的 REST API 進行驗證,以提出 REST 要求。

注意

Data Lake Store 檔案系統的用戶端 SDK 尚不支援 Azure 資源的受控識別。

若要完成這些步驟,您需要 SSH 用戶端。 如果您使用 Windows,您可以在 Windows 子系統 Linux 版 中使用 SSH 用戶端。 如果您需要設定 SSH 用戶端金鑰的協助,請參閱 如何在 Azure 上搭配 Windows 使用 SSH 金鑰,或 如何建立和使用 Azure 中 Linux VM 的 SSH 公開和私密金鑰組。

  1. 在入口網站中,流覽至您的 Linux VM。 在 [概觀] 中 ,選取 [連線 ]。

  2. 使用您選擇的 SSH 用戶端連線至 VM。

  3. 在終端機視窗中,使用 cURL 向 Azure 資源端點的本機受控識別提出要求,以取得 Data Lake Store 檔案系統的存取權杖。 Data Lake Store 的資源識別碼為 https://datalake.azure.net/ 。 請務必在資源識別碼中包含尾端斜線。

    curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fdatalake.azure.net%2F' -H Metadata:true   
    

    成功的回應會傳回您用來向 Data Lake Store 進行驗證的存取權杖:

    {"access_token":"eyJ0eXAiOiJ...",
     "refresh_token":"",
     "expires_in":"3599",
     "expires_on":"1508119757",
     "not_before":"1508115857",
     "resource":"https://datalake.azure.net/",
     "token_type":"Bearer"}
    
  4. 藉由使用 cURL,向 Data Lake Store 檔案系統的 REST 端點提出要求,以列出根資料夾中的資料夾。 這是檢查所有專案是否正確設定的簡單方式。 從上一個步驟複製存取權杖的值。 授權標頭中的字串 「Bearer」 必須有大寫 「B」。您可以在 Azure 入口網站 的 Data Lake Store 窗格 [概觀] 區段中 ,找到 Data Lake Store 實例 的名稱。

    curl https://<YOUR_ADLS_NAME>.azuredatalakestore.net/webhdfs/v1/?op=LISTSTATUS -H "Authorization: Bearer <ACCESS_TOKEN>"
    

    成功的回覆看起來像這樣:

    {"FileStatuses":{"FileStatus":[{"length":0,"pathSuffix":"TestFolder","type":"DIRECTORY","blockSize":0,"accessTime":1507934941392,"modificationTime":1508105430590,"replication":0,"permission":"770","owner":"bd0e76d8-ad45-4fe1-8941-04a7bf27f071","group":"bd0e76d8-ad45-4fe1-8941-04a7bf27f071"}]}}
    
  5. 現在,您可以嘗試將檔案上傳至 Data Lake Store 實例。 首先,建立要上傳的檔案。

    echo "Test file." > Test1.txt
    
  6. 藉由使用 cURL,向 Data Lake Store 檔案系統的 REST 端點提出要求,以將檔案上傳至您稍早建立的資料夾。 上傳牽涉到重新導向,而 cURL 會自動遵循重新導向。

    curl -i -X PUT -L -T Test1.txt -H "Authorization: Bearer <ACCESS_TOKEN>" 'https://<YOUR_ADLS_NAME>.azuredatalakestore.net/webhdfs/v1/<FOLDER_NAME>/Test1.txt?op=CREATE' 
    

    成功的回覆看起來像這樣:

    HTTP/1.1 100 Continue
    HTTP/1.1 307 Temporary Redirect
    Cache-Control: no-cache, no-cache, no-store, max-age=0
    Pragma: no-cache
    Expires: -1
    Location: https://mytestadls.azuredatalakestore.net/webhdfs/v1/TestFolder/Test1.txt?op=CREATE&write=true
    x-ms-request-id: 756f6b24-0cca-47ef-aa12-52c3b45b954c
    ContentLength: 0
    x-ms-webhdfs-version: 17.04.22.00
    Status: 0x0
    X-Content-Type-Options: nosniff
    Strict-Transport-Security: max-age=15724800; includeSubDomains
    Date: Sun, 15 Oct 2017 22:10:30 GMT
    Content-Length: 0
    
    HTTP/1.1 100 Continue
    
    HTTP/1.1 201 Created
    Cache-Control: no-cache, no-cache, no-store, max-age=0
    Pragma: no-cache
    Expires: -1
    Location: https://mytestadls.azuredatalakestore.net/webhdfs/v1/TestFolder/Test1.txt?op=CREATE&write=true
    x-ms-request-id: af5baa07-3c79-43af-a01a-71d63d53e6c4
    ContentLength: 0
    x-ms-webhdfs-version: 17.04.22.00
    Status: 0x0
    X-Content-Type-Options: nosniff
    Strict-Transport-Security: max-age=15724800; includeSubDomains
    Date: Sun, 15 Oct 2017 22:10:30 GMT
    Content-Length: 0
    

藉由使用 Data Lake Store 檔案系統的其他 API,您可以附加至檔案、下載檔案等等。

下一步

在本教學課程中,您已瞭解如何使用 Linux VM 系統指派的受控識別來存取 Azure Data Lake Store。 若要深入瞭解 Azure Data Lake Store,請參閱: