Can you restore deleted S3 files when you have deleted the folder?

0

I uploaded some files to S3, and I wanted to change the directory structure of my bucket. In order to rename files, I found searching on the internet that there was no way to do it through the console, so I used CloudShell to do it. Let's say the files were in /temp and I wanted to move them to /old/temp.

I did this: aws s3 --recursive mv s3://bucketname/temp s3://bucketname/old/temp

One thing that may be important is that I did not create the folder old/temp before I did this. After entering this command, /temp was gone, and there was an /old directory. I can't remember if there was an old/temp directory or not, but the main problem is that the files had disappeared. I did have file versioning enabled on the bucket, so I searched for how to use it. I created a new /temp and turned "Show versions" on, but they were not there. I also do not remember the exact name of the files, I would need to be able to see all files that have existed and be able to restore them, which seems like it would be something that should be possible with a version-enabled bucket. Are my files gone? With versioning on the bucket, shouldn't I be able to get back any files that were previously stored there? I need help! Any assistance would be greatly appreciated. Thanks!

asked a year ago520 views
2 Answers
4

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!

profile picture
EXPERT
answered a year 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.

0

Hi, A) There is a way to rename via S3 console:

  1. select your object
  2. button Actions > Edit actions > Rename Object

B) restore objects on a versioned bucket: see https://docs.aws.amazon.com/AmazonS3/latest/userguide/RestoringPreviousVersions.html

profile pictureAWS
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions