快速入门:使用 Azure PowerShell 创建和管理 Azure 文件共享Quickstart: Create and manage an Azure file share with Azure PowerShell

本指南介绍通过 PowerShell 来使用 Azure 文件共享的基本知识。This guide walks you through the basics of working with Azure file shares with PowerShell. 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 PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本指南需要 Azure PowerShell 模块 Az 版本 0.7 或更高版本。This guide requires the Azure PowerShell module Az version 0.7 or later. 若要找出正在运行的 Azure PowerShell 模块的版本,请执行 Get-Module -ListAvailable AzTo find out which version of the Azure PowerShell module you are running, execute Get-Module -ListAvailable Az. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Login-AzAccount -Environment AzureChinaCloud 以登录到 Azure 帐户。If you are running PowerShell locally, you also need to run Login-AzAccount -Environment AzureChinaCloud to login to your Azure account.

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed. 如果没有 Azure 资源组,可以使用 New-AzResourceGroup cmdlet 新建一个。If you don't already have an Azure resource group, you can create a new one with the New-AzResourceGroup cmdlet.

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

$resourceGroupName = "myResourceGroup"
$region = "chinaeast2"

New-AzResourceGroup `
    -Name $resourceGroupName `
    -Location $region | Out-Null

创建存储帐户Create a storage account

存储帐户是一个存储共享池,可以用来部署 Azure 文件共享。A storage account is a shared pool of storage you can use to deploy Azure file shares. 一个存储帐户可以包含无限数量的共享,一个共享可以存储无限数量的文件,直到达到存储帐户的容量限制为止。A storage account can contain an unlimited number of shares, and a share can store an unlimited number of files, up to the capacity limits of the storage account. 此示例创建常规用途版本 2(GPv2 存储帐户),该版本可以在硬盘驱动器 (HDD) 旋转介质上存储标准 Azure 文件共享或其他存储资源(例如 blob 或队列)。This example creates a general purpose version 2 (GPv2 storage account), which can storage standard Azure file shares or other storage resources such as blobs or queues, on hard-disk drive (HDD) rotational media. Azure 文件还支持高级固态磁盘驱动器 (SSD);高级 Azure 文件共享可在 FileStorage 存储帐户中创建。Azure Files also supports premium solid-state disk drives (SSDs); premium Azure file shares can be created in FileStorage storage accounts.

此示例使用 New-AzStorageAccount cmdlet 创建存储帐户。This example creates a storage account using the New-AzStorageAccount cmdlet. 存储帐户名为 mystorageaccount<random number> ,对该存储帐户的引用存储在变量 $storageAcct 中。The storage account is named mystorageaccount<random number> and a reference to that storage account is stored in the variable $storageAcct. 存储帐户名称必须唯一,因此请使用 Get-Random 将一个数字追加到名称末尾,使之变得唯一。Storage account names must be unique, so use Get-Random to append a number to the name to make it unique.

$storageAccountName = "mystorageacct$(Get-Random)"

$storageAcct = New-AzStorageAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $storageAccountName `
    -Location $region `
    -Kind StorageV2 `
    -SkuName Standard_LRS `


超过 5 TiB(每个共享最多100 TiB)的共享仅适用于本地冗余 (LRS) 存储帐户。Shares greater than 5 TiB (up to a maximum of 100 TiB per share) are only available in locally redundant (LRS) storage accounts. 若要创建异地冗余 (GRS) 存储帐户,请删除 -EnableLargeFileShare 参数。To create a geo-redundant (GRS) storage account, remove the -EnableLargeFileShare parameter.

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

现在可以创建第一个 Azure 文件共享。Now you can create your first Azure file share. 可以使用 New-AzRmStorageShare cmdlet 创建文件共享。You can create a file share using the New-AzRmStorageShare cmdlet. 此示例创建名为 myshare 的共享。This example creates a share named myshare.

$shareName = "myshare"

New-AzRmStorageShare `
    -StorageAccount $storageAcct `
    -Name $shareName `
    -QuotaGiB 1024 | Out-Null

共享名必须全部采用小写字母、数字和单个连字符,但不能以连字符开头。Share names need to be all lower-case letters, numbers, and single hyphens but cannot 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 PowerShell 模块、Azure CLI 或 Azure 存储 SDK,所有这些方式都可以在所选脚本/编程语言中为文件 REST 协议提供很好的包装器。It is possible work with the File REST protocol directly (i.e. handcrafting REST HTTP calls yourself), but the most common way to use the File REST protocol is to use the Azure PowerShell module, the Azure CLI, 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.

大多数情况下,需要通过 SMB 协议来使用 Azure 文件共享,因为这样可以使用那些预期可以使用的现有应用程序和工具,但某些情况下,使用文件 REST API 比使用 SMB 更具优势,例如:In most cases, you will use your Azure file share over the SMB protocol, as this allows you to use the existing applications and tools you 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).
  • 需利用无服务器资源,例如 Azure FunctionsYou are taking advantage of serverless resources, such as Azure Functions.
  • 你将创建将与许多 Azure 文件共享进行交互的增值服务,例如执行备份或防病毒扫描。You are creating a value-add service that will interact with many Azure file shares, such as performing backup or antivirus scans.

以下示例介绍如何使用 Azure PowerShell 模块通过文件 REST 协议来操作 Azure 文件共享。The following examples show how to use the Azure PowerShell module to manipulate your Azure file share with the File REST protocol. -Context 参数用于检索存储帐户密钥,以便对文件共享执行指示的操作。The -Context parameter is used to retrieve the storage account key to perform the indicated actions against the file share. 若要检索存储帐户密钥,必须在存储帐户上有 Azure 角色 OwnerTo retrieve the storage account key, you must have the Azure role of Owner on the storage account.

创建目录Create directory

若要在 Azure 文件共享的根目录中创建名为 myDirectory 的新目录,请使用 New-AzStorageDirectory cmdlet。To create a new directory named myDirectory at the root of your Azure file share, use the New-AzStorageDirectory cmdlet.

New-AzStorageDirectory `
   -Context $storageAcct.Context `
   -ShareName $shareName `
   -Path "myDirectory"

上传文件Upload a file

若要演示如何使用 Set-AzStorageFileContent cmdlet 来上传文件,首先需要在 PowerShell 的暂存驱动器中创建要上传的文件。To demonstrate how to upload a file using the Set-AzStorageFileContent cmdlet, we first need to create a file inside your PowerShell's scratch drive to upload.

此示例将当前的日期和时间置于暂存驱动器的新文件中,然后将文件上传到文件共享。This example puts the current date and time into a new file on your scratch drive, then uploads the file to the file share.

# this expression will put the current date and time into a new file on your scratch drive
cd "~/CloudDrive/"
Get-Date | Out-File -FilePath "SampleUpload.txt" -Force

# this expression will upload that newly created file to your Azure file share
Set-AzStorageFileContent `
   -Context $storageAcct.Context `
   -ShareName $shareName `
   -Source "SampleUpload.txt" `
   -Path "myDirectory\SampleUpload.txt"

应将 ~/CloudDrive/ 替换为计算机上存在的路径。You should substitute ~/CloudDrive/ with a path that exists on your machine.

上传文件后,可以使用 Get-AzStorageFile cmdlet 进行检查,确保文件已上传到 Azure 文件共享。After uploading the file, you can use Get-AzStorageFile cmdlet to check to make sure that the file was uploaded to your Azure file share.

Get-AzStorageFile `
    -Context $storageAcct.Context `
    -ShareName $shareName `
    -Path "myDirectory\" 

下载文件Download a file

可以使用 Get-AzStorageFileContent cmdlet 下载刚上传到 PowerShell 的暂存驱动器的文件的副本。You can use the Get-AzStorageFileContent cmdlet to download a copy of the file you just uploaded to the scratch drive of your PowerShell.

# Delete an existing file by the same name as SampleDownload.txt, if it exists because you've run this example before.
Remove-Item `
    -Path "SampleDownload.txt" `
    -Force `
    -ErrorAction SilentlyContinue

Get-AzStorageFileContent `
    -Context $storageAcct.Context `
    -ShareName $shareName `
    -Path "myDirectory\SampleUpload.txt" `
    -Destination "SampleDownload.txt"

下载文件以后,可以使用 Get-ChildItem 来查看该文件是否已下载到 PowerShell 的暂存驱动器。After downloading the file, you can use the Get-ChildItem to see that the file has been downloaded to your PowerShell's scratch drive.

Get-ChildItem | Where-Object { $_.Name -eq "SampleDownload.txt" }

复制文件Copy files

一项常见的任务是将文件从一个文件共享复制到另一个文件共享。One common task is to copy files from one file share to another file share. 若要演示此功能,可以创建一个新的共享,然后使用 Start-AzStorageFileCopy cmdlet 将刚上传的文件复制到该新共享。To demonstrate this functionality, you can create a new share and copy the file you just uploaded over to this new share using the Start-AzStorageFileCopy cmdlet.

$otherShareName = "myshare2"

New-AzRmStorageShare `
    -StorageAccount $storageAcct `
    -Name $otherShareName `
    -QuotaGiB 1024 | Out-Null
New-AzStorageDirectory `
   -Context $storageAcct.Context `
   -ShareName $otherShareName `
   -Path "myDirectory2"

Start-AzStorageFileCopy `
    -Context $storageAcct.Context `
    -SrcShareName $shareName `
    -SrcFilePath "myDirectory\SampleUpload.txt" `
    -DestShareName $otherShareName `
    -DestFilePath "myDirectory2\SampleCopy.txt" `
    -DestContext $storageAcct.Context

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

Get-AzStorageFile `
    -Context $storageAcct.Context `
    -ShareName $otherShareName `
    -Path "myDirectory2" 

虽然 Start-AzStorageFileCopy cmdlet 可以方便地用于 Azure 文件共享之间的临时文件移动,但对于迁移和更大的数据移动,我们建议在 Windows 上使用 robocopy,在 macOS 和 Linux 上使用 rsyncWhile the Start-AzStorageFileCopy cmdlet is convenient for ad hoc file moves between Azure file shares, for migrations and larger data movements, we recommend robocopy on Windows and rsync on macOS and Linux. robocopyrsync 使用 SMB 而不是 FileREST API 来执行数据移动。robocopy and rsync use SMB to perform the data movements instead of the FileREST API.

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

可以通过 Azure 文件共享执行的另一项有用的任务是创建共享快照。One additional useful task you can do with an Azure file share is to create share snapshots. 快照保存的是 Azure 文件共享的某个时间点。A snapshot preserves a point in time for an Azure file share. 共享快照类似于你可能已经熟悉的操作系统技术,例如:Share snapshots are similar to operating system technologies you may already be familiar with such as:

可以在通过 Get-AzStorageShare cmdlet 检索的文件共享的 PowerShell 对象上使用 Snapshot 方法来创建某个共享的共享快照。You can create a share snapshot for a share by using the Snapshot method on PowerShell object for a file share, which is retrieved with the Get-AzStorageShare cmdlet.

$share = Get-AzStorageShare -Context $storageAcct.Context -Name $shareName
$snapshot = $share.CloudFileShare.Snapshot()

浏览共享快照Browse share snapshots

可以浏览共享快照的内容,只需将快照引用 ($snapshot) 传递给 Get-AzStorageFile cmdlet 的 -Share 参数即可。You can browse the contents of the share snapshot by passing the snapshot reference ($snapshot) to the -Share parameter of the Get-AzStorageFile cmdlet.

Get-AzStorageFile -Share $snapshot

列出共享快照List share snapshots

可以使用以下命令查看为共享生成的快照的列表。You can see the list of snapshots you've taken for your share with the following command.

Get-AzStorageShare `
        -Context $storageAcct.Context | `
    Where-Object { $_.Name -eq $shareName -and $_.IsSnapshot -eq $true }

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

可以使用以前用过的 Start-AzStorageFileCopy 命令还原某个文件。You can restore a file by using the Start-AzStorageFileCopy command we used before. 就本快速入门来说,首先请删除以前上传的 SampleUpload.txt 文件,这样才能将其从快照还原。For the purposes of this quickstart, we'll first delete our SampleUpload.txt file we previously uploaded so we can restore it from the snapshot.

# Delete SampleUpload.txt
Remove-AzStorageFile `
    -Context $storageAcct.Context `
    -ShareName $shareName `
    -Path "myDirectory\SampleUpload.txt"

# Restore SampleUpload.txt from the share snapshot
Start-AzStorageFileCopy `
    -SrcShare $snapshot `
    -SrcFilePath "myDirectory\SampleUpload.txt" `
    -DestContext $storageAcct.Context `
    -DestShareName $shareName `
    -DestFilePath "myDirectory\SampleUpload.txt"

删除共享快照Delete a share snapshot

可以使用 Remove-AzStorageShare cmdlet 删除共享快照,其中的变量包含对 -Share 参数的 $snapshot 引用。You can delete a share snapshot by using the Remove-AzStorageShare cmdlet, with the variable containing the $snapshot reference to the -Share parameter.

Remove-AzStorageShare `
    -Share $snapshot `
    -Confirm:$false `

清理资源Clean up resources

完成后,可以使用 Remove-AzResourceGroup cmdlet 删除资源组和所有相关的资源。When you are done, you can use the Remove-AzResourceGroup cmdlet to remove the resource group and all related resources.

Remove-AzResourceGroup -Name myResourceGroup

也可逐一删除资源:You can alternatively remove resources one by one:

  • 删除为本快速入门创建的 Azure 文件共享。To remove the Azure file shares we created for this quickstart.

    Get-AzRmStorageShare -StorageAccount $storageAcct | Remove-AzRmStorageShare -Force


    删除 Azure 文件共享之前,必须删除创建的 Azure 文件共享的所有共享快照。You must delete all the share snapshots for the Azure file shares you created before deleting the Azure file share.

  • 删除存储帐户本身(这会隐式删除已创建的 Azure 文件共享,以及任何其他可能已创建的存储资源,例如 Azure Blob 存储容器)。To remove the storage account itself (this will implicitly remove the Azure file shares we created as well as any other storage resources you may have created such as an Azure Blob storage container).

    Remove-AzStorageAccount `
        -ResourceGroupName $storageAcct.ResourceGroupName `
        -Name $storageAcct.StorageAccountName

后续步骤Next steps