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

Azure CLI 是 Azure 的命令行体验,用于管理 Azure 资源。The Azure CLI is Azure's command-line experience for managing Azure resources. 可以将其安装在 macOS、Linux 和 Windows 上,然后从命令行运行它。You can install it on macOS, Linux, or Windows and run it from the command line. 本快速入门介绍了如何使用 Azure CLI 通过 Azure Blob 存储来上传和下载数据。In this quickstart, you learn to use the Azure CLI to upload and download data to and from Azure Blob storage.

先决条件Prerequisites

若要访问 Azure 存储,需要一个 Azure 订阅。To access Azure Storage, you'll need an Azure subscription. 如果还没有订阅,则在开始前创建一个 1 元人民币的试用帐户If you don't already have a subscription, then create a 1rmb trial account before you begin.

对 Azure 存储进行的所有访问都要通过存储帐户完成。All access to Azure Storage takes place through a storage account. 对于本快速入门,请使用 Azure 门户、Azure PowerShell 或 Azure CLI 创建存储帐户。For this quickstart, create a storage account using the Azure portal, Azure PowerShell, or Azure CLI. 有关如何创建帐户的帮助,请参阅创建存储帐户For help creating the account, see Create a storage account.

本快速入门需要运行 Azure CLI 2.0.4 或更高版本。This quickstart requires that you are running the Azure CLI version 2.0.4 or later. 运行 az --version 即可确定你的版本。Run az --version to determine your version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

创建资源组Create a resource group

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

az group create \
    --name myResourceGroup \
    --location "China East"

创建存储帐户Create a storage account

使用 az storage account create 命令创建常规用途存储帐户。Create a general-purpose storage account with the az storage account create command. 常规用途的存储帐户可用于以下四种服务:Blob、文件、表和队列。The general-purpose storage account can be used for all four services: blobs, files, tables, and queues.

az storage account create \
    --name mystorageaccount \
    --resource-group myResourceGroup \
    --location "China East" \
    --sku Standard_LRS \
    --encryption blob

指定存储帐户凭据Specify storage account credentials

Azure CLI 需要存储帐户凭据才能执行本教程的大部分命令。The Azure CLI needs your storage account credentials for most of the commands in this tutorial. 提供凭据的选项有多种,其中最简单方法之一是设置 AZURE_STORAGE_ACCOUNTAZURE_STORAGE_KEY 环境变量。While there are several options for doing so, one of the easiest ways to provide them is to set AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_KEY environment variables.

首先,使用 az storage account keys list 命令显示存储帐户密钥:First, display your storage account keys by using the az storage account keys list command:

az storage account keys list \
    --account-name mystorageaccount \
    --resource-group myResourceGroup \
    --output table

现在,设置 AZURE_STORAGE_ACCOUNTAZURE_STORAGE_KEY 环境变量。Now, set the AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_KEY environment variables. 可以通过使用 export 命令在 Bash Shell 中完成此操作:You can do this in the Bash shell by using the export command:

export AZURE_STORAGE_ACCOUNT="mystorageaccountname"
export AZURE_STORAGE_KEY="myStorageAccountKey"

创建容器Create a container

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

可以使用 az storage container create 命令创建用于存储 blob 的容器。Create a container for storing blobs with the az storage container create command.

az storage container create --name mystoragecontainer

上传 blobUpload a blob

Blob 存储支持块 blob、追加 blob 和页 blob。Blob storage supports block blobs, append blobs, and page blobs. 存储在 Blob 存储中的大多数文件都存储为块 blob。Most files stored in Blob storage are stored as block blobs. 必须将数据添加到现有的 blob 中且不能修改该 blob 的现有内容时(例如进行日志记录时),使用追加 blob。Append blobs are used when data must be added to an existing blob without modifying its existing contents, such as for logging. 页 blob 支持 IaaS 虚拟机的 VHD 文件。Page blobs back the VHD files of IaaS virtual machines.

首先,创建要上传到 Blob 的文件。First, create a file to upload to a blob. 可以使用以下方法来创建文件:vi helloworld 当文件打开时,按“插入”,键入“Hello world”,按 Esc 并输入 :x,然后按 EnterYou can use the following in order to create a file: vi helloworld when the file opens, press insert, type "Hello world" and then press Esc and enter :x and press Enter.

此示例使用 az storage blob upload 命令将 Blob 上传到在上一个步骤中创建的容器中。In this example, you upload a blob to the container you created in the last step using the az storage blob upload command.

az storage blob upload \
    --container-name mystoragecontainer \
    --name blobName \
    --file ~/path/to/local/file

如果使用了上述方法创建文件,则可改用此 CLI 命令(请注意,不需指定路径,因为此文件是在基目录创建的,而通常是需要指定路径的):If you used the previously described method to create a file, you can use this CLI command instead (note that you didn't need to specify a path since the file was created at the base directory, normally you'd need to specify a path):

az storage blob upload \
    --container-name mystoragecontainer \
    --name helloworld
    --file helloworld

此操作将创建 Blob(如果该 Blob 尚不存在),或者覆盖 Blob(如果该 Blob 已存在)。This operation creates the blob if it doesn't already exist, and overwrites it if it does. 上传尽可能多的文件,然后继续操作。Upload as many files as you like before continuing.

若要同时上传多个文件,则可使用 az storage blob upload-batch 命令。To upload multiple files at the same time, you can use the az storage blob upload-batch command.

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

使用 az storage blob list 命令列出容器中的 blob。List the blobs in the container with the az storage blob list command.

az storage blob list \
    --container-name mystoragecontainer \
    --output table

下载 BlobDownload a blob

使用 az storage blob download 命令下载之前上传的 Blob。Use the az storage blob download command to download the blob you uploaded earlier.

az storage blob download \
    --container-name mystoragecontainer \
    --name blobName \
    --file ~/destination/path/for/file

使用 AzCopy 传输数据Data transfer with AzCopy

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

作为一个快速示例,下面是用于将名为 myfile.txt 的文件上传到 mystoragecontainer 容器中的 AzCopy 命令。As a quick example, here is the AzCopy command for uploading a file called myfile.txt to the mystoragecontainer container.

azcopy \
    --source /mnt/myfiles \
    --destination https://mystorageaccount.blob.core.chinacloudapi.cn/mystoragecontainer \
    --dest-key <storage-account-access-key> \
    --include "myfile.txt"

清理资源Clean up resources

如果不再需要你的资源组中的任何一个资源(包括使用本教程创建的存储帐户),可使用 az group delete 命令删除该资源组。If you no longer need any of the resources in your resource group, including the storage account you created in this Quickstart, delete the resource group with the az group delete command.

az group delete --name myResourceGroup

后续步骤Next steps

在此快速入门教程中,介绍了如何在本地磁盘和 Azure Blob 存储中的容器之间传输文件。In this Quickstart, you learned how to transfer files between local disk and a container in Azure Blob storage. 若要详细了解如何使用 Azure 存储中的 blob,请继续通过本教程了解如何使用 Azure Blob 存储。To learn more about working with blobs in Azure Storage, continue to the tutorial for working with Azure Blob storage.

[How to: Blob storage operations with the Azure CLI](storage-how-to-use-blobs-cli.md)