本文介绍如何使用 Azure CLI 在具有分层命名空间的存储帐户中创建和管理目录与文件。
本文中所有示例均假设存储账户启用了层级命名空间,并使用 Microsoft Entra 授权(--auth-mode login)。
先决条件
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 命令将容器的属性打印到控制台。
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 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 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 命令从目录下载文件。
此示例从名为 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 file list 命令来列出目录内容。
此示例列出名为 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