快速入门:使用 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. 可以将其安装在 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.

备注

本文中所述的功能现在可用于具有分层命名空间的帐户。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. 如果还没有订阅,请在开始前创建一个 1 元试用帐户If you don't already have a subscription, 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 a storage account, see Create a storage account.

本快速入门需要运行 Azure CLI 2.0.46 或更高版本。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,请在浏览器窗口中运行 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_GRS \
    --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. 有关分配 Azure 角色的详细信息,请参阅使用 Azure CLI 为访问分配 Azure 角色For more information about assigning Azure roles, see Use Azure CLI to assign an Azure role for access.

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

az ad signed-in-user show --query objectId -o tsv | az role assignment create \
    --role "Storage Blob Data Contributor" \
    --assignee @- \
    --scope "/subscriptions/<subscription>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account>"

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

重要

Azure 角色分配可能需要几分钟时间来进行传播。Azure role assignments may take a few minutes to propagate.

你还可以使用存储帐户密钥来授权操作创建容器。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.

上传 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.

此示例使用 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 \
    --account-name <storage-account> \
    --container-name <container> \
    --name helloworld \
    --file ~/path/to/local/file \
    --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 --aad-endpoint https://login.partner.microsoftonline.cn
azcopy copy 'C:\myDirectory\myTextFile.txt' 'https://mystorageaccount.blob.core.chinacloudapi.cn/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.