Search code examples
amazon-s3data-protections3cmdcloudberry

Commandline tools to restore a versioned deleted file in S3?


I have versioning enabled in my S3 bucket, how do I restore a deleted file with a command line S3 client, such as s3cmd? How do I browse the different versions of the files? So far, I have regressed to Freeware Cloudberry Windows Client to achieve this. I know I could use also Boto Python library, but I would prefer a common command line tool.


Solution

  • Use this AWS CLI command: (docs here):

    aws s3api list-object-versions --bucket mybucket
    

    This returns the list of objects, eg.:

    {
        "DeleteMarkers": [
            ...
        ],
        "Versions": [
            {
                "LastModified": "2015-11-17T12:25:01.000Z",
                "VersionId": "Mj1.PcWG8e.C._7LhvFU131pXJ98abIl",
                "ETag": "\"b76b2b775023e60be16bc332496f8409\"",
                "StorageClass": "STANDARD",
                "Key": "file1.txt",
                "Owner": {
                    "DisplayName": "aws03005",
                    "ID": "b09   0e41acbdf98bc34ef4781f10e4aeebb7a495b8be78da0db884af2980ed38d"
                },
                "IsLatest": true,
                "Size": 30318
            },
        ...
    }
    

    Use Key (file name) and VersionId to download a previous version:

    aws s3api get-object --bucket mybucket --key file1.txt --version-id Mj1.PcWG8e.C._7LhvFU131pXJ98abIl foo.txt
    

    If you are trying to get a deleted file, please note that you have to get-object the latest version listed in Versions, not the one listed in DeleteMarkers.

    To do this in a script, --query and/or jq might help.