0
0
AWScloud~5 mins

S3 versioning in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes files in storage get overwritten or deleted by mistake. S3 versioning helps keep all versions of files so you can recover old ones if needed.
When you want to protect important files from accidental deletion or overwriting.
When you need to keep a history of changes to files stored in S3.
When multiple people or systems update files and you want to track each change.
When you want to recover a previous version of a file after a mistake.
When you want to audit changes to files over time.
Commands
This command creates a new S3 bucket named 'example-versioned-bucket' in the us-east-1 region where we will enable versioning.
Terminal
aws s3api create-bucket --bucket example-versioned-bucket --region us-east-1
Expected OutputExpected
No output (command runs silently)
--bucket - Specifies the name of the bucket to create.
--region - Specifies the AWS region where the bucket will be created.
This command turns on versioning for the bucket so that all changes to files are saved as different versions.
Terminal
aws s3api put-bucket-versioning --bucket example-versioned-bucket --versioning-configuration Status=Enabled
Expected OutputExpected
No output (command runs silently)
--bucket - Specifies which bucket to enable versioning on.
--versioning-configuration - Sets the versioning status to Enabled.
This command checks the versioning status of the bucket to confirm it is enabled.
Terminal
aws s3api get-bucket-versioning --bucket example-versioned-bucket
Expected OutputExpected
{"Status": "Enabled"}
--bucket - Specifies which bucket to check.
This command uploads a file named file.txt to the bucket. Each upload creates a new version if versioning is enabled.
Terminal
aws s3 cp file.txt s3://example-versioned-bucket/file.txt
Expected OutputExpected
upload: ./file.txt to s3://example-versioned-bucket/file.txt
cp - Copies a file to the S3 bucket.
This command lists all versions of the file.txt in the bucket so you can see the different saved versions.
Terminal
aws s3api list-object-versions --bucket example-versioned-bucket --prefix file.txt
Expected OutputExpected
{"Versions": [{"Key": "file.txt", "VersionId": "111111111111111111111", "IsLatest": true, "LastModified": "2024-06-01T12:00:00.000Z"}]}
--bucket - Specifies the bucket to list versions from.
--prefix - Filters the list to only show versions of file.txt.
Key Concept

If you remember nothing else from this pattern, remember: enabling versioning on an S3 bucket saves every change to files so you can recover old versions anytime.

Common Mistakes
Trying to enable versioning on a bucket that does not exist.
The command will fail because versioning can only be enabled on existing buckets.
First create the bucket, then enable versioning.
Assuming versioning is enabled by default on new buckets.
Versioning is off by default, so changes will overwrite files without saving old versions.
Always explicitly enable versioning after creating the bucket.
Uploading files without checking versioning status.
If versioning is not enabled, uploads overwrite files without keeping history.
Check versioning status with get-bucket-versioning before uploading important files.
Summary
Create an S3 bucket to store your files.
Enable versioning on the bucket to save all changes as versions.
Upload files to the bucket; each upload creates a new version.
List object versions to see all saved versions of a file.