Search in sources :

Example 16 with ListObjectsV2Request

use of com.amazonaws.services.s3.model.ListObjectsV2Request in project beam by apache.

the class S3FileSystemTest method matchGlobWithSlashes.

@Test
public void matchGlobWithSlashes() throws IOException {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("mys3"));
    S3ResourceId path = S3ResourceId.fromUri("mys3://testbucket/foo/bar\\baz*");
    ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(path.getBucket()).prefix(path.getKeyNonWildcardPrefix()).continuationToken(null).build();
    // Expected to be returned; prefix and wildcard/regex match
    S3Object firstMatch = S3Object.builder().key("foo/bar\\baz0").size(100L).lastModified(Instant.ofEpochMilli(1540000000001L)).build();
    // Expected to not be returned; prefix matches, but substring after wildcard does not
    S3Object secondMatch = S3Object.builder().key("foo/bar/baz1").size(200L).lastModified(Instant.ofEpochMilli(1540000000002L)).build();
    // Expected first request returns continuation token
    ListObjectsV2Response response = ListObjectsV2Response.builder().contents(firstMatch, secondMatch).build();
    when(s3FileSystem.getS3Client().listObjectsV2(argThat(new ListObjectsV2RequestArgumentMatches(request)))).thenReturn(response);
    // Expect object metadata queries for content encoding
    HeadObjectResponse headObjectResponse = HeadObjectResponse.builder().contentEncoding("").build();
    when(s3FileSystem.getS3Client().headObject(any(HeadObjectRequest.class))).thenReturn(headObjectResponse);
    assertThat(s3FileSystem.matchGlobPaths(ImmutableList.of(path)).get(0), MatchResultMatcher.create(ImmutableList.of(MatchResult.Metadata.builder().setIsReadSeekEfficient(true).setResourceId(S3ResourceId.fromComponents("mys3", path.getBucket(), firstMatch.key())).setSizeBytes(firstMatch.size()).setLastModifiedMillis(firstMatch.lastModified().toEpochMilli()).build())));
}
Also used : HeadObjectResponse(software.amazon.awssdk.services.s3.model.HeadObjectResponse) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) S3Object(software.amazon.awssdk.services.s3.model.S3Object) HeadObjectRequest(software.amazon.awssdk.services.s3.model.HeadObjectRequest) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws2.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Example 17 with ListObjectsV2Request

use of com.amazonaws.services.s3.model.ListObjectsV2Request in project beam by apache.

the class S3FileSystemTest method matchGlob.

@Test
public void matchGlob() throws IOException {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("mys3"));
    S3ResourceId path = S3ResourceId.fromUri("mys3://testbucket/foo/bar*baz");
    ListObjectsV2Request firstRequest = ListObjectsV2Request.builder().bucket(path.getBucket()).prefix(path.getKeyNonWildcardPrefix()).continuationToken(null).build();
    // Expected to be returned; prefix and wildcard/regex match
    S3Object firstMatch = S3Object.builder().key("foo/bar0baz").size(100L).lastModified(Instant.ofEpochMilli(1540000000001L)).build();
    // Expected to not be returned; prefix matches, but substring after wildcard does not
    S3Object secondMatch = S3Object.builder().key("foo/bar1qux").size(200L).lastModified(Instant.ofEpochMilli(1540000000002L)).build();
    // Expected first request returns continuation token
    ListObjectsV2Response firstResponse = ListObjectsV2Response.builder().nextContinuationToken("token").contents(firstMatch, secondMatch).build();
    when(s3FileSystem.getS3Client().listObjectsV2(argThat(new ListObjectsV2RequestArgumentMatches(firstRequest)))).thenReturn(firstResponse);
    // Expect second request with continuation token
    ListObjectsV2Request secondRequest = ListObjectsV2Request.builder().bucket(path.getBucket()).prefix(path.getKeyNonWildcardPrefix()).continuationToken("token").build();
    // Expected to be returned; prefix and wildcard/regex match
    S3Object thirdMatch = S3Object.builder().key("foo/bar2baz").size(300L).lastModified(Instant.ofEpochMilli(1540000000003L)).build();
    // Expected second request returns third prefix match and no continuation token
    ListObjectsV2Response secondResponse = ListObjectsV2Response.builder().nextContinuationToken(null).contents(thirdMatch).build();
    when(s3FileSystem.getS3Client().listObjectsV2(argThat(new ListObjectsV2RequestArgumentMatches(secondRequest)))).thenReturn(secondResponse);
    // Expect object metadata queries for content encoding
    HeadObjectResponse headObjectResponse = HeadObjectResponse.builder().contentEncoding("").build();
    when(s3FileSystem.getS3Client().headObject(any(HeadObjectRequest.class))).thenReturn(headObjectResponse);
    assertThat(s3FileSystem.matchGlobPaths(ImmutableList.of(path)).get(0), MatchResultMatcher.create(ImmutableList.of(MatchResult.Metadata.builder().setIsReadSeekEfficient(true).setResourceId(S3ResourceId.fromComponents("mys3", path.getBucket(), firstMatch.key())).setSizeBytes(firstMatch.size()).setLastModifiedMillis(firstMatch.lastModified().toEpochMilli()).build(), MatchResult.Metadata.builder().setIsReadSeekEfficient(true).setResourceId(S3ResourceId.fromComponents("mys3", path.getBucket(), thirdMatch.key())).setSizeBytes(thirdMatch.size()).setLastModifiedMillis(thirdMatch.lastModified().toEpochMilli()).build())));
}
Also used : HeadObjectResponse(software.amazon.awssdk.services.s3.model.HeadObjectResponse) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) S3Object(software.amazon.awssdk.services.s3.model.S3Object) HeadObjectRequest(software.amazon.awssdk.services.s3.model.HeadObjectRequest) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws2.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Example 18 with ListObjectsV2Request

use of com.amazonaws.services.s3.model.ListObjectsV2Request in project beam by apache.

the class S3FileSystemTest method matchGlobWithS3Options.

@Test
public void matchGlobWithS3Options() throws IOException {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());
    S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/foo/bar*baz");
    ListObjectsV2Request firstRequest = new ListObjectsV2Request().withBucketName(path.getBucket()).withPrefix(path.getKeyNonWildcardPrefix()).withContinuationToken(null);
    // Expected to be returned; prefix and wildcard/regex match
    S3ObjectSummary firstMatch = new S3ObjectSummary();
    firstMatch.setBucketName(path.getBucket());
    firstMatch.setKey("foo/bar0baz");
    firstMatch.setSize(100);
    firstMatch.setLastModified(new Date(1540000000001L));
    // Expected to not be returned; prefix matches, but substring after wildcard does not
    S3ObjectSummary secondMatch = new S3ObjectSummary();
    secondMatch.setBucketName(path.getBucket());
    secondMatch.setKey("foo/bar1qux");
    secondMatch.setSize(200);
    secondMatch.setLastModified(new Date(1540000000002L));
    // Expected first request returns continuation token
    ListObjectsV2Result firstResult = new ListObjectsV2Result();
    firstResult.setNextContinuationToken("token");
    firstResult.getObjectSummaries().add(firstMatch);
    firstResult.getObjectSummaries().add(secondMatch);
    when(s3FileSystem.getAmazonS3Client().listObjectsV2(argThat(new ListObjectsV2RequestArgumentMatches(firstRequest)))).thenReturn(firstResult);
    // Expect second request with continuation token
    ListObjectsV2Request secondRequest = new ListObjectsV2Request().withBucketName(path.getBucket()).withPrefix(path.getKeyNonWildcardPrefix()).withContinuationToken("token");
    // Expected to be returned; prefix and wildcard/regex match
    S3ObjectSummary thirdMatch = new S3ObjectSummary();
    thirdMatch.setBucketName(path.getBucket());
    thirdMatch.setKey("foo/bar2baz");
    thirdMatch.setSize(300);
    thirdMatch.setLastModified(new Date(1540000000003L));
    // Expected second request returns third prefix match and no continuation token
    ListObjectsV2Result secondResult = new ListObjectsV2Result();
    secondResult.setNextContinuationToken(null);
    secondResult.getObjectSummaries().add(thirdMatch);
    when(s3FileSystem.getAmazonS3Client().listObjectsV2(argThat(new ListObjectsV2RequestArgumentMatches(secondRequest)))).thenReturn(secondResult);
    // Expect object metadata queries for content encoding
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentEncoding("");
    when(s3FileSystem.getAmazonS3Client().getObjectMetadata(anyObject())).thenReturn(metadata);
    assertThat(s3FileSystem.matchGlobPaths(ImmutableList.of(path)).get(0), MatchResultMatcher.create(ImmutableList.of(MatchResult.Metadata.builder().setIsReadSeekEfficient(true).setResourceId(S3ResourceId.fromComponents("s3", firstMatch.getBucketName(), firstMatch.getKey())).setSizeBytes(firstMatch.getSize()).setLastModifiedMillis(firstMatch.getLastModified().getTime()).build(), MatchResult.Metadata.builder().setIsReadSeekEfficient(true).setResourceId(S3ResourceId.fromComponents("s3", thirdMatch.getBucketName(), thirdMatch.getKey())).setSizeBytes(thirdMatch.getSize()).setLastModifiedMillis(thirdMatch.getLastModified().getTime()).build())));
}
Also used : ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Date(java.util.Date) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Example 19 with ListObjectsV2Request

use of com.amazonaws.services.s3.model.ListObjectsV2Request in project beam by apache.

the class S3FileSystemTest method matchGlobWithSlashes.

@Test
public void matchGlobWithSlashes() throws IOException {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("s3"));
    S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/foo/bar\\baz*");
    ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(path.getBucket()).withPrefix(path.getKeyNonWildcardPrefix()).withContinuationToken(null);
    // Expected to be returned; prefix and wildcard/regex match
    S3ObjectSummary firstMatch = new S3ObjectSummary();
    firstMatch.setBucketName(path.getBucket());
    firstMatch.setKey("foo/bar\\baz0");
    firstMatch.setSize(100);
    firstMatch.setLastModified(new Date(1540000000001L));
    // Expected to not be returned; prefix matches, but substring after wildcard does not
    S3ObjectSummary secondMatch = new S3ObjectSummary();
    secondMatch.setBucketName(path.getBucket());
    secondMatch.setKey("foo/bar/baz1");
    secondMatch.setSize(200);
    secondMatch.setLastModified(new Date(1540000000002L));
    // Expected first request returns continuation token
    ListObjectsV2Result result = new ListObjectsV2Result();
    result.getObjectSummaries().add(firstMatch);
    result.getObjectSummaries().add(secondMatch);
    when(s3FileSystem.getAmazonS3Client().listObjectsV2(argThat(new ListObjectsV2RequestArgumentMatches(request)))).thenReturn(result);
    // Expect object metadata queries for content encoding
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentEncoding("");
    when(s3FileSystem.getAmazonS3Client().getObjectMetadata(anyObject())).thenReturn(metadata);
    assertThat(s3FileSystem.matchGlobPaths(ImmutableList.of(path)).get(0), MatchResultMatcher.create(ImmutableList.of(MatchResult.Metadata.builder().setIsReadSeekEfficient(true).setResourceId(S3ResourceId.fromComponents("s3", firstMatch.getBucketName(), firstMatch.getKey())).setSizeBytes(firstMatch.getSize()).setLastModifiedMillis(firstMatch.getLastModified().getTime()).build())));
}
Also used : ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Date(java.util.Date) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Example 20 with ListObjectsV2Request

use of com.amazonaws.services.s3.model.ListObjectsV2Request in project syndesis-qe by syndesisio.

the class S3Utils method deleteS3Bucket.

public void deleteS3Bucket(String bucketName) {
    ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucketName).build();
    ListObjectsV2Response listObjectsV2Response;
    do {
        listObjectsV2Response = s3client.listObjectsV2(listObjectsV2Request);
        for (S3Object s3Object : listObjectsV2Response.contents()) {
            s3client.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(s3Object.key()).build());
        }
        listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucketName).continuationToken(listObjectsV2Response.nextContinuationToken()).build();
    } while (listObjectsV2Response.isTruncated());
    s3client.deleteBucket(b -> b.bucket(bucketName));
}
Also used : ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Aggregations

ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)15 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)15 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)12 Date (java.util.Date)8 ListObjectsV2Request (software.amazon.awssdk.services.s3.model.ListObjectsV2Request)8 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)8 Test (org.junit.Test)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)7 ArrayList (java.util.ArrayList)6 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 IOException (java.io.IOException)4 S3TestUtils.buildMockedS3FileSystem (org.apache.beam.sdk.io.aws.s3.S3TestUtils.buildMockedS3FileSystem)4 AmazonS3 (com.amazonaws.services.s3.AmazonS3)3 Collection (java.util.Collection)3 List (java.util.List)3 Pattern (java.util.regex.Pattern)3 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonS3URI (com.amazonaws.services.s3.AmazonS3URI)2