Deleting AWS S3 Objects in Java

You can delete S3 objects using the AWS SDK for Java.

Delete one object

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();
}

Delete all objects in the specified folder

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

Deleting AWS S3 Objects in Java
Renamed folders in AWS S3 (Java)
Try running AWS X-Ray in Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
[AWS IoT] Implementing Authorizing Direct Calls in Java [Java]
Upload and download notes in java on S3
Code that deletes all files of the specified prefix in AWS S3 (Java)
[java] sort in list
Compare objects in Swift
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
java classes, instances, objects
Comments in Java source
Azure functions in java
Browse class objects in Kotlin (instead of Java class name.class)
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Log aggregation and analysis (working with AWS Athena in Java)
[AWS] “Aws :: S3 :: Errors :: AccessDenied in 〇〇sController # create” error (not saved in AWS S3)
Create a SlackBot with AWS lambda & API Gateway in Java
Generate AWS Signature V4 in Java and request an API
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
AWS CLI install in Ubuntu 20.04
Edit ini in Java: ini4j
Let Java segfault in 6 lines
Try calling JavaScript in Java