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)
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
Callable Interface 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
Get Locale objects for all locales available 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
Log aggregation and analysis (working with AWS Athena in Java)
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
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
Implement two-step verification in Java
Refactoring: Make Blackjack in Java