Redaguoti

Bendrinti naudojant


List blob containers with JavaScript or TypeScript

When you list the containers in an Azure Storage account from your code, you can specify several options to manage how results are returned from Azure Storage. This article shows how to list containers using the Azure Storage client library for JavaScript.

Prerequisites

  • The examples in this article assume you already have a project set up to work with the Azure Blob Storage client library for JavaScript. To learn about setting up your project, including package installation, importing modules, and creating an authorized client object to work with data resources, see Get started with Azure Blob Storage and JavaScript.
  • The authorization mechanism must have permissions to list blob containers. To learn more, see the authorization guidance for the following REST API operation:

About container listing options

When listing containers from your code, you can specify options to manage how results are returned from Azure Storage. You can specify the number of results to return in each set of results, and then retrieve the subsequent sets. You can also filter the results by a prefix, and return container metadata with the results. These options are described in the following sections.

To list containers in your storage account, call the following method:

This method returns a list of ContainerItem objects. Containers are ordered lexicographically by name.

Manage how many results are returned

By default, a listing operation returns up to 5000 results at a time, but you can specify the number of results that you want each listing operation to return. The examples presented in this article show you how to return results in pages.

Filter results with a prefix

To filter the list of containers, specify a string for the prefix parameter in ServiceListContainersOptions. The prefix string can include one or more characters. Azure Storage then returns only the containers whose names start with that prefix.

Include container metadata

To include container metadata with the results, set the includeMetadata parameter to true in ServiceListContainersOptions. Azure Storage includes metadata with each container returned, so you don't need to fetch the container metadata separately.

Include deleted containers

To include soft-deleted containers with the results, set the includeDeleted parameter in ServiceListContainersOptions.

Code 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 function listContainers(blobServiceClient, containerNamePrefix) {

  const options = {
    includeDeleted: false,
    includeMetadata: true,
    includeSystem: true,
    prefix: containerNamePrefix
  }

  console.log("Containers (by page):");
  for await (const response of blobServiceClient.listContainers(options).byPage({
    maxPageSize: 20,
  })) {
    console.log("- Page:");
    if (response.containerItems) {
      for (const container of response.containerItems) {
        console.log(`  - ${container.name}`);
      }
    }
  }
}

Resources

To learn more about listing containers using the Azure Blob Storage client library for JavaScript, see the following resources.

REST API operations

The Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods for listing containers use the following REST API operation:

Code samples

Client library resources

See also

Client library resources