software.amazon.awssdk.services.s3.model.ListObjectsResponse Java Examples

The following examples show how to use software.amazon.awssdk.services.s3.model.ListObjectsResponse. 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: DecodeUrlEncodedResponseInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context,
                                  ExecutionAttributes executionAttributes) {
    SdkResponse response = context.response();
    if (shouldHandle(response)) {
        if (response instanceof ListObjectsResponse) {
            return modifyListObjectsResponse((ListObjectsResponse) response);
        }

        if (response instanceof ListObjectsV2Response) {
            return modifyListObjectsV2Response((ListObjectsV2Response) response);
        }

        if (response instanceof ListObjectVersionsResponse) {
            return modifyListObjectVersionsResponse((ListObjectVersionsResponse) response);
        }

        if (response instanceof ListMultipartUploadsResponse) {
            return modifyListMultipartUploadsResponse((ListMultipartUploadsResponse) response);
        }
    }
    return response;
}
 
Example #2
Source File: ListObjectsIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void listObjectsNoParameters() {
    ListObjectsResponse result = s3.listObjects(ListObjectsRequest.builder().bucket(bucketName).build());
    List<S3Object> objects = result.contents();

    assertEquals(keys.size(), objects.size());
    assertEquals(bucketName, result.name());
    assertS3ObjectSummariesAreValid(objects);
    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());

    // We didn't set other request parameters, so we expect them to be empty
    assertNull(result.encodingType());
    assertThat(result.prefix()).isEmpty();
}
 
Example #3
Source File: ListObjectsIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void listObjectsWithAllElements() {
    String delimiter = "/";
    String marker = "aaa";
    ListObjectsResponse result = s3.listObjects(ListObjectsRequest.builder()
                                                                  .bucket(bucketName)
                                                                  .prefix(KEY_NAME_WITH_SPECIAL_CHARS)
                                                                  .marker(marker)
                                                                  .encodingType(EncodingType.URL)
                                                                  .delimiter(delimiter)
                                                                  .build());
    List<S3Object> objects = result.contents();

    assertEquals(bucketName, result.name());
    assertS3ObjectSummariesAreValid(objects);
    assertEquals(marker, result.marker());
    assertEquals(delimiter, result.delimiter());
    assertEquals(KEY_NAME_WITH_SPECIAL_CHARS, result.prefix());

    assertFalse(result.isTruncated());
    assertTrue(result.maxKeys() >= 1000);
}
 
Example #4
Source File: ListObjects.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void listBucketObjects(S3Client s3, String bucketName ) {

       try {
            ListObjectsRequest listObjects = ListObjectsRequest
                    .builder()
                    .bucket(bucketName)
                    .build();

            ListObjectsResponse res = s3.listObjects(listObjects);
            List<S3Object> objects = res.contents();

            for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
                S3Object myValue = (S3Object) iterVals.next();
                System.out.print("\n The name of the key is " + myValue.key());
                System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
                System.out.print("\n The owner is " + myValue.owner());
                }
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
 
Example #5
Source File: S3Repository.java    From djl with Apache License 2.0 5 votes vote down vote up
private Artifact listFiles() {
    ListObjectsRequest req =
            ListObjectsRequest.builder()
                    .bucket(bucket)
                    .maxKeys(100)
                    .prefix(prefix)
                    .delimiter("/")
                    .build();

    ListObjectsResponse resp = client.listObjects(req);
    List<S3Object> list = resp.contents();
    if (list.isEmpty()) {
        return null;
    }

    Artifact artifact = new Artifact();
    artifact.setName(modelName);
    Map<String, Artifact.Item> files = new ConcurrentHashMap<>();
    for (S3Object obj : list) {
        Artifact.Item item = new Artifact.Item();
        String key = obj.key();
        if (!key.endsWith("/")) {
            item.setUri(key);
            item.setSize(obj.size());
            item.setArtifact(artifact);
            if ("dir".equals(item.getType())) {
                item.setName(""); // avoid creating extra folder
            }
            files.put(key, item);
        }
    }
    artifact.setFiles(files);
    return artifact;
}
 
Example #6
Source File: DecodeUrlEncodedResponseInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static SdkResponse modifyListObjectsResponse(ListObjectsResponse response) {
    return response.toBuilder()
                   .delimiter(urlDecode(response.delimiter()))
                   .marker(urlDecode(response.marker()))
                   .prefix(urlDecode(response.prefix()))
                   .nextMarker(urlDecode(response.nextMarker()))
                   .contents(decodeContents(response.contents()))
                   .commonPrefixes(decodeCommonPrefixes(response.commonPrefixes()))
                   .build();
}
 
Example #7
Source File: ListObjectsIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void listObjectsWithMaxKeys() {
    int maxKeys = 4;
    ListObjectsResponse result = s3.listObjects(ListObjectsRequest.builder()
                                                                  .bucket(bucketName)
                                                                  .maxKeys(maxKeys)
                                                                  .build());

    List<S3Object> objects = result.contents();

    assertEquals(maxKeys, objects.size());
    assertEquals(bucketName, result.name());
    assertThat(maxKeys).isEqualTo(result.maxKeys());
    assertS3ObjectSummariesAreValid(objects);

    // 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());

    // 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()).isEmpty();
    assertNull(result.delimiter());
}
 
Example #8
Source File: UrlEncodingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void listObjectWithUrlEncodingType_shouldDecode() {
    ListObjectsResponse listObjectsV2Response =
        s3.listObjects(b -> b.bucket(BUCKET_NAME).encodingType(EncodingType.URL));

    listObjectsV2Response.contents().forEach(c -> assertKeyIsDecoded(c.key()));
    ListObjectVersionsResponse asyncResponse =
        s3Async.listObjectVersions(b -> b.bucket(BUCKET_NAME).encodingType(EncodingType.URL)).join();

    asyncResponse.versions().forEach(v -> assertKeyIsDecoded(v.key()));
}
 
Example #9
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void encodingTypeSet_decodesListObjectsResponseParts() {
    Context.ModifyResponse ctx = newContext(V1_TEST_ENCODED_RESPONSE);

    ListObjectsResponse decoded = (ListObjectsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());

    assertDecoded(decoded::delimiter, " delimiter");
    assertDecoded(decoded::nextMarker, " nextmarker");
    assertDecoded(decoded::prefix, " prefix");
    assertDecoded(decoded::marker, " marker");
    assertKeysAreDecoded(decoded.contents());
    assertCommonPrefixesAreDecoded(decoded.commonPrefixes());
}
 
Example #10
Source File: DecodeUrlEncodedResponseInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void encodingTypeNotSet_doesNotDecodeListObjectsResponseParts() {
    ListObjectsResponse original = V1_TEST_ENCODED_RESPONSE.toBuilder()
            .encodingType((String) null)
            .build();

    Context.ModifyResponse ctx = newContext(original);

    ListObjectsResponse fromInterceptor = (ListObjectsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());

    assertThat(fromInterceptor).isEqualTo(original);
}
 
Example #11
Source File: CloudTrailIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
public static void deleteBucketAndAllContents(String bucketName) {
    System.out.println("Deleting S3 bucket: " + bucketName);
    ListObjectsResponse response = s3.listObjects(ListObjectsRequest.builder().bucket(bucketName).build());

    while (true) {
        if (response.contents() == null) {
            break;
        }
        for (Iterator<?> iterator = response.contents().iterator(); iterator
                .hasNext(); ) {
            S3Object objectSummary = (S3Object) iterator.next();
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build());
        }

        if (response.isTruncated()) {
            response = s3.listObjects(ListObjectsRequest.builder().marker(response.nextMarker()).build());
        } else {
            break;
        }
    }

    ListObjectVersionsResponse versionsResponse = s3
            .listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build());
    if (versionsResponse.versions() != null) {
        for (ObjectVersion s : versionsResponse.versions()) {
            s3.deleteObject(DeleteObjectRequest.builder()
                                               .bucket(bucketName)
                                               .key(s.key())
                                               .versionId(s.versionId())
                                               .build());
        }
    }

    s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
}
 
Example #12
Source File: S3BundlePersistenceProvider.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void deleteAllBundleVersions(final BundleCoordinate bundleCoordinate) throws BundlePersistenceException {
    final String basePrefix = s3KeyPrefix == null ? "" : s3KeyPrefix + "/";
    final String bundlePrefix = getBundlePrefix(bundleCoordinate.getBucketId(), bundleCoordinate.getGroupId(), bundleCoordinate.getArtifactId());

    final String prefix = basePrefix + bundlePrefix;
    LOGGER.debug("Deleting all bundle versions from S3 bucket '{}' with prefix '{}'", new Object[]{s3BucketName, prefix});

    try {
        // List all the objects in the bucket with the given prefix of group/artifact...
        final ListObjectsResponse objectsResponse = s3Client.listObjects(
                ListObjectsRequest.builder()
                        .bucket(s3BucketName)
                        .prefix(prefix)
                        .build()
        );

        // Now delete each object, might be able to do this more efficiently with bulk delete
        for (final S3Object s3Object : objectsResponse.contents()) {
            final String s3ObjectKey = s3Object.key();
            s3Client.deleteObject(DeleteObjectRequest.builder()
                    .bucket(s3BucketName)
                    .key(s3ObjectKey)
                    .build()
            );
            LOGGER.debug("Successfully object from S3 bucket '{}' with key '{}'", new Object[]{s3BucketName, s3ObjectKey});
        }

        LOGGER.debug("Successfully deleted all bundle versions from S3 bucket '{}' with prefix '{}'", new Object[]{s3BucketName, prefix});
    } catch (Exception e) {
        throw new BundlePersistenceException("Error deleting bundle versions from S3 due to: " + e.getMessage(), e);
    }
}
 
Example #13
Source File: S3ListObjects.java    From piper with Apache License 2.0 5 votes vote down vote up
@Override
public List<S3ObjectDescription> handle (TaskExecution aTask) throws Exception {
  
  S3Client s3 = S3Client.builder().build();
  
  ListObjectsResponse response = s3.listObjects(ListObjectsRequest.builder()
                                   .bucket(aTask.getRequiredString("bucket"))
                                   .prefix(aTask.getRequiredString("prefix"))
                                   .build());
  
  return response.contents()
                 .stream()
                 .map(o->new S3ObjectDescription(aTask.getRequiredString("bucket"),o))
                 .collect(Collectors.toList());
}
 
Example #14
Source File: S3AsyncClientResource.java    From quarkus-quickstarts with Apache License 2.0 4 votes vote down vote up
private List<FileObject> toFileItems(ListObjectsResponse objects) {
    return objects.contents().stream()
            .sorted(Comparator.comparing(S3Object::lastModified).reversed())
            .map(FileObject::from).collect(Collectors.toList());
}
 
Example #15
Source File: S3TestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static void deleteBucketAndAllContents(S3Client s3, String bucketName) {
    try {
        System.out.println("Deleting S3 bucket: " + bucketName);
        ListObjectsResponse response = Waiter.run(() -> s3.listObjects(r -> r.bucket(bucketName)))
                                             .ignoringException(NoSuchBucketException.class)
                                             .orFail();
        List<S3Object> objectListing = response.contents();

        if (objectListing != null) {
            while (true) {
                for (Iterator<?> iterator = objectListing.iterator(); iterator.hasNext(); ) {
                    S3Object objectSummary = (S3Object) iterator.next();
                    s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build());
                }

                if (response.isTruncated()) {
                    objectListing = s3.listObjects(ListObjectsRequest.builder()
                                                                     .bucket(bucketName)
                                                                     .marker(response.marker())
                                                                     .build())
                                      .contents();
                } else {
                    break;
                }
            }
        }


        ListObjectVersionsResponse versions = s3
                .listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build());

        if (versions.deleteMarkers() != null) {
            versions.deleteMarkers().forEach(v -> s3.deleteObject(DeleteObjectRequest.builder()
                                                                                     .versionId(v.versionId())
                                                                                     .bucket(bucketName)
                                                                                     .key(v.key())
                                                                                     .build()));
        }

        if (versions.versions() != null) {
            versions.versions().forEach(v -> s3.deleteObject(DeleteObjectRequest.builder()
                                                                                .versionId(v.versionId())
                                                                                .bucket(bucketName)
                                                                                .key(v.key())
                                                                                .build()));
        }

        s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
    } catch (Exception e) {
        System.err.println("Failed to delete bucket: " + bucketName);
        e.printStackTrace();
    }
}