Storage & backups

Object Versioning

1 min read

With versioning enabled, a bucket keeps every version of an object. If a file is overwritten or deleted by mistake, you can restore a previous version.

The examples use AWS CLI configured for UzCloud. Replace my-bucket with the name of your bucket.

Enable Versioning

In the console: turn on Bucket Versioning when you create a bucket. See Manage Object Storage.

With AWS CLI:

aws s3api put-bucket-versioning --bucket my-bucket \
  --versioning-configuration Status=Enabled

# check the status
aws s3api get-bucket-versioning --bucket my-bucket

Versioning cannot be turned off completely, only suspended (Status=Suspended). Existing versions are kept.

View Versions

aws s3api list-object-versions --bucket my-bucket --prefix docs/report.pdf

Each version has a VersionId. The current version has "IsLatest": true.

Restore a Version

Download a specific version:

aws s3api get-object --bucket my-bucket --key docs/report.pdf \
  --version-id <VersionId> report-old.pdf

Or make it current again by copying it over the object:

aws s3api copy-object --bucket my-bucket --key docs/report.pdf \
  --copy-source "my-bucket/docs/report.pdf?versionId=<VersionId>"

Restore a Deleted Object

In a versioned bucket, deleting an object only adds a delete marker. To restore the object, delete the marker:

aws s3api list-object-versions --bucket my-bucket --prefix docs/report.pdf \
  --query 'DeleteMarkers[?IsLatest].VersionId'

aws s3api delete-object --bucket my-bucket --key docs/report.pdf \
  --version-id <delete-marker-VersionId>

Note

All versions take up space and are billed. To limit the number of stored versions, set up a lifecycle rule that deletes old versions.

Tip

See also: