本文介绍如何使用 Azure CLI 在具有分层命名空间的存储帐户中创建和管理目录与文件。
要了解如何获取、设置和更新目录和文件的访问控制列表 (ACL),请参阅使用 Azure CLI 管理 Azure Data Lake Storage 中的 ACL。
先决条件
- Azure 订阅。 有关详细信息,请参阅获取 Azure 试用版。 
- 一个已启用分层命名空间的存储帐户。 按这些说明创建一个。 
- Azure CLI 版本 - 2.6.0或更高版本。
确保安装正确版本的 Azure CLI
- 如果在本地安装了 Azure CLI,请打开命令控制台应用程序,例如 Windows PowerShell。 
- 使用以下命令验证安装的 Azure CLI 版本是否是 - 2.6.0或更高版本。- az --version- 如果 Azure CLI 版本低于 - 2.6.0,则安装更高版本。 有关详细信息,请参阅安装 Azure CLI。
连接到帐户
- 如果在本地使用 Azure CLI,请运行 login 命令。 - az login- 如果 CLI 可以打开默认浏览器,它将这样做并加载 Azure 登录页。 - 否则,请在 https://aka.ms/deviceloginchina 处打开浏览器页,然后输入终端中显示的授权代码。 然后,在浏览器中使用帐户凭据登录。 - 若要详细了解不同的身份验证方法,请参阅使用 Azure CLI 授权访问 blob 或队列数据。 
- 如果你的标识已关联到多个订阅,且系统未提示你选择订阅,请将活动订阅设置为要对器进行操作的存储帐户的订阅。 在此示例中,请将 - <subscription-id>占位符值替换为你的订阅 ID。- az account set --subscription <subscription-id>- 将 - <subscription-id>占位符值替换为你的订阅 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-file-system 容器中是否存在名为 my-directory 的目录。
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 的目录的内容,该目录位于名为 mystorageaccount 的存储帐户的 my-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