How to Enable Spaces Versioning

Spaces Object Storage is an S3-compatible object storage service that lets you store and serve large amounts of data. Each Space is a bucket for you to store and serve files. The built-in Spaces CDN minimizes page load times and improves performance.


The S3 Versioning feature allows you to maintain and retrieve every version of each object in your buckets. For example when versioning is activated for a bucket, you can recover a previous version of an object if a user or application were to overwrite or delete it.

The Spaces API aims to be interoperable with Amazon’s AWS S3 API. For more details, see our AWS S3 compatibility table.

By default, Spaces Versioning is disabled on buckets. To enable it, following this guide.

Prerequisites

Before you enable Spaces Versioning, you need to do the following:

Generate a DigitalOcean Spaces API Key

To create a DigitalOcean Spaces API key, follow our tutorial Manage Access to Spaces.

At the end of the generation process, save the access key ID and the secret key. You will use them later to configure AWS CLI to access your account.

Find the Spaces S3-Compatible Endpoint

Next, you need to find the endpoint for your bucket. Go to the control panel, click your bucket, and click the Settings tab. Then scroll down to the Endpoint section and click the Copy button.

Screenshot of 'Endpoint' menu option

The endpoint is always your bucket’s region followed by .digitaloceanspaces.com. Make a note of the endpoint for your bucket.

Enable Spaces Versioning

You can enable Spaces Versioning using the AWS CLI.

First, configure the AWS CLI to use your Spaces Access Key using aws configure.

Then, enable versioning on your bucket using the following command, replacing your_bucket_name with your bucket name and your_endpoint with your endpoint:

aws s3api put-bucket-versioning --bucket your_bucket_name --endpoint=https://your_endpoint --versioning-configuration Status=Enabled

Check whether the command was successful by running the following command:

aws s3api get-bucket-versioning --bucket your_bucket_name --endpoint=https://your_endpoint

Which returns the following output:

{
    "Status": "Enabled",
    "MFADelete": "Disabled"
}

Retrieve a Deleted Object in a Versioning-Enabled Bucket

Spaces creates a delete marker when you delete an object from a version-enabled bucket. In this case, the delete marker becomes the current version of the object, and the actual object becomes the previous version.

First, find the deleted object’s delete marker:

aws s3api list-object-versions --prefix your_filename --bucket your_bucket_name --endpoint=https://your_endpoint

This example returns all object versions for files with your_filename in their name:

{
    "Versions": [
        {
            "ETag": "\"3a38169a722a5ac103d758ede3e13be6\"",
            "Size": 701918,
            "StorageClass": "STANDARD",
            "Key": "sammy.png",
            "VersionId": "WT3RrRINHbSmi9o4CUJwtMRMMxg0Wx7",
            "IsLatest": false,
            "LastModified": "2023-04-05T10:00:09.059000+00:00",
            "Owner": {
                "DisplayName": "12148280",
                "ID": "12148280"
            }
        }
    ],
    "DeleteMarkers": [
        {
            "Owner": {
                "DisplayName": "12148280",
                "ID": "12148280"
            },
            "Key": "sammy.png",
            "VersionId": "xod2CHTniex2vcATz0ByCLJzweygEJl",
            "IsLatest": true,
            "LastModified": "2023-04-05T10:01:32.610000+00:00"
        }
    ]
}

Then, remove the delete marker:

aws s3api delete-object --bucket your_bucket_name --endpoint=your_endpoint --key your_filename --version-id xod2CHTniex2vcATz0ByCLJzweygEJl

This command returns:

{
    "DeleteMarker": true,
    "VersionId": "xod2CHTniex2vcATz0ByCLJzweygEJl"
}

Check the VersionID to ensure that it is the VersionID of the delete marker. DO NOT use the VersionID of one of the object versions as it will cause that version of the object to be deleted.

The file is now restored.

Permanently Delete a Specific Version

Use the following command to return all object versions:

aws s3api list-object-versions --prefix your_filename --bucket your_bucket_name --endpoint=https://your_endpoint

This returns all object versions for files with your_filename in their name. In this instance, the object has three versions:

{
    "Versions": [
        {
            "ETag": "\"f0cfd2bdaa82e1a5ff0a32537c5d3b9a\"",
            "Size": 51730,
            "StorageClass": "STANDARD",
            "Key": "shark.png",
            "VersionId": "DcntGSvYg-oI..sID8OM7W524Ejofkf",
            "IsLatest": true,
            "LastModified": "2023-04-11T10:29:22.772000+00:00",
            "Owner": {
                "DisplayName": "12148280",
                "ID": "12148280"
            }
        },
        {
            "ETag": "\"a9d268788d4562567ce01136cc382ac4\"",
            "Size": 87721,
            "StorageClass": "STANDARD",
            "Key": "shark.png",
            "VersionId": "DqZI-H8.1BAnrXA7Py3jNNBRrcm4eu8",
            "IsLatest": false,
            "LastModified": "2023-04-11T10:28:58.990000+00:00",
            "Owner": {
                "DisplayName": "12148280",
                "ID": "12148280"
            }
        },
        {
            "ETag": "\"137057b79e68b856bb5f6a203f0c98eb\"",
            "Size": 190179,
            "StorageClass": "STANDARD",
            "Key": "shark.png",
            "VersionId": "j-pjg8GhKzotq1rG5XiT12Jhobj9kR7",
            "IsLatest": false,
            "LastModified": "2023-04-11T10:28:33.359000+00:00",
            "Owner": {
                "DisplayName": "12148280",
                "ID": "12148280"
            }
        }
    ]
}

Then, you can delete a specific version:

aws s3api delete-object --bucket your_bucket_name --endpoint=https://your_endpoint --key your_filename --version-id DqZI-H8.1BAnrXA7Py3jNNBRrcm4eu8

Which returns the following output:

{
    "VersionId": "DqZI-H8.1BAnrXA7Py3jNNBRrcm4eu8"
}