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

The following examples show how to use software.amazon.awssdk.services.s3.model.DeleteObjectRequest. 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: S3Helper.java    From realworld-serverless-application with Apache License 2.0 7 votes vote down vote up
public void emptyBucket(String bucketName){
  log.info("Emptying bucket {}", bucketName);

  ListObjectsV2Iterable listObjectsV2Responses = s3Client.listObjectsV2Paginator(ListObjectsV2Request.builder()
      .bucket(bucketName)
      .build());

  listObjectsV2Responses.stream()
      .flatMap(r -> r.contents().stream())
      .forEach(s3Object -> {
        log.info("deleting {}", s3Object.key());
        s3Client.deleteObject(DeleteObjectRequest.builder()
            .bucket(bucketName)
            .key(s3Object.key())
            .build());
      });
}
 
Example #2
Source File: S3BundlePersistenceProvider.java    From nifi-registry with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void deleteBundleVersion(final BundleVersionCoordinate versionCoordinate) throws BundlePersistenceException {
    final String key = getKey(versionCoordinate);
    LOGGER.debug("Deleting bundle version from S3 bucket '{}' with key '{}'", new Object[]{s3BucketName, key});

    final DeleteObjectRequest request = DeleteObjectRequest.builder()
            .bucket(s3BucketName)
            .key(key)
            .build();

    try {
        s3Client.deleteObject(request);
        LOGGER.debug("Successfully deleted bundle version from S3 bucket '{}' with key '{}'", new Object[]{s3BucketName, key});
    } catch (Exception e) {
        throw new BundlePersistenceException("Error deleting bundle version from S3 due to: " + e.getMessage(), e);
    }
}
 
Example #3
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 #4
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 #5
Source File: Synch.java    From pegasus with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #6
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();
    }
}
 
Example #7
Source File: S3WithUrlHttpClientIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private static void deleteObject(String bucket, String key) {
    DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(key).build();
    s3.deleteObject(deleteObjectRequest);
}
 
Example #8
Source File: S3BucketDeletion.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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);
        }
    }