Sign In
Sign In

Object Versioning

Updated on 24 March 2025

Versioning in S3 allows you to retain all changes made to objects uploaded to a bucket. It helps protect data from accidental deletion or overwrites and allows you to restore previous versions of objects when needed.

Versioning can be enabled using various clients, such as AWS CLI, S3 Browser, and others. This article covers working with versions using AWS CLI.

Enabling Versioning

To enable versioning in an S3 bucket, run the following command:

aws s3api put-bucket-versioning --bucket <bucket_name> --versioning-configuration Status=Enabled --endpoint-url https://s3.hostman.com

To verify that versioning is enabled, use:

aws s3api get-bucket-versioning --bucket <bucket_name> --endpoint-url https://s3.hostman.com

Expected output:

{
    "Status": "Enabled",
    "MFADelete": "Disabled"
}

Testing Versioning

Let's demonstrate how versioning works with an example.

  1. Create a test file:

echo "Version 1" > testfile.txt
  1. Upload it to the bucket:

aws s3 cp testfile.txt s3://<bucket_name>/ --endpoint-url https://s3.hostman.com
  1. Update the file:

echo "Version 2" > testfile.txt
  1. Upload it again:

aws s3 cp testfile.txt s3://<bucket_name>/ --endpoint-url https://s3.hostman.com

Now the file has multiple versions. To check them, use:

aws s3api list-object-versions --bucket <bucket_name> --prefix testfile.txt --endpoint-url https://s3.hostman.com

Example output:

{
    "Versions": [
        {
            "Key": "testfile.txt",
            "VersionId": "S7hpDH6F0RYfubNoTjin6hpBu5ewDj.",
            "IsLatest": true
        },
        {
            "Key": "testfile.txt",
            "VersionId": "pdFehsuCbaPz2hNh4b9DBndfEwzjTfH",
            "IsLatest": false
        }
    ]
}

Important Notes

  • Each object version takes up storage space in the bucket.
  • Object versions are not displayed in the control panel or other S3 clients.
  • Even if an object is deleted, all its versions remain in the bucket.

Managing Object Versions

There are several commands available for managing object versions.

List All Versions of a Specific Object

aws s3api list-object-versions --bucket <bucket_name> --prefix <object_name> --endpoint-url https://s3.hostman.com

List All Objects and Versions

aws s3api list-object-versions --bucket <bucket_name> --query 'Versions[*].[Key, VersionId, Size]' --endpoint-url https://s3.hostman.com

Downloading a Specific Object Version

To download a specific version of an object, use:

aws s3api get-object --bucket <bucket_name> \
                     --key testfile.txt \
                     --version-id pdFehsuCbaPz2hNh4b9DBndfEwzjTfH \
                     testlocalfile.txt \
                     --endpoint-url https://s3.hostman.com

Where:

  • --key – The object name in the bucket.
  • --version-id – The version ID (retrieved using list-object-versions).
  • testlocalfile.txt – The name of the file where the downloaded version will be saved.

Restoring a Specific Object Version

If an object was updated or deleted, you can restore a previous version by copying it over the current one:

aws s3api copy-object --bucket <bucket_name> \
                      --key testfile.txt \
                      --copy-source <bucket_name>/<object_name>?versionId=<version_id> \
                      --endpoint-url https://s3.hostman.com

Where:

  • <bucket_name>/<object_name> – Full path to the object in the bucket.
  • ?versionId=<version_id> – The version to be restored.

This command will create a new version in the bucket identical to the restored one.

Deleting a Specific Object Version

To delete a specific version of an object, use:

aws s3api delete-object --bucket <bucket_name> --key <object_name> --version-id S7hpDH6F0RYfubNoTjin6hpBu5ewDj. --endpoint-url https://s3.hostman.com

Where --version-id is the ID of the object version to be deleted.

Configuring Automatic Deletion of Old Versions

To prevent old file versions from accumulating, you can set up an object lifecycle policy.

  1. Create a file called lifecycle.json:

{
    "Rules": [
        {
            "ID": "DeleteOldVersions",
            "Status": "Enabled",
            "Filter": {},
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 30
            }
        }
    ]
}

This rule deletes object versions older than 30 days while keeping the current versions intact.

  1. Apply the lifecycle policy:

aws s3api put-bucket-lifecycle-configuration --bucket <bucket_name> --lifecycle-configuration file://lifecycle.json --endpoint-url https://s3.hostman.com
  1. Verify that the policy has been applied:

aws s3api get-bucket-lifecycle-configuration --bucket <bucket_name> --endpoint-url https://s3.hostman.com
Was this page helpful?
Updated on 24 March 2025

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support