Search code examples
azureazure-blob-storagecloud-storageazure-java-sdk

How to retrieve a segmented List of Containers using the new Java v12 SDK for Azure Blob Storage?


I see that the .NET examples have a ListContainersSegmented API call.

The Java SDK documentation specifies a listBlobContainers call but there is no mention about when it hits the limit of containers it will return per call. Is there a different way to obtain the NextMarker or ContinuationToken for this call?


Solution

  • When we use the method listBlobContainers to list containers, It will paginate if its number is greater than 5000. We also can use ListBlobContainersOptions to define per page size. For more details, please refer to here and here

    for example

    String endpoint = String.format(Locale.ROOT, "https://%s.blob.core.windows.net", accountName);
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
            BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
                    .endpoint(endpoint)
                    .credential(credential)
                    .buildClient();
            ListBlobContainersOptions options = new ListBlobContainersOptions();
            options.setMaxResultsPerPage(5);
            PagedIterable<BlobContainerItem> pagedIterableResponse= blobServiceClient.listBlobContainers(options, null);
            Iterator<PagedResponse<BlobContainerItem>> ite = pagedIterableResponse.iterableByPage().iterator();
            int i =1;
            while(ite.hasNext()){
                PagedResponse<BlobContainerItem> items= ite.next();
                System.out.println("The containers in the page "+i);
                i++;
                for (BlobContainerItem item: items.getValue()
                     ) {
    
                       System.out.println("\t"+item.getName());
                }
    
            }
    

    enter image description here