Path: blob/master/code_examples/java_examples/S3Examples/DeleteMultipleObjectsVersionEnabledBucket.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.io.IOException;4import java.util.ArrayList;5import java.util.List;67import com.amazonaws.AmazonServiceException;8import com.amazonaws.SdkClientException;9import com.amazonaws.auth.profile.ProfileCredentialsProvider;10import com.amazonaws.services.s3.AmazonS3;11import com.amazonaws.services.s3.AmazonS3ClientBuilder;12import com.amazonaws.services.s3.model.BucketVersioningConfiguration;13import com.amazonaws.services.s3.model.DeleteObjectsRequest;14import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion;15import com.amazonaws.services.s3.model.DeleteObjectsResult;16import com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject;17import com.amazonaws.services.s3.model.PutObjectResult;1819public class DeleteMultipleObjectsVersionEnabledBucket {20private static AmazonS3 S3_CLIENT;21private static String VERSIONED_BUCKET_NAME;2223public static void main(String[] args) throws IOException {24String clientRegion = "*** Client region ***";25VERSIONED_BUCKET_NAME = "*** Bucket name ***";2627try {28S3_CLIENT = AmazonS3ClientBuilder.standard()29.withCredentials(new ProfileCredentialsProvider())30.withRegion(clientRegion)31.build();3233// Check to make sure that the bucket is versioning-enabled.34String bucketVersionStatus = S3_CLIENT.getBucketVersioningConfiguration(VERSIONED_BUCKET_NAME).getStatus();35if(!bucketVersionStatus.equals(BucketVersioningConfiguration.ENABLED)) {36System.out.printf("Bucket %s is not versioning-enabled.", VERSIONED_BUCKET_NAME);37}38else {39// Upload and delete sample objects, using specific object versions.40uploadAndDeleteObjectsWithVersions();4142// Upload and delete sample objects without specifying version IDs.43// Amazon S3 creates a delete marker for each object rather than deleting44// specific versions.45DeleteObjectsResult unversionedDeleteResult = uploadAndDeleteObjectsWithoutVersions();4647// Remove the delete markers placed on objects in the non-versioned create/delete method.48multiObjectVersionedDeleteRemoveDeleteMarkers(unversionedDeleteResult);49}50}51catch(AmazonServiceException e) {52// The call was transmitted successfully, but Amazon S3 couldn't process53// it, so it returned an error response.54e.printStackTrace();55}56catch(SdkClientException e) {57// Amazon S3 couldn't be contacted for a response, or the client58// couldn't parse the response from Amazon S3.59e.printStackTrace();60}61}6263private static void uploadAndDeleteObjectsWithVersions() {64System.out.println("Uploading and deleting objects with versions specified.");6566// Upload three sample objects.67ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();68for (int i = 0; i < 3; i++) {69String keyName = "delete object without version ID example " + i;70PutObjectResult putResult = S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName,71"Object number " + i + " to be deleted.");72// Gather the new object keys with version IDs.73keys.add(new KeyVersion(keyName, putResult.getVersionId()));74}7576// Delete the specified versions of the sample objects.77DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME)78.withKeys(keys)79.withQuiet(false);8081// Verify that the object versions were successfully deleted.82DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest);83int successfulDeletes = delObjRes.getDeletedObjects().size();84System.out.println(successfulDeletes + " objects successfully deleted");85}8687private static DeleteObjectsResult uploadAndDeleteObjectsWithoutVersions() {88System.out.println("Uploading and deleting objects with no versions specified.");8990// Upload three sample objects.91ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();92for (int i = 0; i < 3; i++) {93String keyName = "delete object with version ID example " + i;94S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName, "Object number " + i + " to be deleted.");95// Gather the new object keys without version IDs.96keys.add(new KeyVersion(keyName));97}9899// Delete the sample objects without specifying versions.100DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keys)101.withQuiet(false);102103// Verify that delete markers were successfully added to the objects.104DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest);105int successfulDeletes = delObjRes.getDeletedObjects().size();106System.out.println(successfulDeletes + " objects successfully marked for deletion without versions.");107return delObjRes;108}109110private static void multiObjectVersionedDeleteRemoveDeleteMarkers(DeleteObjectsResult response) {111List<KeyVersion> keyList = new ArrayList<KeyVersion>();112for (DeletedObject deletedObject : response.getDeletedObjects()) {113// Note that the specified version ID is the version ID for the delete marker.114keyList.add(new KeyVersion(deletedObject.getKey(), deletedObject.getDeleteMarkerVersionId()));115}116// Create a request to delete the delete markers.117DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keyList);118119// Delete the delete markers, leaving the objects intact in the bucket.120DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(deleteRequest);121int successfulDeletes = delObjRes.getDeletedObjects().size();122System.out.println(successfulDeletes + " delete markers successfully deleted");123}124}125126