How can I retrieve an Amazon S3 object that was deleted in a versioning-enabled bucket?
I want to retrieve an object that was deleted from my Amazon Simple Storage Service (Amazon S3) bucket that has versioning turned on.
Short description
When you delete an object from a version-enabled bucket, Amazon S3 creates a delete marker for the object. The delete marker becomes the current version of the object, and the object becomes the previous version. With a delete marker, Amazon S3 responds to requests for the object as though the object was deleted. For example, if you send a GET request for the object, then Amazon S3 returns an error.
Use one of the following ways to retrieve an object that was deleted from a version-enabled bucket:
- Download the previous version of the object: To download the previous version of the object, you must have s3:GetObjectVersion permissions.
- Remove the delete marker: After you remove the delete marker, the original object becomes the current version of the object. To remove the delete marker, you must have s3:DeleteObjectVersion permissions. Also, you must use the AWS account that owns or created the bucket to remove the delete marker
Note: If the bucket has MFA delete configured, you must use the designated multi-factor authentication (MFA) to remove the delete marker.
Resolution
Important: You can't recover data that your permanently delete and remove from a bucket.
Complete the following steps to restore the previous version of the object. In each command, replace DOC-EXAMPLE-BUCKET with the name of your bucket.
Use the Amazon S3 console to download the previous version of the object
Complete the following steps:
- Open the Amazon S3 console.
- From the list of buckets, open the bucket of the deleted object.
- Navigate to the folder of the deleted object.
- Turn on Show versions.
- In the search bar, enter the name of the deleted object.
- Select the previous version of the object. Don't select the delete marker.
- Choose Actions, and then choose Download.
Use the AWS CLI to download the previous version of the object
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
Complete the following steps:
- Run the list-object-versions command on the bucket:
Note: The preceding example command includes the --prefix option to filter the results to the specified key name prefix. This option helps reduce the number of results and saves time when your bucket contains a large number of object versions.aws s3api list-object-versions --bucket DOC-EXAMPLE-BUCKET --prefix example.txt
- From the command output, copy the version ID of the previous version of the object.
- Run the get-object command for the version ID:
aws s3api get-object --bucket DOC-EXAMPLE-BUCKET --key example.txt --version-id example.d6tjAKF1iObKbEnNQkIMPjj filename.txt
Use the Amazon S3 console to remove the delete marker
Complete the following steps:
- From the account that owns the bucket of the deleted object, open the Amazon S3 console.
- From the list of buckets, open the bucket of the deleted object.
- Navigate to the folder of the deleted object.
- Turn on Show versions.
- In the search bar, enter the name of the deleted object.
- Select the delete marker of the object.
Important: Make sure that you selected the delete marker. If you delete an object version, then you can't retrieve the object. - Choose Delete.
- In the Delete objects page, confirm that the correct delete marker is listed. Then, enter permanently delete to confirm deletion.
- Choose Delete objects.
Important: You can't use the Amazon S3 console to undelete folders. Instead, you must use the AWS CLI or the AWS SDK.
Use the AWS CLI to remove the delete marker
Remove the delete mark on several objects
Complete the following steps:
-
Run the list-object-versions command with the following --query parameter:
aws s3api list-object-versions --bucket DOC-EXAMPLE-BUCKET --prefix example.txt --query 'DeleteMarkers[?IsLatest==`true`]'
Note: The preceding example command includes the --prefix option and filters the results to the specified key name prefix.
-
From the command output, copy the version ID of the delete marker.
Important: Make sure that you copied the version ID of the delete marker. If you delete an object version, then you can't retrieve the object. -
Run the delete-object command for the version ID:
aws s3api delete-object --bucket DOC-EXAMPLE-BUCKET --key example.txt --version-id 'example.d6tjAKF1iObKbEnNQkIMPjj'
-
To verify that the delete marker was removed, run the ls command:
aws s3 ls s3://DOC-EXAMPLE-BUCKET
Remove the delete marker on thousands of objects
Complete the following steps:
- Open the AWS CloudShell console.
- Run the list-object-versions command:
Note: The preceding example command uses the jq tool from the GitHub website to parse the ListObjectVersions response for current version DeleteMarkers. By default, jq is installed on AWS CloudShell. If you don't interact with the shell environment, then your shell session ends.aws s3api list-object-versions --bucket DOC-EXAMPLE-BUCKET --prefix examplefolder/ --output json --query 'DeleteMarkers[?IsLatest==`true`].[Key, VersionId]' | jq -r '.[] | "--key '\''" + .[0] + "'\'' --version-id " + .[1]' | xargs -L1 aws s3api delete-object --bucket DOC-EXAMPLE-BUCKET
Remove the delete marker on millions of objects
A LIST call that you perform on a bucket that has millions of objects can be expensive and cause a timeout. Instead, use a custom script with the AWS SDK.
Ähnliche Videos
The final example command under "Remove the delete marker on thousands of objects" will fail if any of the file names contain an apostrophe (') char.
Thank you for your comment. We'll review and update the Knowledge Center article as needed.
Relevanter Inhalt
- AWS OFFICIALAktualisiert vor einem Jahr
- AWS OFFICIALAktualisiert vor einem Jahr
- AWS OFFICIALAktualisiert vor einem Jahr
- AWS OFFICIALAktualisiert vor 2 Jahren