List blob containers with Java

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 Java.

Prerequisites

  • This article assumes you already have a project set up to work with the Azure Blob Storage client library for Java. To learn about setting up your project, including package installation, adding import directives, and creating an authorized client object, see Get Started with Azure Storage and Java.
  • 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 a storage account, call the following method:

This method returns an iterable of type BlobContainerItem. 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. To return a smaller set of results, provide a nonzero value for the size of the page of results to return. You can set this value using the following method:

The examples presented in this article show you how to return results in pages. To learn more about pagination concepts, see Pagination with the Azure SDK for Java.

Filter results with a prefix

To filter the list of containers, specify a string for the prefix parameter. The prefix string can include one or more characters. Azure Storage then returns only the containers whose names start with that prefix. You can set this value using the following method:

Include container metadata

To include container metadata with the results, create a BlobContainerListDetails instance and pass true to the following method:

Then pass the BlobContainerListDetails object to the following method:

Include deleted containers

To include soft-deleted containers with the results, create a BlobContainerListDetails instance and pass true to the following method:

Then pass the BlobContainerListDetails object to the following method:

Code examples

The following example lists containers and filters the results by a specified prefix:

public void listContainers(BlobServiceClient blobServiceClient) {
    // Set a prefix to filter results based on a specified character or string
    ListBlobContainersOptions options = new ListBlobContainersOptions()
            .setPrefix("container-");

    System.out.println("List containers:");
    for (BlobContainerItem blobContainerItem : blobServiceClient.listBlobContainers(options, null)) {
        System.out.printf("Container name: %s%n", blobContainerItem.getName());
    }
}

You can also return a smaller set of results, by specifying the size of the page of results to return:

public void listContainersWithPaging(BlobServiceClient blobServiceClient) {
    // Set a prefix to filter results and specify a page limit
    ListBlobContainersOptions options = new ListBlobContainersOptions()
            .setMaxResultsPerPage(2)  // Low number for demonstration purposes
            .setPrefix("container-");

    int i = 0;
    Iterable<PagedResponse<BlobContainerItem>> blobContainerPages = blobServiceClient
            .listBlobContainers(options, null).iterableByPage();
    for (PagedResponse<BlobContainerItem> page : blobContainerPages) {
        System.out.printf("Page %d%n", ++i);
        page.getElements().forEach(container -> {
            System.out.printf("Name: %s%n", container.getName());
        });
    }
}

Resources

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

REST API operations

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

Code samples

Client library resources

See also