A quick & easy way to retrieve a deleted object from a S3 bucket!

Updated: Nov 4, 2020



OOPS! By mistake, you have deleted the object from s3 in the production environment. How to retrieve it?


First, let’s understand how Amazon S3 works… 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 actual 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, Amazon S3 returns an error.




Let’s modify the below script according to your file path and run it!!!


#! /bin/bash
# List of file paths to be restored!
arr=(s3://restoring_archived_bucket_test/rollups/user_id=1210/user_performance=rollups_i/yr=2020/month=9/day=2020-09-09/hour=all_hours/part-00000-28107112-478b-47ae-89cf-c71dc873e9c9.c000.snappy.parquet s3://restoring_archived_bucket_test/rollups/user_id=1210/user_performance=rollups_i/yr=2020/month=9/day=2020-09-10/hour=all_hours/part-00000-28107112-482c-47ae-89cf-c71dc873e9c9.c000.snappy.parquet)

for i in "${arr[@]}"
do
	echo $i
	[[ $i =~ (s3://restoring_archived_bucket_test/)(.*) ]] # Modify bucket name
	prefix=${BASH_REMATCH[2]}
	version=`aws s3api list-object-versions --bucket  restoring_archived_bucket_test --prefix $prefix --query 'Versions[0].VersionId' --output text` # Modify bucket name
	[[ $i =~ (s3://restoring_archived_bucket_test/)(.*)(all_hours/)(.*) ]] # Modify bucket name and last prefix of file path
	full_path=${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}
	file_name=${BASH_REMATCH[4]}
	aws s3api get-object --bucket restoring_archived_bucket_test --key $prefix --version-id $version $file_name # Modify bucket name
	aws s3 cp $file_name $full_path
done




logo white.png

sales@squareshift.co

+65 9239 2194

© SquareShift Technologies Pte. Ltd.

  • SquareShift on LinkedIn