AWS Code Sample
Catalog

DeleteObjectVersionEnabledBucket.java

DeleteObjectVersionEnabledBucket.java demonstrates how to delete an S3 object version from a version-enabled bucket.

/** * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import java.io.IOException; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.BucketVersioningConfiguration; import com.amazonaws.services.s3.model.DeleteVersionRequest; import com.amazonaws.services.s3.model.PutObjectResult; public class DeleteObjectVersionEnabledBucket { public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String keyName = "*** Key name ****"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Check to ensure that the bucket is versioning-enabled. String bucketVersionStatus = s3Client.getBucketVersioningConfiguration(bucketName).getStatus(); if(!bucketVersionStatus.equals(BucketVersioningConfiguration.ENABLED)) { System.out.printf("Bucket %s is not versioning-enabled.", bucketName); } else { // Add an object. PutObjectResult putResult = s3Client.putObject(bucketName, keyName, "Sample content for deletion example."); System.out.printf("Object %s added to bucket %s\n", keyName, bucketName); // Delete the version of the object that we just created. System.out.println("Deleting versioned object " + keyName); s3Client.deleteVersion(new DeleteVersionRequest(bucketName, keyName, putResult.getVersionId())); System.out.printf("Object %s, version %s deleted\n", keyName, putResult.getVersionId()); } } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }

Sample Details

Service: s3

Last tested: 2019-01-28

Author: AWS

Type: full-example

On this page: