您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用 Azure PowerShell 上传、下载和列出 BlobQuickstart: Upload, download, and list blobs by using Azure PowerShell

使用 Azure PowerShell 模块创建和管理 Azure 资源。Use the Azure PowerShell module to create and manage Azure resources. 可以通过 PowerShell 命令行或脚本创建或管理 Azure 资源。Creating or managing Azure resources can be done from the PowerShell command line or in scripts. 本指南介绍如何使用 PowerShell 在本地磁盘和 Azure Blob 存储之间传输文件。This guide describes using PowerShell to transfer files between local disk and Azure Blob storage.

备注

仅当你注册了 Data Lake Storage 上的多协议访问公共预览版时,具有分层命名空间的帐户才可使用本文中所述的功能。The features described in this article are available to accounts that have a hierarchical namespace only if you enroll in the public preview of multi-protocol access on Data Lake Storage. 若要查看限制,请参阅已知问题一文。To review limitations, see the known issues article.

先决条件Prerequisites

若要访问 Azure 存储,需要一个 Azure 订阅。To access Azure Storage, you'll need an Azure subscription. 如果还没有订阅,则请在开始前创建一个免费帐户If you don't already have a subscription, then create a free 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 quickstart requires the Azure PowerShell module Az version 0.7 or later. 运行 Get-InstalledModule -Name Az -AllVersions | select Name,Version 即可查找版本。Run Get-InstalledModule -Name Az -AllVersions | select Name,Version to find the version. 如果需要进行安装或升级,请参阅安装 Azure PowerShell 模块If you need to install or upgrade, see Install Azure PowerShell module.

登录 AzureSign in to Azure

运行 Connect-AzAccount 命令以登录 Azure 订阅,并按照屏幕上的说明操作。Sign in to your Azure subscription with the Connect-AzAccount command and follow the on-screen directions.

Connect-AzAccount

如果你不知道要使用哪个位置,可以列出可用的位置。If you don't know which location you want to use, you can list the available locations. 使用以下代码示例显示位置列表,并找到要使用的位置。Display the list of locations by using the following code example and find the one you want to use. 本示例使用 eastusThis example uses eastus. 将位置存储在变量中,并使用该变量,这样就可以在一个位置更改它。Store the location in a variable and use the variable so you can change it in one place.

Get-AzLocation | select Location
$location = "eastus"

创建资源组Create a resource group

使用 New-AzResourceGroup 创建 Azure 资源组。Create an Azure resource group with New-AzResourceGroup. 资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed.

$resourceGroup = "myResourceGroup"
New-AzResourceGroup -Name $resourceGroup -Location $location

创建存储帐户Create a storage account

使用 New-AzStorageAccount 创建具有 LRS 复制功能的标准常规用途存储帐户。Create a standard, general-purpose storage account with LRS replication by using New-AzStorageAccount. 接下来,获取用于定义要使用的存储帐户的存储帐户上下文。Next, get the storage account context that defines the storage account you want to use. 对存储帐户执行操作时,引用上下文而不是重复传入凭据。When acting on a storage account, reference the context instead of repeatedly passing in the credentials. 使用以下示例创建一个名为 mystorageaccount 的存储帐户,该帐户默认启用本地冗余存储 (LRS) 和 Blob 加密。Use the following example to create a storage account called mystorageaccount with locally redundant storage (LRS) and blob encryption (enabled by default).

$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
  -Name "mystorageaccount" `
  -SkuName Standard_LRS `
  -Location $location `

$ctx = $storageAccount.Context

创建容器Create a container

始终将 Blob 上传到容器中。Blobs are always uploaded into a container. 可以整理 Blob 组,就像在计算机的文件夹中整理文件一样。You can organize groups of blobs like the way you organize your files on your computer in folders.

设置容器名称,然后使用 New-AzStorageContainer 创建容器。Set the container name, then create the container by using New-AzStorageContainer. 将权限设置为 blob 以允许对文件进行公共访问。Set the permissions to blob to allow public access of the files. 此示例中的容器名称是 quickstartblobs 。The container name in this example is quickstartblobs.

$containerName = "quickstartblobs"
New-AzStorageContainer -Name $containerName -Context $ctx -Permission blob

将 blob 上传到容器Upload blobs to the container

Blob 存储支持块 blob、追加 blob 和页 blob。Blob storage supports block blobs, append blobs, and page blobs. 用于备份 IaaS VM 的 VHD 文件是页 Blob。VHD files that back IaaS VMs are page blobs. 将追加 Blob 用于日志记录,例如有时需要写入到文件,再继续添加更多信息。Use append blobs for logging, such as when you want to write to a file and then keep adding more information. Blob 存储中存储的大多数文件都是块 blob。Most files stored in Blob storage are block blobs.

要将文件上传到块 blob,请获取容器引用,然后获取对该容器中的块 blob 的引用。To upload a file to a block blob, get a container reference, then get a reference to the block blob in that container. 具备 blob 引用后,可使用 Set-AzStorageBlobContent 将数据上传到其中。Once you have the blob reference, you can upload data to it by using Set-AzStorageBlobContent. 此操作将创建 Blob(如果该 Blob 不存在),或者覆盖 Blob(如果该 Blob 存在)。This operation creates the blob if it doesn't exist, or overwrites the blob if it exists.

以下示例将 Image001.jpg 和 Image002.png 从本地磁盘的 D: \_TestImages 文件夹上传到创建的容器中。The following examples upload Image001.jpg and Image002.png from the D:\_TestImages folder on the local disk to the container you created.

# upload a file
Set-AzStorageBlobContent -File "D:\_TestImages\Image001.jpg" `
  -Container $containerName `
  -Blob "Image001.jpg" `
  -Context $ctx 

# upload another file
Set-AzStorageBlobContent -File "D:\_TestImages\Image002.png" `
  -Container $containerName `
  -Blob "Image002.png" `
  -Context $ctx

上传尽可能多的文件,然后继续操作。Upload as many files as you like before continuing.

列出容器中的 BlobList the blobs in a container

使用 Get-AzStorageBlob 获取容器中的 blob 列表。Get a list of blobs in the container by using Get-AzStorageBlob. 此示例仅显示已上传的 blob 的名称。This example shows just the names of the blobs uploaded.

Get-AzStorageBlob -Container $ContainerName -Context $ctx | select Name

下载 BlobDownload blobs

将 blob 下载到本地磁盘。Download the blobs to your local disk. 对于要下载的每个 blob,请设置名称并调用 Get-AzStorageBlobContent 以下载 blob。For each blob you want to download, set the name and call Get-AzStorageBlobContent to download the blob.

此示例将 blob 下载到本地磁盘的 D: \_TestImages\Downloads 中。This example downloads the blobs to D:\_TestImages\Downloads on the local disk.

# download first blob
Get-AzStorageBlobContent -Blob "Image001.jpg" `
  -Container $containerName `
  -Destination "D:\_TestImages\Downloads\" `
  -Context $ctx 

# download another blob
Get-AzStorageBlobContent -Blob "Image002.png" `
  -Container $containerName `
  -Destination "D:\_TestImages\Downloads\" `
  -Context $ctx

使用 AzCopy 传输数据Data transfer with AzCopy

若要按可编写脚本的方式高性能地传输 Azure 存储中的数据,还可使用 AzCopy 实用工具。The AzCopy utility is another option for high-performance scriptable data transfer for Azure Storage. 使用 AzCopy 将数据传输到 Blob、文件和表存储或将数据从其中传出。Use AzCopy to transfer data to and from Blob, File, and Table storage.

在以下快速示例中,AzCopy 命令用于将名为 myfile.txt 的文件从 PowerShell 窗口上传到 mystoragecontainer 容器 。As a quick example, here's the AzCopy command for uploading a file called myfile.txt to the mystoragecontainer container from within a PowerShell window.

./AzCopy `
    /Source:C:\myfolder `
    /Dest:https://mystorageaccount.blob.core.windows.net/mystoragecontainer `
    /DestKey:<storage-account-access-key> `
    /Pattern:"myfile.txt"

清理资源Clean up resources

删除所有已创建的资产。Remove all of the assets you've created. 删除资产的最简单方法是删除资源组。The easiest way to remove the assets is to delete the resource group. 删除资源组还会删除该组中包含的所有资源。Removing the resource group also deletes all resources included within the group. 在以下示例中,删除资源组会删除存储帐户和资源组本身。In the following example, removing the resource group removes the storage account and the resource group itself.

Remove-AzResourceGroup -Name $resourceGroup

后续步骤Next steps

在此快速入门中,你在本地磁盘和 Azure Blob 存储之间传输了文件。In this quickstart, you transferred files between a local disk and Azure Blob storage. 若要详细了解如何使用 PowerShell 操作 Blob 存储,请继续学习如何将 Azure PowerShell 用于 Azure 存储。To learn more about working with Blob storage by using PowerShell, continue to How-to use Azure PowerShell with Azure Storage.

Microsoft Azure PowerShell 存储 cmdlet 参考Microsoft Azure PowerShell Storage cmdlets reference

Microsoft Azure 存储资源管理器Microsoft Azure Storage Explorer