Java Code Examples for software.amazon.awssdk.services.s3.model.ListObjectsV2Response#contents()
The following examples show how to use
software.amazon.awssdk.services.s3.model.ListObjectsV2Response#contents() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3ListObjectsV2IntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void testListNoParameters() { ListObjectsV2Response result = s3.listObjectsV2(ListObjectsV2Request.builder().bucket(bucketName).build()); List<S3Object> objects = result.contents(); assertEquals(keys.size(), objects.size()); assertThat(keys.size(), equalTo(result.keyCount())); assertEquals(bucketName, result.name()); assertS3ObjectSummariesAreValid(objects, false); assertNotNull(result.maxKeys()); // We didn't use a delimiter, so we expect these to be empty/null assertNull(result.delimiter()); // We don't expect any truncated results assertFalse(result.isTruncated()); assertNull(result.nextContinuationToken()); // We didn't set other request parameters, so we expect them to be empty assertNull(result.encodingType()); assertThat(result.prefix(), equalTo("")); assertNull(result.continuationToken()); }
Example 2
Source File: S3ListObjectsV2IntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void testListWithEncodingType() { String encodingType = "url"; ListObjectsV2Response result = s3.listObjectsV2(ListObjectsV2Request.builder() .bucket(bucketName) .prefix(KEY_NAME_WITH_SPECIAL_CHARS) .encodingType(encodingType) .build()); List<S3Object> objects = result.contents(); // EncodingType should be returned in the response. assertEquals(encodingType, result.encodingTypeAsString()); System.out.println(result.contents().get(0).key()); // The key name returned in the response should have been decoded // from the URL encoded form S3 returned us. assertEquals(KEY_NAME_WITH_SPECIAL_CHARS, objects.get(0).key()); }
Example 3
Source File: S3ListObjectsV2IntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void testListWithPrefixAndStartAfter() { String prefix = "key"; String startAfter = "key-01"; ListObjectsV2Response result = s3.listObjectsV2(ListObjectsV2Request.builder() .bucket(bucketName) .prefix(prefix) .startAfter(startAfter) .build()); List<S3Object> objects = result.contents(); assertEquals(BUCKET_OBJECTS - 1, objects.size()); assertEquals(bucketName, result.name()); assertS3ObjectSummariesAreValid(objects, false); assertEquals(startAfter, result.startAfter()); assertEquals(prefix, result.prefix()); // We didn't use a delimiter, so we expect it to be empty/null assertNull(result.delimiter()); // We don't expect any truncated results assertFalse(result.isTruncated()); assertNull(result.nextContinuationToken()); // We didn't set any other request parameters, so we expect them to be // set to the defaults. assertTrue(result.maxKeys() >= 1000); assertNull(result.encodingType()); }
Example 4
Source File: S3ListObjectsV2IntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void testListWithPrefixAndDelimiter() { String prefix = "a"; String delimiter = "/"; ListObjectsV2Response result = s3.listObjectsV2(ListObjectsV2Request.builder() .bucket(bucketName) .prefix(prefix) .delimiter(delimiter) .build()); List<S3Object> objects = result.contents(); assertEquals(1, objects.size()); assertEquals(bucketName, result.name()); assertS3ObjectSummariesAreValid(objects, false); assertEquals(prefix, result.prefix()); assertEquals(delimiter, result.delimiter()); // We don't expect any truncated results assertFalse(result.isTruncated()); assertNull(result.nextContinuationToken()); // We didn't set other request parameters, so we expect them to be empty assertNull(result.startAfter()); assertNull(result.encodingType()); assertTrue(result.maxKeys() >= 1000); }
Example 5
Source File: S3ListObjectsV2IntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void testListWithMaxKeys() { int maxKeys = 4; ListObjectsV2Response result = s3.listObjectsV2(ListObjectsV2Request.builder() .bucket(bucketName) .maxKeys(maxKeys) .build()); List<S3Object> objects = result.contents(); assertEquals(maxKeys, objects.size()); assertEquals(bucketName, result.name()); assertThat(maxKeys, equalTo(result.maxKeys())); assertS3ObjectSummariesAreValid(objects, false); // We didn't use a delimiter, so we expect this to be empty/null assertNull(result.delimiter()); // We expect truncated results since we set maxKeys assertTrue(result.isTruncated()); assertNotNull(result.nextContinuationToken()); assertTrue(result.nextContinuationToken().length() > 1); // URL encoding is requested by default // We didn't set other request parameters, so we expect them to be empty assertNull(result.encodingType()); assertThat(result.prefix(), isEmptyString()); assertNull(result.startAfter()); assertNull(result.delimiter()); }
Example 6
Source File: S3ListObjectsV2IntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void testListWithFetchOwner() { ListObjectsV2Response result = s3.listObjectsV2(ListObjectsV2Request.builder() .bucket(bucketName) .fetchOwner(true) .build()); List<S3Object> objects = result.contents(); assertS3ObjectSummariesAreValid(objects, true); }
Example 7
Source File: S3ListObjectsV2IntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void testListPagination() { int firstRequestMaxKeys = 4; String prefix = "key"; ListObjectsV2Response result = s3.listObjectsV2(ListObjectsV2Request.builder() .bucket(bucketName) .prefix(prefix) .maxKeys(firstRequestMaxKeys) .build()); List<S3Object> objects = result.contents(); assertEquals(firstRequestMaxKeys, objects.size()); assertEquals(bucketName, result.name()); assertEquals(prefix, result.prefix()); assertNotNull(result.nextContinuationToken()); assertTrue(result.isTruncated()); assertS3ObjectSummariesAreValid(objects, false); for (int i = 0; i < firstRequestMaxKeys; i++) { assertEquals(keys.get(i), objects.get(i).key()); } ListObjectsV2Response nextResults = s3.listObjectsV2(ListObjectsV2Request.builder() .bucket(bucketName) .prefix(prefix) .continuationToken( result.nextContinuationToken()) .build()); List<S3Object> nextObjects = nextResults.contents(); assertNull(nextResults.nextContinuationToken()); assertEquals(nextResults.continuationToken(), result.nextContinuationToken()); assertFalse(nextResults.isTruncated()); assertEquals(prefix, nextResults.prefix()); assertS3ObjectSummariesAreValid(nextObjects, false); assertEquals(nextObjects.size(), BUCKET_OBJECTS - firstRequestMaxKeys); }
Example 8
Source File: S3PinotFS.java From incubator-pinot with Apache License 2.0 | 5 votes |
private boolean isEmptyDirectory(URI uri) throws IOException { if (!isDirectory(uri)) { return false; } String prefix = normalizeToDirectoryPrefix(uri); boolean isEmpty = true; ListObjectsV2Response listObjectsV2Response; ListObjectsV2Request.Builder listObjectsV2RequestBuilder = ListObjectsV2Request.builder().bucket(uri.getHost()); if (!prefix.equals(DELIMITER)) { listObjectsV2RequestBuilder = listObjectsV2RequestBuilder.prefix(prefix); } ListObjectsV2Request listObjectsV2Request = listObjectsV2RequestBuilder.build(); listObjectsV2Response = _s3Client.listObjectsV2(listObjectsV2Request); for (S3Object s3Object : listObjectsV2Response.contents()) { if (s3Object.key().equals(prefix)) { continue; } else { isEmpty = false; break; } } return isEmpty; }
Example 9
Source File: Synch.java From pegasus with Apache License 2.0 | 5 votes |
/** * Delete a S3 bucket with the given name * * @param name * @return */ public boolean deleteS3Bucket(String name) { boolean deleted = true; ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(name).build(); ListObjectsV2Response listObjectsV2Response; S3Client s3Client = S3Client.builder().region(mAWSRegion).build(); do { listObjectsV2Response = s3Client.listObjectsV2(listObjectsV2Request); if (listObjectsV2Response.contents() != null) { // detelete the files in the bucket for (S3Object s3Object : listObjectsV2Response.contents()) { mLogger.debug("Deleteing file " + s3Object.key() + " from bucket " + name); s3Client.deleteObject( DeleteObjectRequest.builder().bucket(name).key(s3Object.key()).build()); } } listObjectsV2Request = ListObjectsV2Request.builder() .bucket(name) .continuationToken(listObjectsV2Response.nextContinuationToken()) .build(); } while (listObjectsV2Response.isTruncated()); // Delete empty bucket DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(name).build(); s3Client.deleteBucket(deleteBucketRequest); return deleted; }
Example 10
Source File: S3BucketDeletion.java From aws-doc-sdk-examples with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { final String USAGE = "\n" + "Usage:\n" + " S3BucketDeletion <bucket>\n\n" + "Where:\n" + " bucket - the bucket to delete (i.e., bucket1)\n\n" + "Example:\n" + " bucket1\n\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } String bucket = args[0]; //Create the S3Client object Region region = Region.US_WEST_2; s3 = S3Client.builder().region(region).build(); try { // snippet-start:[s3.java2.s3_bucket_ops.delete_bucket] // To delete a bucket, all the objects in the bucket must be deleted first ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket).build(); ListObjectsV2Response listObjectsV2Response; do { listObjectsV2Response = s3.listObjectsV2(listObjectsV2Request); for (S3Object s3Object : listObjectsV2Response.contents()) { s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(s3Object.key()).build()); } listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket) .continuationToken(listObjectsV2Response.nextContinuationToken()) .build(); } while(listObjectsV2Response.isTruncated()); // snippet-end:[s3.java2.s3_bucket_ops.delete_bucket] DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build(); s3.deleteBucket(deleteBucketRequest); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }