Restore Deleted Objects in Versioned Amazon S3 Buckets

Recover deleted objects in versioned Amazon S3 buckets. Use delete markers to restore previous versions. Reduce downtime. Prevent data loss. Follow the steps below.

You deleted an object from a production Amazon S3 bucket. The bucket enables versioning. This section explains how to retrieve the object.

Amazon S3 uses delete markers in versioned buckets. When you delete an object, S3 creates a delete marker. The marker becomes the current version. The original object becomes a previous version. S3 treats the object as deleted. GET requests return an error.

Update the script path to match your file location. Run the script.

CODE
01#! /bin/bash
02# List of file paths to be restored!
03arr=(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)
04
05for i in "${arr[@]}"
06do
07 echo $i
08 [[ $i =~ (s3://restoring_archived_bucket_test/)(.*) ]] # Modify bucket name
09 prefix=${BASH_REMATCH[2]}
10 version=`aws s3api list-object-versions --bucket restoring_archived_bucket_test --prefix $prefix --query 'Versions[0].VersionId' --output text` # Modify bucket name
11 [[ $i =~ (s3://restoring_archived_bucket_test/)(.*)(all_hours/)(.*) ]] # Modify bucket name and last prefix of file path
12 full_path=${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}
13 file_name=${BASH_REMATCH[4]}
14 aws s3api get-object --bucket restoring_archived_bucket_test --key $prefix --version-id $version $file_name # Modify bucket name
15 aws s3 cp $file_name $full_path
16done

See related articles. Talk to our team.

Contact Us