Search code examples
amazon-web-servicesaws-cli

AWS CLI for copying an older version of a file on s3


I have a versioned bucket. I know that I can download older versions, but can I also copy with the aws command line interface (awscli) to s3 itself? Normally this would be something

aws s3 cp from-loc to-loc


Solution

  • From copy-object — AWS CLI Command Reference you can use:

    aws s3api copy-object --bucket DESTINATION_BUCKET --key DESTINATION_FILENAME --copy-source 'SOURCE_BUCKET/SOURCE_FILENAME?versionId=VERSION_ID'
    

    Note that the versionId is specified at the end of the --copy-source with: ?versionId=...

    I tried it and it worked correctly:

    $ aws s3api copy-object --bucket my_bucket --key version-output --copy-source 'my_bucket/IMG.jpg?versionId=OXxABkv6CbZ1TFY_oupeHRweEdBJa7_K'
    {
        "CopySourceVersionId": "OXxABkv6CbZ1TFY_oupeHRweEdBJa7_K",
        "VersionId": "DJOCWGV79gq01WGVv4BIPIpfBDAPtYLI",
        "ServerSideEncryption": "AES256",
        "CopyObjectResult": {
            "ETag": "\"cf0accb1b590bc16f3806b270a03132d\"",
            "LastModified": "2023-06-06T08:13:50+00:00"
        }
    }