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