AWS CLI: S3 Versioning – List|Get Objects

Amazon S3 has a built-in versioning solution (can be enabled in the bucket’s properties tab), that helps to track all the changes that me make to the files hosted in an S3 bucket.

In this note i will show how to list all the versions of an object (file) stored in an S3 bucket and how to download the specific version of an object.

Cool Tip: List Amazon S3 Buckets & Objects using AWS CLI! Read more →

S3 Versioning

List all the versions of an S3 object:

$ aws s3api list-object-versions --bucket <bucket_name> --prefix <object_name>
- example -
$ aws s3api list-object-versions --bucket myBucket --prefix myDir/myFile.txt

A sample output:

{
  "Name": "myBucket",
  "Versions": [
    {
      "LastModified": "2030-09-29T00:21:12.654Z",
      "VersionId": "1602842863922",
      "ETag": "\"7462528fj587c0df5dj3651a23b80be5\"",
      "StorageClass": "STANDARD",
      "Key": "myFile.txt",
      "Owner": {
          "DisplayName": "myUsername",
          "ID": "myID"
      },
      "IsLatest": true,
      "Size": 819627
    },
    ...
    "Prefix": "",
    "KeyMarker": "myDir/myFile.txt"
    ...

Get the latest version number of an S3 object:

$ aws s3api list-object-versions --bucket <bucket_name> --prefix <object_name> --query 'Versions[?IsLatest].[VersionId]'
- example -
$ aws s3api list-object-versions --bucket myBucket --prefix myDir/myFile.txt --query 'Versions[?IsLatest].[VersionId]'

Download the specific version of an S3 object:

$ aws s3api get-object --bucket <bucket_name> --key <object_name> <save_as> --version-id <version>
- example -
$ aws s3api get-object --bucket myBucket --key myDir/myFile.txt myFile.txt --version-id 1602842863922