findify / s3mock

Embedded S3 server for easy mocking
MIT License
387 stars 107 forks source link

Paging do not work correctly #98

Open sgraca opened 6 years ago

sgraca commented 6 years ago

When paging is used for listing (for example forced by setting max keys to 1) the mock correctly returns configured number of keys and truncated status in the first response. However when listNextBatchOfObjects is invoked it returns whole bucket contents instead of just the next page.

Sample code:

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import io.findify.s3mock.S3Mock;

public class PagingTest {
    public static void main(String[] args) {
        S3Mock api = new S3Mock.Builder().withPort(8001).withInMemoryBackend().build();
        api.start();

        AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration("http://localhost:8001", "us-west-2");
        AmazonS3 client = AmazonS3ClientBuilder
                .standard()
                .withPathStyleAccessEnabled(true)
                .withEndpointConfiguration(endpoint)
                .withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()))
                .build();

        client.createBucket("testbucket");
        client.putObject("testbucket", "prefix/file2", "contents");
        client.putObject("testbucket", "prefix/file1", "contents");
        client.putObject("testbucket", "prefix/file3", "contents");
        client.putObject("testbucket", "prefix/file4", "contents");

        ObjectListing listing = client.listObjects(
                new ListObjectsRequest()
                        .withBucketName("testbucket")
                        .withPrefix("prefix/")
                        .withDelimiter("/")
                        .withMaxKeys(1));
        System.out.println("Objects returned: " + listing.getObjectSummaries());
        while (listing.isTruncated()) {
            listing = client.listNextBatchOfObjects(listing);
            System.out.println("Objects returned: " + listing.getObjectSummaries());
        }

        client.shutdown();
        api.shutdown();
    }
}