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

开始使用 Azure Blob 存储和 Visual Studio 连接服务 (ASP.NET Core)Get started with Azure Blob storage and Visual Studio connected services (ASP.NET Core)

本文内容

提示

使用 Azure 存储资源管理器管理 Azure Blob 存储资源。Azure 存储资源管理器 是 Microsoft 免费提供的独立应用,可用于管理 Azure Blob 存储资源使用 Azure 存储资源管理器可以直观地创建、读取、更新和删除 Blob 容器与 Blob,以及管理对 Blob 容器和 Blob 的访问。

本文介绍通过使用 Visual Studio 中的“连接服务”功能在 ASP.NET Core 项目中创建或引用 Azure 存储帐户之后,如何开始在 Visual Studio 中使用 Azure Blob 存储。执行“连接服务”操作会安装相应的 NuGet 程序包,以访问项目中的 Azure 存储,并将存储帐户的连接字符串添加到项目配置文件中。(有关 Azure 存储的常规信息,请参阅存储文档。)

Azure Blob 存储是一项可存储大量非结构化数据的服务,用户可在世界任何地方通过 HTTP 或 HTTPS 访问这些数据。单个 Blob 可以是任意大小。Blob 可以是图像、音频和视频文件、原始数据以及文档文件等。本文介绍通过使用 Visual Studio 中的“连接服务”在 ASP.NET Core 项目中创建 Azure 存储帐户之后,如何开始使用 blob 存储。

正如文件位于文件夹中一样,存储 Blob 位于容器中。创建 blob 后,可以在此 blob 中创建一个或多个容器。例如,在名为“Scrapbook”的 blob 中,可以创建名为“images”的容器,用于存储图片,以及另一个名为“audio”的容器,用于存储音频文件。创建这些容器后,即可将各个文件上传到其中。有关以编程方式操作 Blob 的详细信息,请参阅快速入门:使用 .NET 上传、下载和列出 Blob

某些 Azure 存储 API 为异步,而本文中的代码假定正在使用异步方法。有关详细信息,请参阅异步编程

使用代码访问 blob 容器Access blob containers in code

若要以编程方式访问 ASP.NET Core 项目中的 blob,需要添加以下代码(如果尚未存在):

  • 添加必要的 using 语句:
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.WindowsAzure.Storage;
  3. using Microsoft.WindowsAzure.Storage.Blob;
  4. using System.Threading.Tasks;
  5. using LogLevel = Microsoft.Extensions.Logging.LogLevel;
  • 获取表示存储帐户信息的 CloudStorageAccount 对象。使用下面的代码从 Azure 服务配置中获取存储连接字符串和存储帐户信息:
  1. CloudStorageAccount storageAccount = new CloudStorageAccount(
  2. new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
  3. "<storage-account-name>",
  4. "<access-key>"), true);
  • 使用 CloudBlobClient 对象获取对存储帐户中现有容器的 CloudBlobContainer 引用:
  1. // Create a blob client.
  2. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
  3. // Get a reference to a container named "mycontainer."
  4. CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

使用代码创建容器Create a container in code

还可以通过调用 CloudBlobClient 使用 CreateIfNotExistsAsync 在存储帐户中创建容器:

  1. // Create a blob client.
  2. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
  3. // Get a reference to a container named "my-new-container."
  4. CloudBlobContainer container = blobClient.GetContainerReference("my-new-container");
  5. // If "mycontainer" doesn't exist, create it.
  6. await container.CreateIfNotExistsAsync();

如果要让容器中的文件可供所有人使用,可将容器设置为公用:

  1. await container.SetPermissionsAsync(new BlobContainerPermissions
  2. {
  3. PublicAccess = BlobContainerPublicAccessType.Blob
  4. });

将 Blob 上传到容器中Upload a blob into a container

要将 Blob 文件上传到容器中,请获取容器引用,并使用它来获取 Blob 引用。然后通过调用 UploadFromStreamAsync 方法将任何数据流上传到该引用。此操作将创建 blob(如果该 blob 不存在),并覆盖现有 blob。

  1. // Get a reference to a blob named "myblob".
  2. CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
  3. // Create or overwrite the "myblob" blob with the contents of a local file
  4. // named "myfile".
  5. using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
  6. {
  7. await blockBlob.UploadFromStreamAsync(fileStream);
  8. }

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

若要列出容器中的 blob,首先获取容器引用,然后调用其 ListBlobsSegmentedAsync 方法来检索其中的 blob 和/或目录。若要访问返回的 IListBlobItem 的丰富属性集和方法,请将它强制转换为 CloudBlockBlobCloudPageBlobCloudBlobDirectory 对象。如果 blob 类型未知,可以使用类型检查来确定要将其强制转换为哪种类型。

  1. BlobContinuationToken token = null;
  2. do
  3. {
  4. BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);
  5. token = resultSegment.ContinuationToken;
  6. foreach (IListBlobItem item in resultSegment.Results)
  7. {
  8. if (item.GetType() == typeof(CloudBlockBlob))
  9. {
  10. CloudBlockBlob blob = (CloudBlockBlob)item;
  11. Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
  12. }
  13. else if (item.GetType() == typeof(CloudPageBlob))
  14. {
  15. CloudPageBlob pageBlob = (CloudPageBlob)item;
  16. Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);
  17. }
  18. else if (item.GetType() == typeof(CloudBlobDirectory))
  19. {
  20. CloudBlobDirectory directory = (CloudBlobDirectory)item;
  21. Console.WriteLine("Directory: {0}", directory.Uri);
  22. }
  23. }
  24. } while (token != null);

有关列出 Blob 容器内容的其他方法,请参阅快速入门:使用 .NET 上传、下载和列出 Blob

下载 BlobDownload a blob

要下载某个 blob,请首先获取对该 blob 的引用,然后调用 DownloadToStreamAsync 方法。下面的示例使用 DownloadToStreamAsync 方法将 blob 内容传输到稍后可以另存为本地文件的流对象。

  1. // Get a reference to a blob named "photo1.jpg".
  2. CloudBlockBlob blockBlob = container.GetBlockBlobReference("photo1.jpg");
  3. // Save the blob contents to a file named "myfile".
  4. using (var fileStream = System.IO.File.OpenWrite(@"path\myfile"))
  5. {
  6. await blockBlob.DownloadToStreamAsync(fileStream);
  7. }

有关将 Blob 另存为文件的其他方法,请参阅快速入门:使用 .NET 上传、下载和列出 Blob

删除 BlobDelete a blob

要删除某个 blob,请首先获取对该 blob 的引用,然后调用 DeleteAsync 方法:

  1. // Get a reference to a blob named "myblob.txt".
  2. CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob.txt");
  3. // Delete the blob.
  4. await blockBlob.DeleteAsync();

后续步骤Next steps

现在,已了解有关 Azure Blob 存储的基础知识,可单击下面的链接来了解更复杂的存储任务。