快速入门:使用 Azure CLI 创建和管理 Azure 文件共享Quickstart: Create and manage Azure file shares using Azure CLI

本指南介绍通过 Azure CLI 来使用 Azure 文件共享的基本知识。This guide walks you through the basics of working with Azure file shares with the Azure CLI. Azure 文件共享与其他文件共享一样,只不过是存储在云中并由 Azure 平台提供支持。Azure file shares are just like other file shares, but stored in the cloud and backed by the Azure platform. Azure 文件共享支持行业标准 SMB 协议,可以跨多个计算机、应用程序和实例进行文件共享。Azure File shares support the industry standard SMB protocol and enable file sharing across multiple machines, applications, and instances.

如果没有 Azure 订阅,可在开始前创建一个 1 元人民币试用帐户If you don't have an Azure subscription, create a 1rmb trial account before you begin.

对于本文中的步骤,必须运行 Azure CLI 2.0.4 或更高版本。For the steps in this article, you must be running Azure CLI version 2.0.4 or later. 若要查找 Azure CLI 版本,请运行 az --versionRun az --version to find your Azure CLI version. 如果需要进行安装或升级,请参阅安装 Azure CLI 2.0If you need to install or upgrade, see Install Azure CLI 2.0.

默认情况下,Azure CLI 命令返回 JavaScript 对象表示法 (JSON)。By default, Azure CLI commands return JavaScript Object Notation (JSON). JSON 是通过 REST API 发送和接收消息的标准方式。JSON is the standard way to send and receive messages from REST APIs. 为了便于处理 JSON 响应,本文中的某些示例使用基于 Azure CLI 命令的 query 参数。To facilitate working with JSON responses, some of the examples in this article use the query parameter on Azure CLI commands. 该参数使用 JMESPath 查询语言来分析 JSON。This parameter uses the JMESPath query language to parse JSON. 若要详细了解如何按照 JMESPath 查询语言的规范来使用 Azure CLI 命令的结果,请参阅 JMESPath tutorial(JMESPath 教程)。To learn more about how to use the results of Azure CLI commands by following the JMESPath query language, see the JMESPath tutorial.

登录 AzureSign in to Azure

如果在本地使用 Azure CLI,请打开一个提示符窗口并登录到 Azure(如果尚未这样做)。If you are using the Azure CLI locally, open a prompt and sign in to Azure if you haven't done so already.

az login

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container in which Azure resources are deployed and managed. 如果还没有 Azure 资源组,可以使用 az group create 命令创建一个。If you don't already have an Azure resource group, you can use the az group create command to create one.

以下示例在“中国东部”位置创建名为 myResourceGroup 的资源组: The following example creates a resource group named myResourceGroup in the China East location:

az group create --name myResourceGroup --location chinaeast

创建存储帐户Create a storage account

存储帐户是一个存储共享池,在其中可以部署 Azure 文件共享或其他存储资源,例如 Blob 或队列。A storage account is a shared pool of storage in which you can deploy Azure file shares or other storage resources, such as blobs or queues. 一个存储帐户可以包含无数个文件共享。A storage account can contain an unlimited number of file shares. 一个共享可以存储无数个文件,直到达到存储帐户的容量限制为止。A share can store an unlimited number of files, up to the capacity limits of the storage account.

以下示例使用 az storage account create 命令创建名为 mystorageaccount<随机数字> 的存储帐户,然后将该存储帐户的名称置于 $STORAGEACCT 变量中。The following example creates a storage account named mystorageaccount<random number> by using the az storage account create command, and then puts the name of that storage account in the $STORAGEACCT variable. 存储帐户名称必须是唯一的,因此请确保将“mystorageacct”替换为唯一名称。Storage account names must be unique, so make sure to replace "mystorageacct" with a unique name.

STORAGEACCT=$(az storage account create \
    --resource-group "myResourceGroup" \
    --name "mystorageacct$RANDOM" \
    --location chinaeast \
    --sku Standard_LRS \
    --query "name" | tr -d '"')

获取存储帐户密钥Get the storage account key

存储帐户密钥控制对存储帐户中资源的访问。Storage account keys control access to resources in a storage account. 这些密钥是在创建存储帐户时自动创建的。The keys are automatically created when you create a storage account. 可以使用 az storage account keys list 命令获取存储帐户的存储帐户密钥:You can get the storage account keys for your storage account by using the az storage account keys list command:

STORAGEKEY=$(az storage account keys list \
    --resource-group "myResourceGroup" \
    --account-name $STORAGEACCT \
    --query "[0].value" | tr -d '"')

创建 Azure 文件共享Create an Azure file share

现在可以创建第一个 Azure 文件共享了。Now, you can create your first Azure file share. 请使用 az storage share create 命令创建文件共享。Create file shares by using the az storage share create command. 以下示例创建名为 myshare 的 Azure 文件共享:This example creates an Azure file share named myshare:

az storage share create \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --name "myshare" 

共享名称只能包含小写字母、数字和单个连字符(但不能以连字符开头)。Share names can contain only lowercase letters, numbers, and single hyphens (but they can't start with a hyphen). 有关为文件共享和文件命名的完整详细信息,请参阅 命名和引用共享、目录、文件和元数据For complete details about naming file shares and files, see Naming and referencing shares, directories, files, and metadata.

使用 Azure 文件共享Use your Azure file share

Azure 文件提供两种在 Azure 文件共享中使用文件和文件夹的方法:行业标准服务器消息块 (SMB) 协议文件 REST 协议Azure Files provides two methods of working with files and folders within your Azure file share: the industry standard Server Message Block (SMB) protocol and the File REST protocol.

若要通过 SMB 装载文件共享,请参阅下述基于 OS 的文档:To mount a file share with SMB, see the following document based on your OS:

将 Azure 文件共享与文件 REST 协议配合使用Using an Azure file share with the File REST protocol

可以直接使用文件 REST 协议(手动进行 REST HTTP 调用),但最常见的使用文件 REST 协议的方式是使用 Azure CLI、Azure PowerShell 模块或 Azure 存储 SDK,所有这些方式都可以在所选脚本/编程语言中为文件 REST 协议提供很好的包装器。It is possible work directly with the File REST protocol directly (handcrafting REST HTTP calls yourself), but the most common way to use the File REST protocol is to use the Azure CLI, the Azure PowerShell module, or an Azure Storage SDK, all of which provide a nice wrapper around the File REST protocol in the scripting/programming language of your choice.

我们预计,在使用 Azure 文件时,大多数情况下需要通过 SMB 协议来使用 Azure 文件共享,因为这样可以使用那些预期可以使用的现有应用程序和工具,但某些情况下,使用文件 REST API 比使用 SMB 更具优势,例如:We expect most uses of Azure Files will want to work with their Azure file share over the SMB protocol, as this allows them to use the existing applications and tools they expect to be able to use, but there are several reasons why it is advantageous to use the File REST API rather than SMB, such as:

  • 需要通过 PowerShell(不能通过 SMB 来装载文件共享)来浏览文件共享。You are browsing your file share from the PowerShell (which cannot mount file shares over SMB).
  • 需在无法装载 SMB 共享的客户端(例如未将端口 445 解除阻止的本地客户端)中执行脚本或应用程序。You need to execute a script or application from a client that cannot mount an SMB share, such as on-premises clients that do not have port 445 unblocked.
  • 需利用无服务器资源,例如 Azure FunctionsYou are taking advantage of serverless resources, such as Azure Functions.

以下示例介绍如何使用 Azure CLI 通过文件 REST 协议来操作 Azure 文件共享。The following examples show how to use the Azure CLI to manipulate your Azure file share with the File REST protocol.

创建目录Create a directory

若要在 Azure 文件共享的根目录中创建名为 myDirectory 的新目录,请使用 az storage directory create 命令:To create a new directory named myDirectory at the root of your Azure file share, use the az storage directory create command:

az storage directory create \
   --account-name $STORAGEACCT \
   --account-key $STORAGEKEY \
   --share-name "myshare" \
   --name "myDirectory" 

上传文件Upload a file

若要演示如何使用 az storage file upload 命令来上传文件,请首先在 Cloud Shell 暂存驱动器上创建要上传的文件。To demonstrate how to upload a file by using the az storage file upload command, first create a file to upload on the Cloud Shell scratch drive. 以下示例创建并上传该文件:In the following example, you create and then upload the file:

date > ~/clouddrive/SampleUpload.txt

az storage file upload \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --share-name "myshare" \
    --source "~/clouddrive/SampleUpload.txt" \
    --path "myDirectory/SampleUpload.txt"

如果在本地运行 Azure CLI,请将 ~/clouddrive 替换为计算机上的现有路径。If you're running Azure CLI locally, substitute ~/clouddrive with a path that exists on your machine.

上传文件以后,即可使用 az storage file list 命令,确保文件已上传到 Azure 文件共享:After you upload the file, you can use the az storage file list command to make sure that the file was uploaded to your Azure file share:

az storage file list \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --share-name "myshare" \
    --path "myDirectory" \
    --output table

下载文件Download a file

可以使用 az storage file download 命令下载已上传到 PowerShell 暂存驱动器的文件的副本:You can use the az storage file download command to download a copy of the file that you uploaded to the PowerShell scratch drive:

# Delete an existing file by the same name as SampleDownload.txt, if it exists, because you've run this example before
rm -rf ~/clouddrive/SampleDownload.txt

az storage file download \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --share-name "myshare" \
    --path "myDirectory/SampleUpload.txt" \
    --dest "~/clouddrive/SampleDownload.txt"

复制文件Copy files

一项常见的任务是将文件从一个文件共享复制到另一个文件共享,或者将文件在文件共享和 Azure Blob 存储容器之间来回复制。A common task is to copy files from one file share to another file share, or to or from an Azure Blob storage container. 为了演示此功能,请创建一个新的共享。To demonstrate this functionality, create a new share. 请使用 az storage file copy 命令将上传的文件复制到该新共享:Copy the file that you uploaded to this new share by using the az storage file copy command:

az storage share create \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --name "myshare2"

az storage directory create \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --share-name "myshare2" \
    --name "myDirectory2"

az storage file copy start \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --source-share "myshare" \
    --source-path "myDirectory/SampleUpload.txt" \
    --destination-share "myshare2" \
    --destination-path "myDirectory2/SampleCopy.txt"

现在,如果列出新共享中的文件,则应看到已复制的文件:Now, if you list the files in the new share, you should see your copied file:

az storage file list \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --share-name "myshare2" \
    --output table

虽然 az storage file copy start 命令可以方便地用于 Azure 文件共享和 Azure Blob 存储容器之间的文件移动,但我们仍建议你使用 AzCopy 进行较大型Although the az storage file copy start command is convenient for file moves between Azure file shares and Azure Blob storage containers, we recommend that you use AzCopy for larger moves. (就要移动的文件的数量或大小而言)的移动。详细了解 Linux 版 AzCopyWindows 版 AzCopy(Larger in terms of the number or size of files being moved.) Learn more about AzCopy for Linux and AzCopy for Windows. AzCopy 必须安装在本地。AzCopy must be installed locally. AzCopy 在 Cloud Shell 中不可用。AzCopy isn't available in Cloud Shell.

创建和管理共享快照Create and manage share snapshots

可以通过 Azure 文件共享执行的另一项有用的任务是创建共享快照。Another useful task that you can do with an Azure file share is create share snapshots. 快照保存 Azure 文件共享在某个时间点的副本。A snapshot preserves a point-in-time copy of an Azure file share. 共享快照类似于你可能已经熟悉的某些操作系统技术:Share snapshots are similar to some operating system technologies that you might already be familiar with:

SNAPSHOT=$(az storage share snapshot \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --name "myshare" \
    --query "snapshot" | tr -d '"')

浏览共享快照内容Browse share snapshot contents

可以浏览共享快照的内容,只需将变量 $SNAPSHOT 中捕获的共享快照的时间戳传递给 az storage file list 命令即可:You can browse the contents of a share snapshot by passing the time stamp of the share snapshot that you captured in the $SNAPSHOT variable to the az storage file list command:

az storage file list \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --share-name "myshare" \
    --snapshot $SNAPSHOT \
    --output table

列出共享快照List share snapshots

若要查看为共享生成的快照的列表,请使用以下命令:To see the list of snapshots that you've taken for your share, use the following command:

az storage share list \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --include-snapshot \
    --query "[? name=='myshare' && snapshot!=null]" | tr -d '"'

从共享快照还原Restore from a share snapshot

可以使用此前用过的 az storage file copy start 命令还原某个文件。You can restore a file by using the az storage file copy start command that you used earlier. 首先,请删除已上传的 SampleUpload.txt 文件,这样才能将其从快照还原:First, delete the SampleUpload.txt file that you uploaded, so you can restore it from the snapshot:

# Delete SampleUpload.txt
az storage file delete \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --share-name "myshare" \
    --path "myDirectory/SampleUpload.txt"
 # Build the source URI for a snapshot restore
URI=$(az storage account show \
    --resource-group "myResourceGroup" \
    --name $STORAGEACCT \
    --query "primaryEndpoints.file" | tr -d '"')
 URI=$URI"myshare/myDirectory/SampleUpload.txt?sharesnapshot="$SNAPSHOT
 # Restore SampleUpload.txt from the share snapshot
az storage file copy start \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --source-uri $URI \
    --destination-share "myshare" \
    --destination-path "myDirectory/SampleUpload.txt"

删除共享快照Delete a share snapshot

可以使用 az storage share delete 命令删除共享快照。You can delete a share snapshot by using the az storage share delete command. 所使用的变量包含对 --snapshot 参数的 $SNAPSHOT 引用:Use the variable that contains the $SNAPSHOT reference to the --snapshot parameter:

az storage share delete \
    --account-name $STORAGEACCT \
    --account-key $STORAGEKEY \
    --name "myshare" \
    --snapshot $SNAPSHOT

清理资源Clean up resources

完成后,可以使用 az group delete 命令删除资源组和所有相关的资源:When you are done, you can use the az group delete command to remove the resource group and all related resources:

az group delete --name "myResourceGroup"

也可逐一删除资源。Alternatively, you can remove resources individually.

  • 若要删除为本文创建的 Azure 文件共享,请执行以下命令:To remove the Azure file shares that you created for this article:

    az storage share delete \
        --account-name $STORAGEACCT \
        --account-key $STORAGEKEY \
        --name "myshare" \
        --delete-snapshots include
    
    az storage share delete \
        --account-name $STORAGEACCT \
        --account-key $STORAGEKEY \
        --name "myshare2" \
        --delete-snapshots include
    
  • 若要删除存储帐户本身,请执行以下命令To remove the storage account itself. (这会隐式删除已创建的 Azure 文件共享,以及任何其他可能已创建的存储资源,例如 Azure Blob 存储容器):(This implicitly removes the Azure file shares that you created, and any other storage resources that you might have created, such as an Azure Blob storage container.)

    az storage account delete \
        --resource-group "myResourceGroup" \
        --name $STORAGEACCT \
        --yes
    

后续步骤Next steps