Restoring deleted objects using versioning
With the VK Object Storage service, you can protect the data in a bucket from accidental deletion and unauthorized modification. To do this, versioning is used — a mechanism that preserves all versions of an object when it is overwritten or deleted. If an object is deleted by mistake, it can be restored from a previous version.
Versioning also helps protect against ransomware attacks: even if an attacker encrypts or deletes the data, the original versions will remain available in the bucket.
The following data is used as an example below:
- Bucket name —
versioning-test-bucket. - Object key —
report.txt. - Host name for connection —
https://hb.ru-msk.vkcloud-storage.ru(for the Moscow region).
-
Create an account for the VK Object Storage service, if this has not been done yet:
-
Go to the VK Cloud management console.
-
Select a project.
-
Go to the Object Storage → Аккаунты section.
-
Click the Добавить аккаунт or Добавить button.
-
Set the account name.
-
Click the Создать button.
-
In the window that opens, copy and save the access key identifier (Access Key ID) and the secret key (Secret Key).
-
-
Make sure that AWS CLI is installed and configured to work with the created VK Object Storage account.
-
Create a bucket named
versioning-test-bucket.
Enable versioning for the versioning-test-bucket bucket by running the command:
aws s3api put-bucket-versioning \--bucket versioning-test-bucket \--versioning-configuration Status=Enabled \--endpoint-url https://hb.ru-msk.vkcloud-storage.ru
After versioning is enabled, all upload, overwrite, and delete operations on objects in the bucket will preserve previous versions. Each version is assigned a unique identifier (VersionId).
-
Create a text file
report.txtand upload it to the bucket:echo "Version 1" > report.txtaws s3 cp report.txt s3://versioning-test-bucket/report.txt --endpoint-url https://hb.ru-msk.vkcloud-storage.ru -
Overwrite the object by uploading a new version of the file:
echo "Version 2" > report.txtaws s3 cp report.txt s3://versioning-test-bucket/report.txt --endpoint-url https://hb.ru-msk.vkcloud-storage.ru -
Verify that both versions of the object are saved in the bucket:
aws s3api list-object-versions \--bucket versioning-test-bucket \--prefix report.txt \--endpoint-url https://hb.ru-msk.vkcloud-storage.ru
Save the version identifier (VersionId) of the desired object version — it will be needed for restoration.
-
Delete the object from the bucket:
aws s3 rm s3://versioning-test-bucket/report.txt --endpoint-url https://hb.ru-msk.vkcloud-storage.ruWith versioning enabled, a regular object deletion does not physically remove it. Instead, a delete marker is created, which marks the object as deleted.
-
Verify that the delete marker has been created:
aws s3api list-object-versions \--bucket versioning-test-bucket \--prefix report.txt \--endpoint-url https://hb.ru-msk.vkcloud-storage.ru -
Make sure that the object is unavailable with a regular request:
aws s3 ls s3://versioning-test-bucket/report.txt --endpoint-url https://hb.ru-msk.vkcloud-storage.ru
-
To restore the object, copy the desired version from the bucket, specifying its identifier using the
--version-idparameter:aws s3 cp s3://versioning-test-bucket/report.txt restored_report.txt \--version-id <VERSION_ID> \--endpoint-url https://hb.ru-msk.vkcloud-storage.ruHere
<VERSION_ID>is the identifier of the object version obtained from the output of thelist-object-versionscommand. -
Verify the contents of the restored file:
cat restored_report.txt -
Copy the desired version back into the bucket to restore the object directly in the bucket (making the restored version the current one):
aws s3 cp s3://versioning-test-bucket/report.txt s3://versioning-test-bucket/report.txt \--version-id <VERSION_ID> \--endpoint-url https://hb.ru-msk.vkcloud-storage.ru
With prolonged use of versioning, the number of object versions grows, which increases storage costs. To automatically delete outdated objects, configure lifecycle rules for the bucket.
-
Create a
lifecycle.jsonfile with the lifecycle configuration:{"Rules": [{"ID": "Expire old objects","Status": "Enabled","Filter": {"Prefix": ""},"NoncurrentVersionExpiration": {"NoncurrentDays": 30,"NewerNoncurrentVersions": 50}}]}Here:
NoncurrentDays— the number of days a noncurrent object version is stored, after which that version is deleted.NewerNoncurrentVersions— the number of noncurrent object versions stored. When the specified limit is reached, old object versions will be deleted automatically. When used together withNoncurrentDays, deletion of a noncurrent version occurs only when both limits are reached.
-
Apply the lifecycle configuration to the bucket:
aws s3api put-bucket-lifecycle-configuration \--bucket versioning-test-bucket \--lifecycle-configuration file://lifecycle.json \--endpoint-url https://hb.ru-msk.vkcloud-storage.ru -
Verify that the configuration has been applied:
aws s3api get-bucket-lifecycle-configuration \--bucket versioning-test-bucket \--endpoint-url https://hb.ru-msk.vkcloud-storage.ru
-
Make sure that the restored object is available in the bucket:
aws s3 ls s3://versioning-test-bucket/report.txt --endpoint-url https://hb.ru-msk.vkcloud-storage.ru -
Download the object and check its contents:
aws s3 cp s3://versioning-test-bucket/report.txt downloaded_report.txt --endpoint-url https://hb.ru-msk.vkcloud-storage.rucat downloaded_report.txtThe file contents must match the restored version.
-
Make sure that the lifecycle configuration has been applied:
aws s3api get-bucket-lifecycle-configuration \--bucket versioning-test-bucket \--endpoint-url https://hb.ru-msk.vkcloud-storage.ru
The created resources are billed. If you no longer need them:
- Delete the objects from the bucket, including all object versions and Delete Markers.
- Delete the bucket.