- Newest
- Most votes
- Most comments
Amazon S3 has a flat structure instead of a hierarchy like you would see in a file system[1]. Although it looks like a folder, it is basically a prefix before the name of any object. Therefore, S3 can not directly change the name of prefix/folder in the bucket.
Now coming back to the question whether deleted objects can be retrieved. So answer is YES, If you have already enabled versioning in your S3 bucket, you could retrieve objects that were deleted from a version-enabled bucket using following aws CLI command. This example command uses JQ Tool
aws s3api list-object-versions --bucket <BUCKET_NAME> --prefix examplefolder/ --output json --query 'DeleteMarkers[?IsLatest==`true`].[Key, VersionId]' | jq -r '.[] | "--key '\''" + .[0] + "'\'' --version-id " + .[1]' | xargs -L1 aws s3api delete-object --bucket <BUCKET_NAME>
In above command, please replace the BUCKET_NAME with your s3 bucket and <examplefolder> with your effected prefix (i.e. "temp"). Above command will restore all deleted objects under that prefix.
If you want to restore objects deleted after a certain date and time you can use below modified command to restore all objects deleted from specified prefix on April 22, 2023 @ 12 AM UTC or later.
aws s3api list-object-versions --bucket <BUCKET_NAME> --prefix logs/ --output json --query 'DeleteMarkers[?LastModified>=`2023-04-22T00:00:00` && IsLatest==`true`].[Key,VersionId]' | jq -r '.[] | "--key '\''" + .[0] + "'\'' --version-id " + .[1]' | xargs -L1 aws s3api delete-object --bucket <BUCKET_NAME>
Lastly, to move folders/prefix with in s3 bucket you can check our following document [2] which provide detail examples of mv command.
[1] - https://docs.aws.amazon.com/AmazonS3/latest/user-guide/using-folders.html
[2] - https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/mv.html
I would appreciate if this answer is accepted so that community can benefit for clarity!
Hi, A) There is a way to rename via S3 console:
- select your object
- button Actions > Edit actions > Rename Object
B) restore objects on a versioned bucket: see https://docs.aws.amazon.com/AmazonS3/latest/userguide/RestoringPreviousVersions.html
Relevant content
- asked 2 years ago
- Accepted Answerasked 10 months ago
- AWS OFFICIALUpdated 8 months ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 3 months ago
Two things... This lists the objects in the console(i.e. the console says "--key temp/ --version-id w.0VtHhAR79CUTswRLpwlMd11RTqLGNl" and some other directories, but it does not actually restore those directories and the files in them. Since I am brand new to AWS, I haven't played around with this bucket much. Restoring everything that has ever been deleted would serve my purposes if that's easier.