如何使用新的 Java v12 SDK for Azure Blob Storage 检索分段的容器列表?

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

我看到 .NET 示例有一个 ListContainersSegmented API 调用。

Java SDK 文档指定了一个 listBlobContainers 调用,但没有提及它何时达到每次调用 return 的容器限制。是否有不同的方法来获取此调用的 NextMarkerContinuationToken

当我们使用listBlobContainers方法列出容器时,如果它的数量大于5000,它会分页。我们也可以使用ListBlobContainersOptions来定义每页大小。详情请参考here and here

例如

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());
            }

        }

我们可以使用下面的方法获取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);