You can delete S3 objects using the AWS SDK for Java.
You can delete the specified object by specifying the bucket name and object key.
try {
final AmazonS3 s3 = new AmazonS3Client();
s3.deleteObject(bucket_name, object_key);
} catch (AmazonServiceException e) {
e.printStackTrace();
}
If you specify the bucket name and folder name, you can delete all the objects in the specified folder.
try {
final AmazonS3 s3 = new AmazonS3Client();
ObjectListing objListing = s3.listObjects(bucket_name,folder_nm);
List<S3ObjectSummary> objList = objListing.getObjectSummaries();
for (S3ObjectSummary obj : objList) {
s3.deleteObject(bucket_name, obj.getKey());
}
} catch (AmazonServiceException e) {
e.printStackTrace();
}
The following policy settings are required for s3.listObjects.
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: "s3:*"
Resource:
- "arn:aws:s3:::bucket-test-01"
- "arn:aws:s3:::bucket-test-01/*"
Recommended Posts