Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest Fabric, Power BI, and SQL learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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.
Open the pom.xml
file in your text editor. Install the packages by including the BOM file, or including a direct dependency.
Add the following import
statements:
import com.azure.core.http.rest.*;
import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;
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).
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.
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.
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.
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:
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:
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:
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());
});
}
}
To learn more about listing containers using the Azure Blob Storage client library for Java, see the following resources.
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:
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest Fabric, Power BI, and SQL learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayTraining
Learning path
Expand the capabilities for Java apps on Azure - Training
Start here and learn how you can get the full power of Azure with your Java apps - use idiomatic libraries to connect and interact with your preferred cloud services, including Azure SQL and NoSQL databases, messaging and eventing systems, Redis cache, storage and directory services. As always, use tools and frameworks that you know and love – Spring, Tomcat, WildFly, JBoss, WebLogic, WebSphere, Maven, Gradle, IntelliJ, Eclipse, Jenkins, Terraform and more.
Documentation
List blobs with Java - Azure Storage
Learn how to list blobs in your storage account using the Azure Storage client library for Java. Code examples show how to list blobs in a flat listing, or how to list blobs hierarchically, as though they were organized into directories or folders.
Delete and restore a blob with Java - Azure Storage
Learn how to delete and restore a blob in your Azure Storage account using the Java client library
Create a blob container with Java - Azure Storage
Learn how to create a blob container in your Azure Storage account using the Java client library.