使用 .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. 若要返回更少的结果,请在调用某个 ListContainerSegmented 方法时为 maxresults 参数提供非零值。To return a smaller set of results, provide a nonzero value for the maxresults parameter when calling one of the ListContainerSegmented methods.

如果存储帐户包含 5000 个以上的容器,或者你为 maxresults 指定了一个导致列出操作返回存储帐户中一部分容器的值,则 Azure 存储将返回一个包含容器列表的继续标记。 If your storage account contains more than 5000 containers, or if you have specified a value for maxresults 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.

在代码中检查继续标记的值,以确定它是否为 null。In your code, check the value of the continuation token to determine whether it is null. 如果继续标记为 null,则表示结果集是完整的。When the continuation token is null, then the set of results is complete. 如果继续标记不为 null,则再次调用 ListContainersSegmentedListContainersSegmentedAsync,并传入继续标记以检索下一组结果,直到继续标记为 null。If the continuation token is not null, then call ListContainersSegmented or ListContainersSegmentedAsync 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

若要连同结果一起返回容器元数据,请指定 ContainerListingDetails 枚举的 Metadata 值。To return container metadata with the results, specify the Metadata value for the ContainerListingDetails enumeration. Azure 存储包含每个返回的容器的元数据,因此无需同时调用 FetchAttributes 方法之一即可检索容器元数据。Azure Storage includes metadata with each container returned, so you do not need to also call one of the FetchAttributes methods to retrieve the container metadata.

示例:列出容器Example: List containers

以下示例以异步方式列出存储帐户中以指定的前缀开头的容器。The following example asynchronously lists the containers in a storage account that begin with a specified prefix. 该示例每次以 5 个结果为增量列出容器,并使用继续标记获取下一个结果段。The example lists containers in increments of 5 results at a time, and uses the continuation token to get the next segment of results. 该示例还会连同结果一起返回容器元数据。The example also returns container metadata with the results.

private static async Task ListContainersWithPrefixAsync(CloudBlobClient blobClient,
                                                        string prefix)
{
    Console.WriteLine("List all containers beginning with prefix {0}, plus container metadata:", prefix);

    try
    {
        ContainerResultSegment resultSegment = null;
        BlobContinuationToken continuationToken = null;

        do
        {
            // List containers beginning with the specified prefix, returning segments of 5 results each.
            // Passing null for the maxResults parameter returns the max number of results (up to 5000).
            // Requesting the container's metadata with the listing operation populates the metadata,
            // so it's not necessary to also call FetchAttributes() to read the metadata.
            resultSegment = await blobClient.ListContainersSegmentedAsync(
                prefix, ContainerListingDetails.Metadata, 5, continuationToken, null, null);

            // Enumerate the containers returned.
            foreach (var container in resultSegment.Results)
            {
                Console.WriteLine("\tContainer:" + container.Name);

                // Write the container's metadata keys and values.
                foreach (var metadataItem in container.Metadata)
                {
                    Console.WriteLine("\t\tMetadata key: " + metadataItem.Key);
                    Console.WriteLine("\t\tMetadata value: " + metadataItem.Value);
                }
            }

            // Get the continuation token. If not null, get the next segment.
            continuationToken = resultSegment.ContinuationToken;

        } while (continuationToken != null);
    }
    catch (StorageException e)
    {
        Console.WriteLine("HTTP error code {0} : {1}",
                            e.RequestInformation.HttpStatusCode,
                            e.RequestInformation.ErrorCode);
        Console.WriteLine(e.Message);
    }
}

使用 .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

列出容器 枚举 Blob 资源List Containers Enumerating Blob Resources