Renamed folders in AWS S3 (Java)

I want to rename a folder on AWS S3

In the first place, AWS S3 does not have the concept of folders, so the name "folder" is inappropriate. (In this case, is it more appropriate to call it a "key"?)

For the time being, it is called a "folder" for convenience.

AWS S3 cannot rename a folder once created

You might think that you can't do it suddenly, but you can't. You can't change it from the Management Console.

Nande? I thought, I looked down for the time being and understood. That's right. It's just a key-value type data store ...

"Destroy the illusion of a" folder "in Amazon S3 and reveal its substance." https://dev.classmethod.jp/cloud/aws/amazon-s3-folders/

Create new folder → copy contents → delete original folder

It will be possible with the following. It's the same procedure as the rename command in Unix / Linux.

public void changeFolderName(String bucketName, String oldPrefix, String newPrefix){
    AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
    ObjectListing listing = s3.listObjects(bucketName, oldPrefix);

    for (S3ObjectSummary summary: listing.getObjectSummaries()) {
      String oldKey = summary.getKey();
      String newKey = new StringBuilder().append(newPrefix).append(oldKey.substring(oldPrefix.length())).toString();
      s3.copyObject(bucketName, oldKey, bucketName, newKey);
      s3.deleteObject(bucketName, oldKey);
    }
}

Recommended Posts

Renamed folders in AWS S3 (Java)
Deleting AWS S3 Objects in 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)
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
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands 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)
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
Java history in this world
Let Java segfault in 6 lines
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Write flyway callbacks in Java
Importing Excel data in Java 2