Skip to content

How to recover a deleted AWS S3 versioning object

Estimated time to read: 3 minutes

In a previous tutorial, you have learned how to (hard and soft) delete objects using AWS S3 versioning. In this last tutorial of the versioning series, you are going to learn how to recover a soft deleted object.

Prerequisites

Find the soft-deleted file

In the previous tutorials, you've created files and deleted them. Currently, there is one soft deleted file that can be recovered.

With the command list-object-versions, it is possible to see all versioned objects.

$ aws --profile=ams2 --endpoint=https://core.fuga.cloud:8080 s3api list-object-versions --bucket versioned
{
    "Versions": [
        {
            "ETag": "\"4c3ef60b8641408bc4bf27af716d9c74\"",
            "Size": 8,
            "StorageClass": "STANDARD",
            "Key": "test.txt",
            "VersionId": "qohtqbnNx9ONbxv4LWan.Qdaz7HUcjB",
            "IsLatest": false,
            "LastModified": "2021-12-06T09:38:35.301Z",
            "Owner": {
                "DisplayName": "owner name",
                "ID": "owner id"
            }
        },
        {
            "ETag": "\"2f548f61bd37f628077e552ae1537be2\"",
            "Size": 7,
            "StorageClass": "STANDARD",
            "Key": "test.txt",
            "VersionId": "swwQLkp35P87WzjHQmoYkO1KDukcvq2",
            "IsLatest": false,
            "LastModified": "2021-12-06T09:38:22.115Z",
            "Owner": {
                "DisplayName": "owner name",
                "ID": "owner id"
            }
        }
    ],
    "DeleteMarkers": [
        {
            "Owner": {
                "DisplayName": "owner name",
                "ID": "owner id"
            },
            "Key": "test.txt",
            "VersionId": "MgHyRwoAJIXMHmWM3IDIP1zC7Ax.iO6",
            "IsLatest": true,
            "LastModified": "2021-12-06T09:58:28.596Z"
        }
    ]
}

In the above output, you see the last two soft-deleted objects. Both share the same Key, and therefore one is an update of the other. Another thing to notice is the delete marker. As explained in the previous tutorial, the delete marker makes the object soft deleted.

Retrieve a soft deleted object

You can retrieve each soft-delete file by adding the VersionId to the get-object command.

$ aws --profile=fuga --endpoint=https://core.fuga.cloud:8080 s3api get-object --bucket versioned --key test.txt --version-id <latest LastModified> test3.txt
$ cat test3.txt
monkey

With the command used above, it is possible to retrieve an object from the Object Store. When checking the object store (list-object-versions), it is possible to see that the object is not back, and the delete marker is still there.

Undo the soft deletion

As mentioned before, the delete marker makes the object look like it has been deleted. When you delete the delete marker, the object is back in the Object Store. You achieve this by placing the VersionId of the delete flag on the delete-object command.

To truly restore a soft deleted object, you must delete the deletion marker.

$ aws --profile=ams2 --endpoint=https://core.fuga.cloud:8080 s3api delete-object --bucket versioned --key test.txt --version-id <delete marker VersionId>

You now deleted the delete marker and restored the versioned object, deleted earlier. Let's check that with the list-objects-v2 command. (Do you remember that there was nothing in the output when we started?).

$ aws --profile=ams2 --endpoint=https://core.fuga.cloud:8080 s3api list-objects-v2 --bucket versioned
{
    "Contents": [
        {
            "Key": "test.txt",
            "LastModified": "2021-12-06T09:38:35.301Z",
            "ETag": "\"4c3ef60b8641408bc4bf27af716d9c74\"",
            "Size": 8,
            "StorageClass": "STANDARD"
        }
    ]
}

In the example output above, you see one object returned to the bucket (one live object).

$ aws --profile=ams2 --endpoint=https://core.fuga.cloud:8080 s3api list-object-versions --bucket versioned
{
"Versions": [
     {
         "ETag": "\"f4aa19fc7ee46cce02bf1fb6fad054cf\"",
         "Size": 5,
         "StorageClass": "STANDARD",
         "Key": "test.txt",
         "VersionId": "EGlSkj1dR.OrtmmmZ0R4DsI5aVPHNcO",
         "IsLatest": true,
         "LastModified": "2021-11-30T09:19:02.669Z",
         "Owner": {
             "DisplayName": "owner name",
             "ID": "owner ID"
         }
     },
     {
         "ETag": "\"dbb126075a1d0b4ca64c3e2ae0159bc7\"",
         "Size": 4,
         "StorageClass": "STANDARD",
         "Key": "test.txt",
         "VersionId": "swwQLkp35P87WzjHQmoYkO1KDukcvq2",
         "IsLatest": false,
         "LastModified": "2021-11-30T09:18:48.423Z",
         "Owner": {
             "DisplayName": "owner name",
             "ID": "owner ID"
         }
     }
]
}

As seen in the output above, there are still two object files left while the deletion marker is gone. The latest LastModified object is now set again to IsLatest with True. It means that this object (VersionId: EGlSkj1dR.OrtmmmZ0R4DsI5aVPHNcO) is now the current object, as shown in the list-objects-v2 command.

Conclusion

In this tutorial, you learned to recover a soft-deleted object from a Fuga S3 bucket.