@@ -1100,7 +1100,7 @@ public CompletableFuture<Void> removeObject(RemoveObjectArgs args)
11001100 * minioAsyncClient.removeObjects(
11011101 * RemoveObjectsArgs.builder().bucket("my-bucketname").objects(objects).build());
11021102 * for (Result<DeleteError> result : results) {
1103- * DeleteError error = errorResult .get();
1103+ * DeleteError error = result .get();
11041104 * System.out.println(
11051105 * "Error in deleting object " + error.objectName() + "; " + error.message());
11061106 * }
@@ -1330,41 +1330,135 @@ public Iterable<Result<Item>> listObjects(ListObjectsArgs args) {
13301330 public CompletableFuture <List <Bucket >> listBuckets ()
13311331 throws InsufficientDataException , InternalException , InvalidKeyException , IOException ,
13321332 NoSuchAlgorithmException , XmlParserException {
1333- return listBuckets (ListBucketsArgs .builder ().build ());
1333+ return listBucketsAsync (null , null , null , null , null , null )
1334+ .thenApply (
1335+ response -> {
1336+ return response .result ().buckets ();
1337+ });
13341338 }
13351339
13361340 /**
13371341 * Lists bucket information of all buckets.
13381342 *
13391343 * <pre>Example:{@code
1340- * CompletableFuture<List<Bucket>> future =
1341- * minioAsyncClient.listBuckets(ListBucketsArgs.builder().extraHeaders(headers).build());
1344+ * Iterable<Result<Bucket>> results = minioAsyncClient.listBuckets(ListBucketsArgs.builder().build());
1345+ * for (Result<Bucket> result : results) {
1346+ * Bucket bucket = result.get();
1347+ * System.out.println(String.format("Bucket: %s, Region: %s, CreationDate: %s", bucket.name(), bucket.bucketRegion(), bucket.creationDate()));
1348+ * }
13421349 * }</pre>
13431350 *
1344- * @return {@link CompletableFuture}<{@link List}<{@link Bucket}>> object.
1345- * @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
1346- * @throws InternalException thrown to indicate internal library error.
1347- * @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
1348- * @throws IOException thrown to indicate I/O error on S3 operation.
1349- * @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
1350- * @throws XmlParserException thrown to indicate XML parsing error.
1351+ * @return {@link Iterable}<{@link List}<{@link Bucket}>> object.
13511352 */
1352- public CompletableFuture <List <Bucket >> listBuckets (ListBucketsArgs args )
1353- throws InsufficientDataException , InternalException , InvalidKeyException , IOException ,
1354- NoSuchAlgorithmException , XmlParserException {
1355- return executeGetAsync (args , null , null )
1356- .thenApply (
1357- response -> {
1353+ public Iterable <Result <Bucket >> listBuckets (ListBucketsArgs args ) {
1354+ return new Iterable <Result <Bucket >>() {
1355+ @ Override
1356+ public Iterator <Result <Bucket >> iterator () {
1357+ return new Iterator <Result <Bucket >>() {
1358+ private ListAllMyBucketsResult result = null ;
1359+ private Result <Bucket > error = null ;
1360+ private Iterator <Bucket > iterator = null ;
1361+ private boolean completed = false ;
1362+
1363+ private synchronized void populate () {
1364+ if (completed ) return ;
1365+
1366+ try {
1367+ this .iterator = new LinkedList <Bucket >().iterator ();
13581368 try {
1359- ListAllMyBucketsResult result =
1360- Xml .unmarshal (ListAllMyBucketsResult .class , response .body ().charStream ());
1361- return result .buckets ();
1362- } catch (XmlParserException e ) {
1363- throw new CompletionException (e );
1364- } finally {
1365- response .close ();
1369+ ListBucketsResponse response =
1370+ listBucketsAsync (
1371+ args .bucketRegion (),
1372+ args .maxBuckets (),
1373+ args .prefix (),
1374+ (result == null )
1375+ ? args .continuationToken ()
1376+ : result .continuationToken (),
1377+ args .extraHeaders (),
1378+ args .extraQueryParams ())
1379+ .get ();
1380+ this .result = response .result ();
1381+ } catch (InterruptedException e ) {
1382+ throw new RuntimeException (e );
1383+ } catch (ExecutionException e ) {
1384+ throwEncapsulatedException (e );
13661385 }
1367- });
1386+ this .iterator = this .result .buckets ().iterator ();
1387+ } catch (ErrorResponseException
1388+ | InsufficientDataException
1389+ | InternalException
1390+ | InvalidKeyException
1391+ | InvalidResponseException
1392+ | IOException
1393+ | NoSuchAlgorithmException
1394+ | ServerException
1395+ | XmlParserException e ) {
1396+ this .error = new Result <>(e );
1397+ completed = true ;
1398+ }
1399+ }
1400+
1401+ @ Override
1402+ public boolean hasNext () {
1403+ if (this .completed ) return false ;
1404+
1405+ if (this .error == null && this .iterator == null ) {
1406+ populate ();
1407+ }
1408+
1409+ if (this .error == null
1410+ && !this .iterator .hasNext ()
1411+ && this .result .continuationToken () != null
1412+ && !this .result .continuationToken ().isEmpty ()) {
1413+ populate ();
1414+ }
1415+
1416+ if (this .error != null ) return true ;
1417+ if (this .iterator .hasNext ()) return true ;
1418+
1419+ this .completed = true ;
1420+ return false ;
1421+ }
1422+
1423+ @ Override
1424+ public Result <Bucket > next () {
1425+ if (this .completed ) throw new NoSuchElementException ();
1426+ if (this .error == null && this .iterator == null ) {
1427+ populate ();
1428+ }
1429+
1430+ if (this .error == null
1431+ && !this .iterator .hasNext ()
1432+ && this .result .continuationToken () != null
1433+ && !this .result .continuationToken ().isEmpty ()) {
1434+ populate ();
1435+ }
1436+
1437+ if (this .error != null ) {
1438+ this .completed = true ;
1439+ return this .error ;
1440+ }
1441+
1442+ Bucket item = null ;
1443+ if (this .iterator .hasNext ()) {
1444+ item = this .iterator .next ();
1445+ }
1446+
1447+ if (item != null ) {
1448+ return new Result <>(item );
1449+ }
1450+
1451+ this .completed = true ;
1452+ throw new NoSuchElementException ();
1453+ }
1454+
1455+ @ Override
1456+ public void remove () {
1457+ throw new UnsupportedOperationException ();
1458+ }
1459+ };
1460+ }
1461+ };
13681462 }
13691463
13701464 /**
0 commit comments