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.
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"
}
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.hostman.com
Update the file:
echo "Version 2" > testfile.txt
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
}
]
}
There are several commands available for managing object versions.
aws s3api list-object-versions --bucket <bucket_name> --prefix <object_name> --endpoint-url https://s3.hostman.com
aws s3api list-object-versions --bucket <bucket_name> --query 'Versions[*].[Key, VersionId, Size]' --endpoint-url https://s3.hostman.com
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.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.
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.
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.hostman.com
Verify that the policy has been applied:
aws s3api get-bucket-lifecycle-configuration --bucket <bucket_name> --endpoint-url https://s3.hostman.com