使用 Azure CLI 管理 Azure Data Lake Storage 中的目錄和檔案

本文說明如何使用 Azure CLI,在具有階層命名空間的儲存體帳戶中建立及管理目錄和檔案。

本文中所有範例皆假設儲存帳號啟用階層命名空間,並使用 Microsoft Entra 授權(--auth-mode login)。

範例 | 提供意見反應

必要條件

  • Azure 訂用帳戶。 如需詳細資訊,請參閱取得 Azure 免費試用。

  • 已啟用階層命名空間的儲存體帳戶。 遵循下列指示以建立帳戶。

  • Azure CLI 版本 2.6.0 或更新版本。

請確保您已安裝正確版本的 Azure CLI

  1. 開啟 Azure Cloud Shell,或者如果你在本地安裝了 Azure CLI,也可以開啟像 Windows PowerShell 這樣的指令控制台應用程式。

  2. 請使用以下指令確認您安裝的 Azure CLI 版本為 2.6.0 或更新版本。

     az --version
    

    如果您的 Azure CLI 版本早於 2.6.0,請安裝更新版本。 如需詳細資訊,請參閱 安裝 Azure CLI。

連線到帳戶

  1. 如果您在本機使用 Azure CLI,請執行登入命令。

    az login
    

    如果 CLI 能開啟預設瀏覽器,它會打開瀏覽器並載入 Azure 登入頁面。

    否則,請在 https://aka.ms/devicelogin 中開啟瀏覽器頁面,並輸入顯示在您的終端機中的授權碼。 然後,請在瀏覽器中使用您的帳戶認證登入。

    若要深入了解不同的驗證方法,請參閱使用 Azure CLI 授與 Blob 或佇列資料的存取權。

  2. 如果您的身分識別與多個訂用帳戶有關聯,且沒有收到要選取訂用帳戶的提示,請將您的作用中訂用帳戶設定為您想運作之儲存體帳戶的訂用帳戶。 在此範例中,使用訂用帳戶識別碼取代 <subscription-id> 預留位置值。

    az account set --subscription <subscription-id>
    

    使用訂閱識別碼取代 <subscription-id> 預留位置值。

附註

本文範例展示了 Microsoft Entra 授權。 若要深入了解授權方法,請參閱使用 Azure CLI 授與 Blob 或佇列資料的存取權。

建立容器

容器可作為您檔案的檔案系統。 用 az storage fs create 指令建立一個。

此範例會建立名為 my-file-system 的容器。

az storage fs create -n my-file-system --account-name mystorageaccount --auth-mode login

要驗證容器的存在,請執行 az storage fs show 指令。

顯示容器屬性

使用 az storage fs show 指令,將容器的屬性列印到主控台。

az storage fs show -n my-file-system --account-name mystorageaccount --auth-mode login

列出容器內容

使用 az storage fs file list 指令來列出容器的內容。

此範例會列出名為 my-file-system 的容器所含的內容。

az storage fs file list -f my-file-system --account-name mystorageaccount --auth-mode login

刪除容器

使用 az storage fs delete 指令來刪除容器。

此範例會刪除名為 my-file-system 的容器。

az storage fs delete -n my-file-system --account-name mystorageaccount --auth-mode login

建立目錄

使用 az storage fs directory create 指令來建立目錄參考。

此範例將名為 my-directory 的目錄新增至名為 my-file-system 的容器 (位於名為 mystorageaccount 的帳戶中)。

az storage fs directory create -n my-directory -f my-file-system --account-name mystorageaccount --auth-mode login

要驗證該目錄是否存在,請執行 az storage fs 目錄 exists 指令。

顯示目錄屬性

使用 az storage fs directory show 指令,將目錄的屬性印到主控台。

az storage fs directory show -n my-directory -f my-file-system --account-name mystorageaccount --auth-mode login

重新命名目錄或移動目錄

使用 az storage fs directory move 命令重新命名或移動目錄。

此範例會將相同容器中名稱為 my-directory 的目錄重新命名為名稱 my-new-directory。

az storage fs directory move -n my-directory -f my-file-system --new-directory "my-file-system/my-new-directory" --account-name mystorageaccount --auth-mode login

此範例會將目錄移至名為 my-second-file-system 的容器。

az storage fs directory move -n my-directory -f my-file-system --new-directory "my-second-file-system/my-new-directory" --account-name mystorageaccount --auth-mode login

刪除目錄

使用 az storage fs directory delete 命令刪除 目錄。

此範例刪除名為 my-directory 的目錄。

az storage fs directory delete -n my-directory -f my-file-system --account-name mystorageaccount --auth-mode login

檢查目錄是否存在

透過 az storage fs 目錄 exists 指令判斷容器中是否存在特定目錄。

此範例顯示名為 my-directory 的目錄是否存在於 my-file-system 容器中。

az storage fs directory exists -n my-directory -f my-file-system --account-name mystorageaccount --auth-mode login

從目錄下載

使用 az storage fs file download 命令,從目錄下載 檔案。

此範例從名為 upload.txt 的目錄下載名為 my-directory 的檔案。

az storage fs file download -p my-directory/upload.txt -f my-file-system -d "C:\myFolder\download.txt" --account-name mystorageaccount --auth-mode login

列出目錄內容

使用 az storage fs 檔案列表 指令來列出目錄內容。

此範例列出名為 my-directory 的目錄內容 (位於儲存體帳戶 my-file-system 的 mystorageaccount 容器中)。

az storage fs file list -f my-file-system --path my-directory --account-name mystorageaccount --auth-mode login

將檔案上傳至目錄

使用 az storage fs file upload 命令將檔案上傳 至目錄。

此範例將名為 upload.txt 的檔案上傳至名為 my-directory 的目錄。

az storage fs file upload -s "C:\myFolder\upload.txt" -p my-directory/upload.txt -f my-file-system --account-name mystorageaccount --auth-mode login

要確認檔案已上傳,請執行 az storage fs file list 指令。

顯示檔案屬性

使用 az storage fs file show 指令,將檔案的屬性列印到主控台。

az storage fs file show -p my-file.txt -f my-file-system --account-name mystorageaccount --auth-mode login

重新命名檔案或移動檔案

使用 az storage fs file move 命令重新命名或移動檔案。

此範例會將名稱為 my-file.txt 的檔案重新命名為 my-file-renamed.txt。

az storage fs file move -p my-file.txt -f my-file-system --new-path my-file-system/my-file-renamed.txt --account-name mystorageaccount --auth-mode login

刪除檔案

使用 az storage fs file delete 命令刪除 檔案。

此範例刪除了一個名為 my-file.txt的檔案。

az storage fs file delete -p my-directory/my-file.txt -f my-file-system --account-name mystorageaccount --auth-mode login

另請參閱