S3 Versioning with Example
S3 Versioning is a feature in Amazon S3 that allows you to preserve, retrieve, and restore every version of every object stored in a bucket. This helps protect against accidental deletions and overwrites.
How S3 Versioning Works
When versioning is enabled on a bucket:
- Every time you upload (PUT) an object with the same key, S3 creates a new version of that object.
- Each version has a unique version ID.
- Deleting an object does not remove it permanently; instead, S3 adds a delete marker, making the object appear deleted, but previous versions remain accessible.
Example Scenario
Initial Upload You upload a file called report.txt with the content:
Initial Upload
You upload a file called
report.txtwith the content:Q1 Sales Data
S3 assigns a version ID, for example,
1111.Update the Object
Later, you upload a new version of
report.txtwith updated content:Q1 and Q2 Sales Data
S3 creates a new version with a different version ID, for example,
2222.Delete the Object
You delete
report.txt. S3 does not remove the file but adds a delete marker with a new version ID, for example,3333. The object now appears deleted when you list objects without specifying a version.
Retrieving Previous Versions
You can retrieve any previous version by specifying its version ID. For example, to get the original report.txt, you request the object with version ID 1111.
Example: S3 Versioning in Action (AWS CLI)
# Enable versioning on the bucket
aws s3api put-bucket-versioning --bucket my-bucket --versioning-configuration Status=Enabled
# Upload the first version
aws s3 cp report.txt s3://my-bucket/report.txt
# Upload a new version (after editing report.txt)
aws s3 cp report.txt s3://my-bucket/report.txt
# List all versions
aws s3 api list-object-versions --bucketmy-bucket --prefix report.txt
# Delete the object
aws s3 rm s3://my-bucket/report.txt
# Restore a previous version
aws s3 api get-object --bucketmy-bucket --key report.txt --version-id 1111 report_v1.txt
Benefits
- Accidental Deletion Protection: Restore deleted objects by removing the delete marker or retrieving a previous version.
- Accidental Overwrite Protection: Retrieve any previous version if a file is overwritten.
- Audit and Compliance: Maintain a complete history of changes for auditing purposes.
Considerations
- Storage Costs: All versions are stored and billed, so manage old versions with lifecycle policies if needed.
