> For the complete documentation index, see [llms.txt](https://docs.glesys.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.glesys.com/products/storage/object-storage/how-tos/clients-and-tooling/create-immutable-buckets-with-aws-cli.md). Agents can also search and read this documentation through its [MCP server](https://docs.glesys.com/~gitbook/mcp).

# Create immutable buckets with AWS CLI

Glesys Object Storage buckets support Object Lock and versioning, also referred to as immutable buckets. To manage these features, you need to use a tool like the AWS CLI.

***

To manage more advanced features of a bucket, like Object Lock and object versioning, you need to use the AWS CLI tool. The easiest way to get started with AWS CLI is to install it in a Python virtual environment.

## Installing Python and necessary tools

{% tabs %}
{% tab title="Linux" %}
Python is pre-installed in most Linux distributions. If it's not installed on your particular distribution, you can easily install it using the distribution's package manager. For example:

* Debian/Ubuntu: `sudo apt install python3`
* Fedora/AlmaLinux: `dnf install python3`
  {% endtab %}

{% tab title="macOS" %}
Python should already be installed in newer versions of macOS.
{% endtab %}

{% tab title="Windows 11" %}

1. Open a terminal window by searching for **Terminal** in the Start menu.
2. Execute `python3` in the Terminal. If Python is not already installed, the **Windows Store** will open with Python pre-loaded.
3. Click **Get** or **Install** in the Windows Store to install Python.
4. Once the installation in the Windows Store finishes, execute `python3` again in the Terminal. This will do the actual installation and open a Python prompt.
5. When you see the prompt change to `>>>` you know that Python is properly installed and working. Exit from Python by executing the command `quit()`.
6. You will also need Microsoft Visual C++ 14 or greater to build the AWS CLI tool. Start by downloading and installing Microsoft Visual Studio Build Tools from [https://visualstudio.microsoft.com/visual-cpp-build-tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/). When the installer reaches the screen where you are asked to select tools and workloads, select **Desktop Development with C++**. Then proceed with the installation.

<figure><img src="https://3379166260-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlMZhhdQNcWOpX5Bj48GK%2Fuploads%2FwuwvKMgtA9S2BIlJvirR%2Fwindows11-build-tools-cpp.png?alt=media&amp;token=d61b1b3f-02f9-4ea6-9bae-fdebcd3c723d" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

## Installing AWS CLI

Open a terminal window. The first step is to create a directory for AWS CLI and enter that directory.

<pre data-title="Multiple commands"><code><strong>mkdir awscli
</strong><strong>cd awscli
</strong></code></pre>

Now you need to create a Python virtual environment:

<pre data-title="Command"><code><strong>python3 -m venv env
</strong></code></pre>

Activate the environment:

{% tabs %}
{% tab title="Linux and macOS" %}

<pre data-title="Command"><code><strong>source env/bin/activate
</strong></code></pre>

{% endtab %}

{% tab title="Windows 11" %}

<pre data-title="Command"><code><strong>.\env\Scripts\Activate.ps1
</strong></code></pre>

If you get an error about scripts being disabled due to the execution policy, allow your own user to execute scripts by first executing:

<pre data-title="Command"><code><strong>Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned 
</strong></code></pre>

{% endtab %}
{% endtabs %}

The prompt should now change to `(env)`. If so, everything is set up correctly so far.

Now it's time to install the AWS CLI tool in the Python virtual environment:

<pre data-title="Command"><code><strong>pip install awscli
</strong></code></pre>

Once the installation is finished, test the tool so it's working by executing `aws --version`:

<pre data-title="Command and output"><code><strong>aws --version
</strong>aws-cli/1.46.1 Python/3.14.7 Linux/7.2.3-arch1-2 botocore/1.43.62
</code></pre>

If you close the terminal, make sure you activate the Python virtual environment again when you want to use the AWS CLI tool. The virtual environment is active when you see the `(env)` prompt.

## Setting up AWS CLI with Glesys Object Storage

To set up the AWS CLI tool with your Glesys Object Storage, run `aws configure` and follow the guide, for example (change `dc-fbg1` to `dc-sto1` if your Object Storage instance is in Stockholm):

<pre data-title="Command and output. Commands are highlighted. Input lines are annotated and underlined."><code><strong>aws configure
</strong>AWS Access Key ID [None]: <a data-footnote-ref href="#user-content-fn-1">U3CVOSFFYCHYI2FJJHC4</a>
AWS Secret Access Key [None]: <a data-footnote-ref href="#user-content-fn-2">ExxxRc86KF29hxxxyyyzzzA7P3kJvWVa4zzzzzz</a>
Default region name [None]: <a data-footnote-ref href="#user-content-fn-3">dc-fbg1</a>
Default output format [None]:
</code></pre>

You also need to set the endpoint URL (change the URL to *<https://objects.dc-sto1.glesys.net>* if your Object Storage instance is in Stockholm):

<pre data-title="Command"><code><strong>aws configure set endpoint_url <a data-footnote-ref href="#user-content-fn-4">https://objects.dc-fbg1.glesys.net</a>
</strong></code></pre>

Everything is now set up and ready for use.

## Making a bucket immutable

Start by listing your current buckets.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3 ls
</strong>2026-10-16 15:05:47 cold-bread
2026-10-22 20:53:42 warm-coat
</code></pre>

This object storage instance contains two buckets: *cold-bread* and *warm-coat*. In this example, we will make *warm-coat* an immutable bucket by activating versioning and Object Lock, and setting a default retention period of 60 days on it. We also set the retention mode to *COMPLIANCE*. During the retention period (in *COMPLIANCE* mode), no one can delete or modify locked object versions—not even the bucket owner.

{% hint style="success" %}
Note that PUT and DELETE operations still succeed, but with versioning enabled, these operations do not remove underlying data. A PUT creates a new object version, and a DELETE creates a delete marker. All prior versions remain intact and retrievable throughout the retention period.
{% endhint %}

Start by enabling versioning on the bucket.

<pre data-title="Command"><code><strong>aws s3api put-bucket-versioning --bucket warm-coat --versioning-configuration Status=Enabled
</strong></code></pre>

Let's verify it's enabled.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3api get-bucket-versioning --bucket warm-coat
</strong>{
    "Status": "Enabled",
    "MFADelete": "Disabled"
}
</code></pre>

For better readability and easier editing, create a JSON file with the Object Lock rules. In this example, we name the file `lock.json` and give it the following content.

{% code title="lock.json" %}

```json
{
  "ObjectLockEnabled": "Enabled",
  "Rule": {
    "DefaultRetention": {
      "Mode": "COMPLIANCE",
      "Days": 60
    }
  }
}
```

{% endcode %}

Now it's time to activate Object Lock using the above file.

<pre data-title="Command"><code><strong>aws s3api put-object-lock-configuration --bucket warm-coat --object-lock-configuration file://lock.json
</strong></code></pre>

Let's verify it before moving on.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3api get-object-lock-configuration --bucket warm-coat
</strong>{
    "ObjectLockConfiguration": {
        "ObjectLockEnabled": "Enabled",
        "Rule": {
            "DefaultRetention": {
                "Mode": "COMPLIANCE",
                "Days": 60
            }
        }
    }
}
</code></pre>

{% hint style="warning" %}
Object Lock with *COMPLIANCE* mode is now activated for the bucket, and all files uploaded from now on will be locked for the retention period. However, existing files in the bucket are not locked automatically. These need to be locked manually. See [Locking existing files](#locking-existing-files) for more information at the end of this guide.
{% endhint %}

## Testing and using the immutable bucket

Start by uploading a new file.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3 cp forest.jpg s3://warm-coat
</strong>upload: ./forest.jpg to s3://warm-coat/forest.jpg
</code></pre>

Verify the retention of the file.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3api get-object-retention --bucket warm-coat --key forest.jpg
</strong>{
    "Retention": {
        "Mode": "COMPLIANCE",
        "RetainUntilDate": "2026-12-22T15:00:11.868603332Z"
    }
}
</code></pre>

In the output above, you can see for how long the file is locked.

If you upload a modified version of the same file, the upload will succeed. A new version of the file will be created automatically.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3 cp forest.jpg s3://warm-coat
</strong>upload: ./forest.jpg to s3://warm-coat/forest.jpg
</code></pre>

Let's list the file's versions.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3api list-object-versions --bucket warm-coat --prefix forest.jpg
</strong>{
    "Versions": [
        {
            "ETag": "\"5d7ffdcee9fa15d5bc21909e25daae70\"",
            "Size": 6247983,
            "StorageClass": "STANDARD",
            "Key": "forest.jpg",
            "VersionId": "klwrSwEx0yq8B2ZJhpc8YBDS2zi27LF",
            "IsLatest": true,
            "LastModified": "2026-10-23T15:05:35.754Z",
            "Owner": {
                "DisplayName": "os-ee5b8",
                "ID": "os-ee5b8"
            }
        },
        {
            "ETag": "\"ff1d33e60ea85f1fe01d8d3e48858db9\"",
            "Size": 3559606,
            "StorageClass": "STANDARD",
            "Key": "forest.jpg",
            "VersionId": "2iz4Ft.0ciqtOtBzf7C-l7FeGb56yDE",
            "IsLatest": false,
            "LastModified": "2026-10-23T15:00:11.868Z",
            "Owner": {
                "DisplayName": "os-ee5b8",
                "ID": "os-ee5b8"
            }
        }
    ],
    "RequestCharged": null,
    "Prefix": "forest.jpg"
}
</code></pre>

There are two versions of this file: the initial one and the modified one. Each version has a timestamp, size, id, and an `IsLatest` field, making it easy to find the correct version.

To download a different version than the latest, use its `VersionId`. For example:

<pre data-title="Command and output. Command is highlighted. (Replace \ with &#x60; in PowerShell.)"><code><strong>aws s3api get-object --bucket warm-coat --key forest.jpg \
</strong><strong>--version-id 2iz4Ft.0ciqtOtBzf7C-l7FeGb56yDE original-forest.jpg
</strong>{
    "AcceptRanges": "bytes",
    "LastModified": "Fri, 23 Oct 2026 15:00:11 GMT",
    "ContentLength": 3559606,
    "ETag": "\"ff1d33e60ea85f1fe01d8d3e48858db9\"",
    "VersionId": "2iz4Ft.0ciqtOtBzf7C-l7FeGb56yDE",
    "ContentType": "image/jpeg",
    "Metadata": {},
    "ObjectLockMode": "COMPLIANCE",
    "ObjectLockRetainUntilDate": "2026-12-22T15:00:11.868603332Z"
}
</code></pre>

The original image is now downloaded to the local computer with the name `original-forest.jpg`.

Even if the file is deleted, its versions remain intact, even if the file no longer shows up in file listings.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3 rm s3://warm-coat/forest.jpg
</strong>delete: s3://warm-coat/forest.jpg

<strong>aws s3 ls s3://warm-coat
</strong>2026-10-22 21:00:52      36584 hunt
2026-10-22 21:01:04      86008 phantasia
2026-10-22 21:01:18      22456 robots
</code></pre>

The file is missing from the file listing above, but its data is still intact. Let's list the versions for `forest.jpg` again.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3api list-object-versions --bucket warm-coat --prefix forest.jpg
</strong>{
    "Versions": [
        {
            "ETag": "\"5d7ffdcee9fa15d5bc21909e25daae70\"",
            "Size": 6247983,
            "StorageClass": "STANDARD",
            "Key": "forest.jpg",
            "VersionId": "klwrSwEx0yq8B2ZJhpc8YBDS2zi27LF",
            "IsLatest": false,
            "LastModified": "2026-10-23T15:05:35.754Z",
            "Owner": {
                "DisplayName": "os-ee5b8",
                "ID": "os-ee5b8"
            }
        },
        {
            "ETag": "\"ff1d33e60ea85f1fe01d8d3e48858db9\"",
            "Size": 3559606,
            "StorageClass": "STANDARD",
            "Key": "forest.jpg",
            "VersionId": "2iz4Ft.0ciqtOtBzf7C-l7FeGb56yDE",
            "IsLatest": false,
            "LastModified": "2026-10-23T15:00:11.868Z",
            "Owner": {
                "DisplayName": "os-ee5b8",
                "ID": "os-ee5b8"
            }
        }
    ],
    "DeleteMarkers": [
        {
            "Owner": {
                "DisplayName": "os-ee5b8",
                "ID": "os-ee5b8"
            },
            "Key": "forest.jpg",
            "VersionId": "PwwjE-wglBPCVgcdYXADDqPsBBqw1c1",
            "IsLatest": true,
            "LastModified": "2026-10-23T15:22:44.798Z"
        }
    ],
    "RequestCharged": null,
    "Prefix": "forest.jpg"
}
</code></pre>

In the output above, you can see that the file versions are intact. There is also a delete marker for the file, which is now the latest version of the file (`"IsLatest": true`).

But if you try to delete a version—any version—it will be denied. This is where the Object Lock comes into play.

<pre data-title="Commands and output. Commands are highlighted."><code><strong>aws s3api delete-object --bucket warm-coat --key forest.jpg --version-id 2iz4Ft.0ciqtOtBzf7C-l7FeGb56yDE
</strong>
An error occurred (AccessDenied) when calling the DeleteObject operation: forbidden by object lock

<strong>aws s3api delete-object --bucket warm-coat --key forest.jpg --version-id klwrSwEx0yq8B2ZJhpc8YBDS2zi27LF
</strong>
An error occurred (AccessDenied) when calling the DeleteObject operation: forbidden by object lock
</code></pre>

However, it's possible to delete the delete marker from the file, making it appear in the file listing again (since this doesn't affect the file's data, the delete marker isn't locked).

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3api delete-object --bucket warm-coat --key forest.jpg --version-id PwwjE-wglBPCVgcdYXADDqPsBBqw1c1
</strong>{
    "DeleteMarker": true,
    "VersionId": "PwwjE-wglBPCVgcdYXADDqPsBBqw1c1"
}

<strong>aws s3 ls s3://warm-coat
</strong>2026-10-23 17:05:35    6247983 forest.jpg
2026-10-22 21:00:52      36584 hunt
2026-10-22 21:01:04      86008 phantasia
2026-10-22 21:01:18      22456 robots
</code></pre>

### Locking existing files

In the output above, there are three more files besides `forest.jpg`: `hunt`, `phantasia`, and `robots`. These files existed before Object Lock was activated; hence, they lack any protection.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3api get-object-retention --bucket warm-coat --key hunt
</strong>
argument of type 'NoneType' is not a container or iterable
</code></pre>

Individual files (objects) can't be locked for a specific number of days. Instead, you must supply a specified date to the command.

<pre data-title="Command. (Replace \ with &#x60; in PowerShell.)"><code><strong>aws s3api put-object-retention --bucket warm-coat --key hunt \
</strong><strong>--retention 'Mode=COMPLIANCE,RetainUntilDate=2026-12-22T20:00:00.000Z'
</strong></code></pre>

Double-check that it worked.

<pre data-title="Command and output. Command is highlighted."><code><strong>aws s3api get-object-retention --bucket warm-coat --key hunt
</strong>{
    "Retention": {
        "Mode": "COMPLIANCE",
        "RetainUntilDate": "2026-12-22T20:00:00.000000000Z"
    }
}
</code></pre>

[^1]: The access key for your object storage instance.

[^2]: The secret key that was shown when you created your object storage instance.

[^3]: The  default region for Falkenberg is dc-fbg1. For Stockholm it's dc-sto1.

[^4]: For Stockholm, use <https://objects.dc-sto1.glesys.net> instead.
