Somansh Reddy
Somansh Reddy

Reputation: 135

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?

Upvotes: 0

Views: 1005

Answers (2)

Somansh Reddy
Somansh Reddy

Reputation: 135

We can use the following method to get the continuation token

        List<BlobItem> allBlobs = new ArrayList<>();
        String continuationToken;
        ListBlobsOptions options = new ListBlobsOptions().setMaxResultsPerPage(5);
        Iterator<PagedResponse<BlobItem>> response = containerClient.listBlobs(options, Duration.ofSeconds(30)).iterableByPage().iterator();
        do {
            PagedResponse<BlobItem> pagedResponse = response.next();
            List<BlobItem> blobs = pagedResponse.getValue();
            continuationToken = pagedResponse.getContinuationToken();
            blobs.forEach(b -> System.out.println(b.getName()));
            allBlobs.addAll(blobs);
        } while (continuationToken != null);

Upvotes: 0

Jim Xu
Jim Xu

Reputation: 23141

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

Upvotes: 2

Related Questions