Versioning in S3

In S3, you can benefit from versioning by storing different versions of the same object in a bucket. This ensures additional data security and recovery options.

Step 0: Creating a Bucket with Versioning (without Object Lock)

If you don’t have a bucket with versioning that you can or want to use in this example, you can use the following command to create a bucket with versioning.

aws s3api create-bucket --bucket <bucket-name> --region <region> --create-bucket-configuration LocationConstraint=<region> --endpoint-url=https://<endpoint>

Then, you need to enable versioning with the following command. This can also be used for existing buckets with objects.

aws s3api put-bucket-versioning --bucket <bucket-name> --versioning-configuration Status=Enabled --endpoint-url=https://<endpoint>

If you have already set up object lock for your bucket, versioning is already activated. The following example shows you how to upload objects, create new versions and access previous versions.

Step 1: Uploading an Object

Use the aws s3 cp command to upload an object to your bucket.

aws s3 cp <local-file-path> s3://<bucketname>/<destination-file-path> --endpoint-url=https://<endpoint>
  • <local-file-path>: The path to the file on your computer that you want to upload.
  • <bucketname>: The name of your bucket.
  • <destination-file-path>: The desired location and name of the uploaded object in the bucket.
  • <endpoint>: The corresponding endpoint for your plusserver S3.

Step 2: Overwriting the Object

Upload an updated version of the same object with the following command:

aws s3 cp <new-local-file-path> s3://<bucketname>/<destination-file-path> --endpoint-url=https://<endpoint>

The destination file path must be identical to the path in Step 1.

Step 3: Accessing Previous Versions

To access previous versions of the object, use the version ID generated by S3. Use the following command to display the version ID:

aws s3api list-object-versions --bucket <bucketname> --prefix <destination-file-path> --endpoint-url=https://<endpoint>
  • <bucketname>: The name of your bucket.
  • <destination-file-path>: The location and name of the object in the bucket.

Choose the desired version ID from the displayed list.

Example Output:

{
    "Versions": [
        {
            "ETag": "\"66b45f0d975835364c3ddba89be46516\"",
            "Size": 57,
            "StorageClass": "STANDARD",
            "Key": "testfile",
            "VersionId": "fe11c7b4-e63c-f2df-bd54-1402ec8ef4c8",
            "IsLatest": true,
            "LastModified": "2023-08-11T13:29:30.098000+00:00",
            "Owner": {
                "ID": "2bf748417f89cbbca94465e9121a2507"
            }
        },
        {
            "ETag": "\"40375ddc11cbe7f92789df9e8f73fb41\"",
            "Size": 40,
            "StorageClass": "STANDARD",
            "Key": "testfile",
            "VersionId": "fe11c7b4-eeae-f76f-a6ff-1402ec8ef430",
            "IsLatest": false,
            "LastModified": "2023-08-11T13:29:15.929000+00:00",
            "Owner": {
                "ID": "2bf748417f89cbbca94465e9121a2507"
            }
        }
    ]
}

Step 4: Downloading a Previous Version

Use the version ID to download a specific version of the object:

aws s3 cp s3://<bucketname>/<destination-file-path>?versionId=<version-id> <local-file-path> --endpoint-url=https://<endpoint>
  • <bucketname>: The name of the bucket.
  • <destination-file-path>: The location and name of the object in the bucket.
  • <version-id>: The selected version ID.
  • <local-file-path>: The path on your computer where the downloaded file should be stored.

By combining Object Lock and automatic versioning, you have the ability to access previous states of objects and protect your data from accidental changes.

Step 5: Deleting Objects with Versioning

With the plusserver S3 service, deleting objects can be a bit more complex depending on versioning settings. Here, we explain how to correctly delete objects with versioning.

Use of Variables

To delete objects in a versioned bucket, you can use the following command:

aws s3api delete-objects --bucket "<bucketname>" --delete "$(aws s3api list-object-versions --bucket "<bucketname>" --output=json | jq '{Objects: [.Versions[] | {Key: .Key, VersionId: .VersionId}], Quiet: false}')"

Please note that correct handling of version IDs is important to ensure that you delete the desired version of an object. We recommend handling version IDs carefully to avoid unexpected data behavior or data loss. The above command deletes all objects without warning, including all versions of an object within a bucket. Explanation of Variables

  • <bucketname>: The name of your bucket.
aws s3api delete-objects --bucket "test-versioning" --delete "$(aws s3api list-object-versions --bucket "test-versioning" --output=json | jq '{Objects: [.Versions[] | {Key: .Key, VersionId: .VersionId}], Quiet: false}')"
Last modified 03.05.2024: dos2unix mansvc files (d79b1ea)