Use Boto3 to Recover Deleted Files in AWS S3 Bucket

So it happened. One of our techs ‘accidentally’ deleted all the directories and files in one of our S3 buckets. I enabled S3 Bucket Versioning on all our important buckets. So the deleted files are still there with the ‘latest version’ of the file being a Delete Marker.

If you do not already know, a bucket with versioning doesn’t actually delete files. It places a Delete Marker on a file instead. A bucket’s life cycle policy may or may not eventually remove previous versions of a file in a set period of time (defined by the admin).  It’s important to recover a file before any policy automatically purges old versions.  So if a file is deleted on a versioned bucket you can quickly recover it by listing all versions of objects in the AWS Web GUI and removing the Delete Marker. And bang, your file is back.

However, in this case all the objects were deleted. I am talking 50,000 items in this bucket. There is no way I was going to use the GUI to recover that many items. So here’s how I did it via script with Boto3.

# ######################################
#
# Empty Bucket of all delete markers from all objects.
#
# ######################################

# -----------------------------------
# Enter these values here:
thebucket = 'bucketname'
access_key = 'yourkey'
secret_key = 'yoursecretkey'

# ------------------------------------
# Don't change anything under here.

ec2 = boto3.resource('ec2')
ec2client = boto3.client('ec2')
s3 = boto3.resource('s3',region_name='us-east-1',
                            aws_access_key_id=access_key,
                            aws_secret_access_key=secret_key)
s3client = boto3.client('s3',region_name='us-east-1',
                            aws_access_key_id=access_key,
                            aws_secret_access_key=secret_key)

# paginate 1000 at a time
paginator = s3client.get_paginator('list_object_versions')
pageresponse = paginator.paginate(Bucket=thebucket)

# iter over the pages from the paginator
for pageobject in pageresponse:
    # Find if there are any delmarkers
    if 'DeleteMarkers' in pageobject.keys():
        for each_delmarker in pageobject['DeleteMarkers']:
            # Create a resource for the version-object 
            # and use .delete() to remove it.
            fileobjver = s3.ObjectVersion(
                thebucket,
                each_delmarker['Key'],
                each_delmarker['VersionId']
            )
            # I added this output just so I could watch the script run.
            print('Restoring ' + each_delmarker['Key'])
            # Lastly, lets remove the del marker and recover one of many files.
            fileobjver.delete()
Tagged , , , , , . Bookmark the permalink.

5 Responses to Use Boto3 to Recover Deleted Files in AWS S3 Bucket

  1. Jeroen says:

    This just saved me big time. Thanks sooo much!

  2. Hareesh says:

    I have version disabled bucket. Unfortunately one file was deleted. Is there any way to recover files?

    Thanks

  3. tata says:

    You saved my life,,,,,, THANK YOU for sharing this

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
26 + 3 =