透過 Azure CLI 管理 Azure Data Lake Storage Gen2 中的目錄和檔案

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

若要了解如何取得、設定及更新目錄和檔案的存取控制清單 (ACL),請參閱使用 Azure CLI 管理 Azure Data Lake Storage Gen2 中的 ACL

範例 | 提供意見反應

必要條件

  • 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. 如果您的身分識別與多個訂閱相關聯,請將您的使用中訂閱設為將代管靜態網站的儲存體帳戶的訂閱。

    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 -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 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 directory 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 命令,從目錄下載檔案。

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

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 file list 命令,列出目錄的內容。

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

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 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

另請參閱