如何使用适用于Azure Blob存储的新Java v12 SDK检索容器的分段列表?

时间:2020-07-23 15:22:13

标签: azure azure-storage-blobs azure-blob-storage cloud-storage azure-java-sdk

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

Java SDK文档指定了 listBlobContainers 调用,但是没有提到何时达到每次调用将返回的容器的限制。有没有其他方法可以为此调用获取 NextMarker ContinuationToken

2 个答案:

答案 0 :(得分:1)

当我们使用方法listBlobContainers列出容器时,如果其数量大于5000,它将进行分页。我们也可以使用ListBlobContainersOptions来定义每页大小。有关更多详细信息,请参阅herehere

例如

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

答案 1 :(得分:0)

我们可以使用以下方法获取延续令牌

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