Search code examples
azureazure-storageazure-blob-storage

cannot use BlobContainerAsyncClient to list blobs


I want to list blobs from container using azure java sdk version 12.0.6. In order to getting good performance, I use async method with class BlobContainerAsyncClient. But I cannot list blobs with the class and I do not get any error.

My code

ListBlobsOptions options = new ListBlobsOptions()
            .setDetails(new BlobListDetails()
                .setRetrieveDeletedBlobs(true)
                .setRetrieveSnapshots(true));

        client.listBlobs(options).subscribe(blob ->
            System.out.printf("Name: %s, Directory? %b, Deleted? %b, Snapshot ID: %s%n",
                blob.getName(),
                blob.isPrefix(),
                blob.isDeleted(),
                blob.getSnapshot()));

Solution

  • Regarding the issue, please refer to the following code

    ListBlobsOptions options = new ListBlobsOptions()
                .setDetails(new BlobListDetails()
                    .setRetrieveDeletedBlobs(true)
                    .setRetrieveSnapshots(true));
    lient.listBlobs(options).doOnNext(blob ->
                System.out.printf("Name: %s, Directory? %b, Deleted? %b, Snapshot ID: %s%n",
                    blob.getName(),
                    blob.isPrefix(),
                    blob.isDeleted(),
                    blob.getSnapshot()))
                 .blockLast();
    

    enter image description here

    Besides, cannot call both subscribe() and block(). When you call subscribe() you get back a Disposable which allows you to cancel the async operation, but you cannot block on this object. If you want to ensure the method in question blocks, you simply call block() on the operation (the type which has a block() method is a Mono or possibly a Flux).