Object Versioning
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 Copy link
Versioning can be enabled through the control panel or using clients such as AWS CLI, S3 Browser, and others.
Using the Control Panel Copy link
- Go to the S3 Storage section and click on the bucket.
-
Go to the Settings tab and click Change next to Version history is disabled.

-
Confirm enabling versioning by clicking Enable versioning.
Note: Once enabled, versioning cannot be fully disabled for the bucket later; it can only be suspended.
Using AWS CLI Copy link
To enable versioning on an S3 bucket using AWS CLI, run the following command:
aws s3api put-bucket-versioning --bucket <bucket_name> --versioning-configuration Status=Enabled --endpoint-url https://s3.hmstorage.netTo verify that versioning is enabled, use:
aws s3api get-bucket-versioning --bucket <bucket_name> --endpoint-url https://s3.hmstorage.netExpected output:
{
"Status": "Enabled",
"MFADelete": "Disabled"
}
Testing Versioning Copy link
Let's demonstrate how versioning works with an example.
-
Create a test file:
echo "Version 1" > testfile.txt-
Upload it to the bucket:
aws s3 cp testfile.txt s3://<bucket_name>/ --endpoint-url https://s3.hmstorage.net-
Update the file:
echo "Version 2" > testfile.txt-
Upload it again:
aws s3 cp testfile.txt s3://<bucket_name>/ --endpoint-url https://s3.hmstorage.netNow the file has multiple versions. To check them, use:
aws s3api list-object-versions --bucket <bucket_name> --prefix testfile.txt --endpoint-url https://s3.hmstorage.netExample 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.
Working with Versions in the Control Panel Copy link
To view a list of object versions in the control panel:
- Go to the S3 Storage section and click on the bucket.
- Open the Objects tab.
- Click the three dots next to the object and select Version history.
- Hover over a version to see available actions:
- File link generates a presigned URL for this object version.
- Downloading allows to download the selected version.
- Restoring object version makes the selected version the current version.
- Deleting object version (by clicking on the three dots) allows to delete the chosen version.
Managing Object Versions Copy link
There are several commands available for managing object versions.
List All Versions of a Specific Object Copy link
aws s3api list-object-versions --bucket <bucket_name> --prefix <object_name> --endpoint-url https://s3.hmstorage.netList All Objects and Versions Copy link
aws s3api list-object-versions --bucket <bucket_name> --query 'Versions[*].[Key, VersionId, Size]' --endpoint-url https://s3.hmstorage.netDownloading a Specific Object Version Copy link
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.hmstorage.net
Where:
--key– The object name in the bucket.--version-id– The version ID (retrieved usinglist-object-versions).testlocalfile.txt– The name of the file where the downloaded version will be saved.
Restoring a Specific Object Version Copy link
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.hmstorage.net
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 Copy link
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.hmstorage.netWhere --version-id is the ID of the object version to be deleted.
Configuring Automatic Deletion of Old Versions Copy link
To prevent old file versions from accumulating, you can set up an object lifecycle policy.
-
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.
-
Apply the lifecycle policy:
aws s3api put-bucket-lifecycle-configuration --bucket <bucket_name> --lifecycle-configuration file://lifecycle.json --endpoint-url https://s3.hmstorage.net-
Verify that the policy has been applied:
aws s3api get-bucket-lifecycle-configuration --bucket <bucket_name> --endpoint-url https://s3.hmstorage.net