使用 .NET 列出 Blob 容器List blob containers with .NET

通过代码列出 Azure 存储帐户中的容器时,可以指定多个选项来管理如何从 Azure 存储返回结果。When you list the containers in an Azure Storage account from your code, you can specify a number of options to manage how results are returned from Azure Storage. 本文介绍如何使用适用于 .NET 的 Azure 存储客户端库列出容器。This article shows how to list containers using the Azure Storage client library for .NET.

了解容器列出选项Understand container listing options

若要列出存储帐户中的容器,请调用以下方法之一:To list containers in your storage account, call one of the following methods:

这些方法的重载提供更多选项用于管理列出操作返回容器的方式。The overloads for these methods provide additional options for managing how containers are returned by the listing operation. 后续部分将介绍这些选项。These options are described in the following sections.

管理要返回的结果数Manage how many results are returned

默认情况下,列出操作每次最多返回 5000 个结果。By default, a listing operation returns up to 5000 results at a time. 若要返回更小的结果集,请为要返回的结果页的大小提供非零值。To return a smaller set of results, provide a nonzero value for the size of the page of results to return.

如果存储帐户包含 5000 个以上的容器,或者你指定了页面大小,以便列表操作返回存储帐户中的容器子集,则 Azure 存储将返回一个包含容器列表的延续令牌。If your storage account contains more than 5000 containers, or if you have specified a page size such that the listing operation returns a subset of containers in the storage account, then Azure Storage returns a continuation token with the list of containers. 继续标记是一个不透明值,可用于从 Azure 存储中检索下一组结果。A continuation token is an opaque value that you can use to retrieve the next set of results from Azure Storage.

在代码中检查延续令牌的值,以确定它是为空(适用于 .NET v12)还是为 NULL(适用于 .NET v11 及更低版本)。In your code, check the value of the continuation token to determine whether it is empty (for .NET v12) or null (for .NET v11 and earlier). 如果继续标记为 null,则表示结果集是完整的。When the continuation token is null, then the set of results is complete. 如果延续令牌不为 NULL,则再次调用列出方法,并传入延续令牌以检索下一组结果,直到延续令牌为 NULL。If the continuation token is not null, then call the listing method again, passing in the continuation token to retrieve the next set of results, until the continuation token is null.

使用前缀筛选结果Filter results with a prefix

若要筛选容器列表,请为 prefix 参数指定一个字符串。To filter the list of containers, specify a string for the prefix parameter. 前缀字符串可以包含一个或多个字符。The prefix string can include one or more characters. 然后,Azure 存储只返回其名称以该前缀开头的容器。Azure Storage then returns only the containers whose names start with that prefix.

返回元数据Return metadata

若要将容器元数据与结果一起返回,请为 BlobContainerTraits 枚举(适用于 .NET v12)或 ContainerListingDetails 枚举(适用于 .NET v11 及更低版本)指定“Metadata”值)。To return container metadata with the results, specify the Metadata value for the BlobContainerTraits enum (for .NET v12) or ContainerListingDetails enum (for .NET v11 and earlier). Azure 存储包含返回的每个容器的元数据,因此你也不需要提取容器元数据。Azure Storage includes metadata with each container returned, so you do not need to also fetch the container metadata.

示例:列出容器Example: List containers

以下示例以异步方式列出存储帐户中以指定的前缀开头的容器。The following example asynchronously lists the containers in a storage account that begin with a specified prefix. 该示例列出了以指定前缀开头的容器,并且每次对列表操作的调用均返回指定数量的结果。The example lists containers that begin with the specified prefix and returns the specified number of results per call to the listing operation. 然后,它使用延续令牌获取下一段结果。It then uses the continuation token to get the next segment of results. 该示例还会连同结果一起返回容器元数据。The example also returns container metadata with the results.

async static Task ListContainers(BlobServiceClient blobServiceClient, 
                                string prefix, 
                                int? segmentSize)
{
    try
    {
        // Call the listing operation and enumerate the result segment.
        var resultSegment = 
            blobServiceClient.GetBlobContainersAsync(BlobContainerTraits.Metadata, prefix, default)
            .AsPages(default, segmentSize);

        await foreach (Azure.Page<BlobContainerItem> containerPage in resultSegment)
        {
            foreach (BlobContainerItem containerItem in containerPage.Values)
            {
                Console.WriteLine("Container name: {0}", containerItem.Name);
            }

            Console.WriteLine();
        }
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}

使用 .NET 进行开发的资源Resources for development with .NET

下面的链接为使用适用于 .NET 的 Azure 存储客户端库的开发人员提供了有用的资源。The links below provide useful resources for developers using the Azure Storage client library for .NET.

Azure 存储通用 APIAzure Storage common APIs

Blob 存储 APIBlob storage APIs

.NET 工具.NET tools

请参阅See also