Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)

Overview

--Use ListObjectsV2Request to get a list of files --ListObjectsV2Request may not return all lists in a single request --If ListObjectsV2Result # isTruncated is true, not all file lists can be obtained. --You need to pass ListObjectsV2Result # getNextContinuationToken to ListObjectsV2Request # setContinuationToken and get it again.

Get a list of all files ʻIterator`

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import lombok.val;

import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

class S3ObjectSummaryIterator implements Iterator<S3ObjectSummary> {

    private final AmazonS3 client;
    private final ListObjectsV2Request request;
    private final Queue<S3ObjectSummary> summaries = new LinkedList<>();
    private boolean isTruncated = false;

    S3ObjectSummaryIterator(AmazonS3 client, ListObjectsV2Request request) {
        this.client = client;
        this.request = request;
        fetch();
    }

    private void fetch() {
        val result = this.client.listObjectsV2(this.request);
        summaries.addAll(result.getObjectSummaries());
        this.isTruncated = result.isTruncated();
        if (result.isTruncated()) {
            request.setContinuationToken(result.getNextContinuationToken());
        }
    }

    @Override
    public boolean hasNext() {
        if (summaries.isEmpty() && isTruncated) {
            fetch();
        }
        return !summaries.isEmpty();
    }

    @Override
    public S3ObjectSummary next() {
        return summaries.poll();
    }

    public static Stream<S3ObjectSummary> stream(AmazonS3 client, ListObjectsV2Request request) {
        val iterator = new S3ObjectSummaryIterator(client, request);
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
    }

}

reference

-Learn object list and folder list using AWS SDK for Java

Recommended Posts

Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
Get a list of MBean information for Java applications
Get S3 object size with AWS SDK for Ruby
[AWS SDK for Java] Set a retry policy on the S3 client
Get a list of Qiita articles for a specific user with Ruby + Qiita API v2
Try local development of AWS (S3, DynamoDB) with AWS SDK for JavaScript and Docker
Set a signed cookie (for CloudFront) with a custom policy using the AWS SDK for Java
A list of rawValues for UITextContentType.
Sort a List of Java objects
Encrypt data uploaded to S3 using AWS SDK for Java / SSE-KMS
Upload / download / bulk delete data to S3 using Amazon S3 Client Builder with AWS SDK for Java
[Java] Get List / Map elements with Iterator
List of download destinations for oracle java
Get Timestamp with Azure BlobStorage Java SDK
[Java] [ibatis] How to get records of 1-to-N relationship with List <Map <>>
I made a Diff tool for Java files
Get block information with Hyperledger Iroha's Java SDK
Until you run a Java program with the AWS SDK local to Windows
A collection of simple questions for Java beginners
Code that deletes all files of the specified prefix in AWS S3 (Java)
Validate the identity token of a user authenticated with AWS Cognito in Java
Submit a job to AWS Batch with Java (Eclipse)
[Java basics] Let's make a triangle with a for statement
Get Azure App Service for Java Configuration with System.getEnv ()
Get a list of classes in a Guava specific package
Output a list of cron settings for all users.
[Java] Get the file path in the folder with List
List of frequently used Java instructions (for beginners and beginners)
Credentials referenced by the AWS SDK for Java by default
Notify Slack of AWS bills daily with Lambda for Ruby
Deploying a Java environment with Windows Subsystem for Linux (WSL)
Story of making a task management application with swing, java
I want to make a list with kotlin and java!
Implementation of a math parser with recursive descent parsing (Java)
Create a SlackBot with AWS lambda & API Gateway in Java
Try Spark Submit to EMR using AWS SDK for Java
Investigated how to call services with Watson SDK for Java
[Java] How to search for a value in an array (or list) with the contains method
Access S3 buckets using SSE-KMS encryption in an EC2 IAM Role environment (AWS SDK for Java)
[Java] Summary of for statements
[Java1.8 +] Get the date of the next x day of the week with LocalDate
Java, JS (jQuery) and Ajax. Get a specific value of JSON.
Handle business logic for a set of Entity in Java class
Find out the list of fonts available in AWS Lambda + Java
Sorting a list with an int array as an element (Java) (Comparator)
[Details] Implementation of consumer applications with Kinesis Client Library for Java
"Bundle install" causes a lot of git differences for gem files
A story about hitting the League Of Legends API with JAVA
Cast an array of Strings to a List of Integers in Java
Get the public URL of a private Flickr file in Java
Until INSERT S3 object into EC2 DB with Lambda @ java: AWS
Overwrite upload of file with the same name with BOX SDK (java)
How to check for the contents of a java fixed-length string