Rediger

Del via


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

Set up your environment

If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for Java. For more information, see Get started with Azure Blob Storage and Java.

To work with the code examples in this article, follow these steps to set up your project.

Note

This article uses the Maven build tool to build and run the example code. Other build tools, such as Gradle, also work with the Azure SDK for Java.

Install packages

Open the pom.xml file in your text editor. Install the packages by including the BOM file, or including a direct dependency.

Add import statements

Add the following import statements:

import com.azure.core.http.rest.*;
import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;

Authorization

The authorization mechanism must have the necessary permissions to list blob containers. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for List Containers (REST API).

Create a client object

To connect an app to Blob Storage, create an instance of BlobServiceClient.

The following example uses BlobServiceClientBuilder to build a BlobServiceClient object using DefaultAzureCredential, and shows how to create container and blob clients, if needed:

// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
        .endpoint("https://<storage-account-name>.blob.core.windows.net/")
        .credential(new DefaultAzureCredentialBuilder().build())
        .buildClient();

// If needed, you can create a BlobContainerClient object from the BlobServiceClient
BlobContainerClient containerClient = blobServiceClient
        .getBlobContainerClient("<container-name>");

// If needed, you can create a BlobClient object from the BlobContainerClient
BlobClient blobClient = containerClient
        .getBlobClient("<blob-name>");

To learn more about creating and managing client objects, see Create and manage client objects that interact with data resources.

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.

Code samples

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:

Client library resources

See also

  • This article is part of the Blob Storage developer guide for Java. To learn more, see the full list of developer guide articles at Build your Java app.