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

快速入门:使用 Azure CLI 创建、下载和列出 blobQuickstart: Create, download, and list blobs with Azure CLI

Azure CLI 是 Azure 的命令行体验,用于管理 Azure 资源。The Azure CLI is Azure's command-line experience for managing Azure resources. 可以在浏览器中将它与 Azure Cloud Shell 配合使用。You can use it in your browser with Azure Cloud Shell. 也可将它安装在 macOS、Linux 或 Windows 上,然后从命令行运行它。You can also 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.

备注

本文中所述的功能现在可用于具有分层命名空间的帐户。The features described in this article are now available to accounts that have a hierarchical namespace. 若要查看限制,请参阅Azure Data Lake Storage Gen2 文章中提供的 Blob 存储功能To review limitations, see the Blob storage features available in Azure Data Lake Storage Gen2 article.

先决条件Prerequisites

若要访问 Azure 存储,需要一个 Azure 订阅。To access Azure Storage, you'll need an Azure subscription. 如果还没有订阅,请在开始前创建一个免费帐户If you don't already have a subscription, create a free 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 a storage account, see Create a storage account.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。You can use either Bash or PowerShell with Cloud Shell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To start Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。Select Try It in the upper-right corner of a code block. 选择“试用”不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell”按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Start Cloud Shell.

  2. 选择代码块上的“复制”按钮以复制代码。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上选择 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. 选择 Enter 运行此代码。Select Enter to run the code.

在本地安装 Azure CLIInstall the Azure CLI locally

如果选择在本地安装并使用 Azure CLI,本快速入门要求运行 Azure CLI 2.0.46 或更高版本。If you choose to install and use the Azure CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.46 or later. 运行 az --version 即可确定你的版本。Run az --version to determine your version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

如果在本地运行 Azure CLI,则必须登录并进行身份验证。If you are running the Azure CLI locally, you must log in and authenticate. 如果使用 Azure Cloud Shell,则不需执行此步骤。This step is not necessary if you are using Azure Cloud Shell. 若要登录到 Azure CLI,请在浏览器窗口中运行 az login 并进行身份验证:To log in to Azure CLI, run az login and authenticate in the browser window:

az login

有关使用 Azure CLI 进行身份验证的详细信息,请参阅使用 Azure CLI 登录For more information about authentication` with Azure CLI, see Sign in with Azure CLI.

授予对 Blob 存储的访问权限Authorize access to Blob storage

可以使用 Azure AD 凭据或存储帐户访问密钥通过 Azure CLI 授予对 Blob 存储的访问权限。You can authorize access to Blob storage from the Azure CLI either with Azure AD credentials or by using the storage account access key. 建议使用 Azure AD 凭据。Using Azure AD credentials is recommended. 本文介绍如何使用 Azure AD 授权 Blob 存储操作。This article shows how to authorize Blob storage operations using Azure AD.

与针对 Blob 存储的数据操作相对应的 Azure CLI 命令支持 --auth-mode 参数,该参数用于指定如何授权给定操作。Azure CLI commands for data operations against Blob storage support the --auth-mode parameter, which enables you to specify how to authorize a given operation. --auth-mode 参数设置为 login,使用 Azure AD 凭据进行授权。Set the --auth-mode parameter to login to authorize with Azure AD credentials. 有关详细信息,请参阅使用 Azure CLI 授权访问 blob 或队列数据For more information, see Authorize access to blob or queue data with Azure CLI.

仅 Blob 存储数据操作支持 --auth-mode 参数。Only Blob storage data operations support the --auth-mode parameter. 管理操作(例如创建资源组或存储帐户)会自动将 Azure AD 凭据用于授权。Management operations, such as creating a resource group or storage account, automatically use Azure AD credentials for authorization.

创建资源组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.

请务必将尖括号中的占位符值替换为你自己的值:Remember to replace placeholder values in angle brackets with your own values:

az group create \
    --name <resource-group> \
    --location <location>

创建存储帐户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.

请务必将尖括号中的占位符值替换为你自己的值:Remember to replace placeholder values in angle brackets with your own values:

az storage account create \
    --name <storage-account> \
    --resource-group <resource-group> \
    --location <location> \
    --sku Standard_ZRS \
    --encryption-services blob

创建容器Create a container

始终将 Blob 上传到容器中。Blobs are always uploaded into a container. 可以在容器中整理 Blob 组,就像在计算机的文件夹中整理文件一样。You can organize groups of blobs in containers 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.

下面的示例使用 Azure AD 帐户授权操作创建容器。The following example uses your Azure AD account to authorize the operation to create the container. 创建容器之前,请将存储 Blob 数据参与者角色分配给自己。Before you create the container, assign the Storage Blob Data Contributor role to yourself. 即使你是帐户所有者,也需要显式权限才能对存储帐户执行数据操作。Even if you are the account owner, you need explicit permissions to perform data operations against the storage account. 有关分配 RBAC 角色的详细信息,请参阅使用 Azure CLI 为访问分配 RBAC 角色For more information about assigning RBAC roles, see Use Azure CLI to assign an RBAC role for access.

你还可以使用存储帐户密钥来授权操作创建容器。You can also use the storage account key to authorize the operation to create the container. 有关使用 Azure CLI 授权数据操作的详细信息,请参阅使用 Azure CLI 授权访问 blob 或队列数据For more information about authorizing data operations with Azure CLI, see Authorize access to blob or queue data with Azure CLI.

请务必将尖括号中的占位符值替换为你自己的值:Remember to replace placeholder values in angle brackets with your own values:

az storage container create \
    --account-name <storage-account> \
    --name <container> \
    --auth-mode login

上传 blobUpload a blob

Blob 存储支持块 blob、追加 blob 和页 blob。Blob storage supports block blobs, append blobs, and page blobs. 本快速入门中的示例介绍如何使用块 blob。The examples in this quickstart show how to work with block blobs.

首先,创建要上传到块 blob 的文件。First, create a file to upload to a block blob. 如果使用的是 Azure Cloud Shell,请使用以下命令创建文件:If you're using Azure Cloud Shell, use the following command to create a file:

vi helloworld

打开该文件后,按“插入”。When the file opens, press insert. 键入“Hello world”,并按“Esc”。接下来,键入“:x”,然后按“Enter”。Type Hello world, then press Esc. Next, type :x, then 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. 由于文件是在根目录下创建的,因此无需指定文件路径。It's not necessary to specify a file path since the file was created at the root directory. 请务必将尖括号中的占位符值替换为你自己的值:Remember to replace placeholder values in angle brackets with your own values:

az storage blob upload \
    --account-name <storage-account> \
    --container-name <container> \
    --name helloworld \
    --file helloworld \
    --auth-mode login

此操作将创建 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. 请务必将尖括号中的占位符值替换为你自己的值:Remember to replace placeholder values in angle brackets with your own values:

az storage blob list \
    --account-name <storage-account> \
    --container-name <container> \
    --output table \
    --auth-mode login

下载 BlobDownload a blob

使用 az storage blob download 命令下载之前上传的 Blob。Use the az storage blob download command to download the blob you uploaded earlier. 请务必将尖括号中的占位符值替换为你自己的值:Remember to replace placeholder values in angle brackets with your own values:

az storage blob download \
    --account-name <storage-account> \
    --container-name <container> \
    --name helloworld \
    --file ~/destination/path/for/file \
    --auth-mode login

使用 AzCopy 传输数据Data transfer with AzCopy

AzCopy 命令行实用程序提供适用于 Azure 存储的高性能且可编写脚本的数据传输。The AzCopy command-line utility offers high-performance, scriptable data transfer for Azure Storage. 可使用 AzCopy 将数据传输到 Blob 存储和 Azure 文件存储,或将数据从其中传出。You can use AzCopy to transfer data to and from Blob storage and Azure Files. 有关 AzCopy v10(最新版 AzCopy)的详细信息,请参阅 AzCopy 入门For more information about AzCopy v10, the latest version of AzCopy, see Get started with AzCopy. 若要了解如何将 AzCopy v10 与 Blob 存储配合使用,请参阅使用 AzCopy 和 Blob 存储传输数据To learn about using AzCopy v10 with Blob storage, see Transfer data with AzCopy and Blob storage.

以下示例使用 AzCopy 将本地文件上传到 blob。The following example uses AzCopy to upload a local file to a blob. 请务必将示例值替换为你自己的值:Remember to replace the sample values with your own values:

azcopy login
azcopy copy 'C:\myDirectory\myTextFile.txt' 'https://mystorageaccount.blob.core.windows.net/mycontainer/myTextFile.txt'

清理资源Clean up resources

若要删除在本快速入门中创建的资源(包括存储帐户),请使用 az group delete 命令删除资源组。If you want to delete the resources you created as part of this quickstart, including the storage account, delete the resource group by using the az group delete command. 请务必将尖括号中的占位符值替换为你自己的值:Remember to replace placeholder values in angle brackets with your own values:

az group delete \
    --name <resource-group> \
    --no-wait

后续步骤Next steps

在此快速入门中,你了解了如何在本地文件系统和 Azure Blob 存储中的容器之间传输文件。In this quickstart, you learned how to transfer files between a local file system and a container in Azure Blob storage. 若要详细了解如何使用 Azure CLI 操作 Blob 存储,请浏览“适用于 Blob 存储的 Azure CLI 示例”。To learn more about working with Blob storage by using Azure CLI, explore Azure CLI samples for Blob storage.