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.
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/
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