CloudAcademia
EN

Cloud Computing · Module 08 · Cloud Storage: Block, File and Object

Lab: Block volumes and an object store

Lab75 minBeginnerMarkdown

Showing raw Markdown: the renderer could not load.

## Goal
Compare what a volume shared by two containers gives you with what a real block device adds, then run a local S3-compatible object store and use it the way you would use a cloud bucket: upload and list objects, enable versioning and recover a deleted object, write a lifecycle rule, share an object with a pre-signed URL and open a prefix to the public with a bucket policy.

## Objectives
- Compare a single-user volume with a volume shared by two containers, and explain what a real block device adds (objective 1).
- Create buckets and upload, list, download and delete objects through the S3 API, and see that prefixes are not folders (objective 3).
- Enable versioning, see a delete marker and restore an earlier version (objective 3).
- Add and read back a lifecycle rule (objectives 2 and 3).
- Share an object with a pre-signed URL and open one prefix with a bucket policy, and explain why public buckets are risky (objective 4).

## Prerequisites
- Linux with Podman 4 or later, rootless, no `sudo` (tested with Podman 5.8). Docker works if you replace `podman` with `docker`.
- `curl`. For the optional Step 4 only: `mkfs.ext4` and `debugfs` from the `e2fsprogs` package (installed by default on most distributions).
- Internet access to pull three images the first time: `docker.io/library/alpine:3.20`, `docker.io/motoserver/moto:5.2.4` and `docker.io/amazon/aws-cli:2.38.0` (a few hundred MB in total).
- Free TCP port 9101 on `127.0.0.1`.
- [The lesson](lesson.md) of this module.

> [!WARNING]
> Everything runs on your computer: the object store listens on `127.0.0.1` only, and the keys `test`/`test` are dummies that work only against it. No account, card or real cloud resource is involved, so there is nothing to bill. All containers and volumes start with `lab-storage-` and the working files live in `~/storage-lab`; Cleanup removes them. Never put real data or real credentials into this lab.

> [!NOTE]
> The outputs were captured in November 2026 with Podman 5.8.7. Dates, version IDs, ETags and signatures differ on your machine; compare the structure, not the exact values. Part B uses **Moto**, an open-source emulator of the Amazon Web Services (AWS) APIs. It is a teaching tool: it runs without authentication checks, keeps its data in memory and doesn't implement every behaviour of a real object store. The Notes in the steps say where it differs.

## Architecture
<figure class="diagram" style="--dg-w: 1100px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1100 440" width="1100" height="440" role="img" aria-labelledby="storage-lab-architecture-title storage-lab-architecture-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="storage-lab-architecture-title">Lab architecture: Podman volumes and a local S3-compatible server</title>
<desc id="storage-lab-architecture-desc">Your Linux computer runs rootless Podman. Part A: a writer container, lab-storage-writer, and a reader container, lab-storage-reader, both mount the named volume lab-storage-data at /data; the volume is a directory on the host, not a block device. Part B: an AWS CLI version 2 container, docker.io/amazon/aws-cli:2.38.0, run through a shell function with the folder ~/storage-lab mounted as /work, sends S3 API requests over HTTP to 127.0.0.1 port 9101. That port is mapped to port 5000 in the Moto container lab-storage-moto, an S3 emulator that accepts the dummy keys test and test and does not enforce authentication, holding the buckets lab-docs, lab-versioned and lab-pub.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1099" height="439" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l2"><rect class="dg-tint" x="20" y="20" width="1060" height="400" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(36 33) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g>
<text x="62" y="47" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Your computer (Linux, rootless Podman)</text></g>
<g class="dg-l7"><rect class="dg-tint" x="40" y="60" width="480" height="340" rx="16" fill="#f6f2fd" stroke="#c5a9f0" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(56 73) scale(0.7500)" fill="none" stroke="#6d28d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z"/><path d="m3.3 7 8.7 5 8.7-5"/><path d="M12 22V12"/></g>
<text x="82" y="87" font-size="13" class="dg-ink" fill="#6d28d9" font-weight="700">Part A: volumes</text></g>
<g class="dg-l4"><rect class="dg-tint" x="540" y="60" width="520" height="340" rx="16" fill="#fbf4f0" stroke="#e7b39e" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(556 73) scale(0.7500)" fill="none" stroke="#c2410c" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"/></g>
<text x="582" y="87" font-size="13" class="dg-ink" fill="#c2410c" font-weight="700">Part B: object storage</text></g>
<rect class="dg-card" x="60" y="104" width="210" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(76 120) scale(1.2500)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z"/><path d="m3.3 7 8.7 5 8.7-5"/><path d="M12 22V12"/></g>
<text x="118" y="141" font-size="15" class="dg-text" fill="#17171c" font-weight="700">Container: writer</text>
<text x="78" y="172" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">lab-storage-writer</text>
<text x="78" y="192" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">appends to shared.log</text>
<rect class="dg-card" x="290" y="104" width="210" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(306 120) scale(1.2500)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z"/><path d="m3.3 7 8.7 5 8.7-5"/><path d="M12 22V12"/></g>
<text x="348" y="141" font-size="15" class="dg-text" fill="#17171c" font-weight="700">Container: reader</text>
<text x="308" y="172" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">lab-storage-reader</text>
<text x="308" y="192" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">reads the same files</text>
<rect class="dg-card" x="60" y="270" width="440" height="110" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(76 286) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<g class="dg-l7"><text x="118" y="307" font-size="15" class="dg-ink" fill="#6d28d9" font-weight="700">Named volume: lab-storage-data</text></g>
<text x="78" y="338" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">mounted at /data in both containers</text>
<text x="78" y="358" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">a directory on the host, not a block device</text>
<g class="dg-l7"><path class="dg-stroke" d="M165 202V262" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M159 257l6 8 6-8" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M395 202V262" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><path class="dg-stroke" d="M389 257l6 8 6-8" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l7"><rect class="dg-bg-plain" x="183.312" y="220" width="193.375" height="24" rx="12" fill="#ffffff"/><text x="280" y="236.5" font-size="11.5" class="dg-ink" fill="#6d28d9" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">-v lab-storage-data:/data</text></g>
<rect class="dg-card" x="560" y="104" width="480" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(576 120) scale(1.2500)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g>
<text x="618" y="141" font-size="15" class="dg-text" fill="#17171c" font-weight="700">AWS CLI v2 container</text>
<text x="578" y="172" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">docker.io/amazon/aws-cli:2.38.0</text>
<text x="578" y="192" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">aws() shell function · ~/storage-lab → /work</text>
<rect class="dg-card" x="560" y="262" width="480" height="118" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(576 278) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"/></g></g>
<g class="dg-l4"><text x="618" y="299" font-size="15" class="dg-ink" fill="#c2410c" font-weight="700">Moto container: lab-storage-moto</text></g>
<text x="578" y="330" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">port 9101 → 5000 · keys test/test, no auth enforced</text>
<text x="578" y="350" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">buckets: lab-docs · lab-versioned · lab-pub</text>
<text x="578" y="370" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">an emulator of the S3 API, not a durable store</text>
<g class="dg-l4"><path class="dg-stroke" d="M800 202V254" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M794 249l6 8 6-8" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><rect class="dg-bg-plain" x="684.74" y="218" width="230.52" height="24" rx="12" fill="#ffffff"/><text x="800" y="234.5" font-size="11.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">S3 API over HTTP · 127.0.0.1:9101</text></g>
</svg></figure>

*Figure 1. Part A uses a Podman volume from two containers; Part B sends S3 API requests from the AWS CLI container to an S3-compatible server on `127.0.0.1:9101`.*

## Steps

### Part A: block storage and file-like sharing

### Step 1 — Create a working folder and a volume
You keep every lab file in one folder, so cleanup is easy. A Podman **named volume** is storage that outlives a container. Create one and give it a recognisable name.

```bash
mkdir -p ~/storage-lab && cd ~/storage-lab
podman --version
podman volume create lab-storage-data
```

Expected output:
```text
podman version 5.8.7
lab-storage-data
```

> [!WARNING]
> Run every following command from `~/storage-lab`. In Part B, the `aws` function mounts the current folder into a container with `:Z`, which relabels it for SELinux. Run from your home folder, it would relabel all of it.

### Step 2 — Use the volume from one container
You mount the volume at `/data` in a short-lived container, write a file, and then read it from a second container to see that the data outlives the first.

```bash
podman run --rm -v lab-storage-data:/data docker.io/library/alpine:3.20 \
  sh -c 'echo "written by container one" > /data/note.txt; ls -l /data'
podman run --rm -v lab-storage-data:/data docker.io/library/alpine:3.20 cat /data/note.txt
podman volume inspect lab-storage-data --format '{{.Mountpoint}}'
```

Expected output:
```text
total 4
-rw-r--r--    1 root     root            25 Nov  7 19:02 note.txt
written by container one
/home/<user>/.local/share/containers/storage/volumes/lab-storage-data/_data
```

The last line is the point: a Podman volume is **just a directory on the host's own filesystem** (`<user>` is your user name). It is not a block device: it has no fixed size, no device name, and no filesystem of its own. It is a convenient stand-in for the idea of "storage that outlives the compute".

### Step 3 — Share the volume between two containers at the same time
A real file share (NFS or SMB) lets many machines use the same files at once. Here the two "machines" are containers on the same host. Start a writer in the background, then read from a second container while it runs.

```bash
podman run -d --name lab-storage-writer -v lab-storage-data:/data docker.io/library/alpine:3.20 \
  sh -c 'i=0; while true; do i=$((i+1)); echo "writer line $i" >> /data/shared.log; sleep 1; done'
sleep 3
podman run --rm --name lab-storage-reader -v lab-storage-data:/data docker.io/library/alpine:3.20 \
  sh -c 'cat /data/shared.log; echo "reader was here" > /data/reader.txt; ls /data'
```

Expected output (the number of lines depends on timing):
```text
writer line 1
writer line 2
writer line 3
writer line 4
note.txt
reader.txt
shared.log
```

The reader sees the writer's file while the writer is still running, and its own file is visible to the writer. Now stop the writer and see what happens when two containers append to the same file at once:

```bash
podman rm -f -t 0 lab-storage-writer
podman run -d --name lab-storage-appender -v lab-storage-data:/data docker.io/library/alpine:3.20 \
  sh -c 'for i in 1 2 3 4 5; do echo "A$i" >> /data/both.log; sleep 1; done'
podman run --rm -v lab-storage-data:/data docker.io/library/alpine:3.20 \
  sh -c 'for i in 1 2 3 4 5; do echo "B$i" >> /data/both.log; sleep 1; done; cat /data/both.log'
podman rm -f -t 0 lab-storage-appender
```

Expected output (the A and B lines interleave; the exact order can differ):
```text
A1
B1
A2
B2
A3
B3
A4
B4
A5
B5
```

Both containers wrote to one file without corrupting it. That works because the **filesystem** (the host's) sits below both containers and coordinates them: the same job that NFS or SMB servers do for a network file share. A file share gives you this across many machines.

Now think about what a real **block device** is and what it would change:

| | Podman volume (this lab) | Real block volume (cloud disk) |
|---|---|---|
| What it is | A directory on the host filesystem | A raw device (`/dev/vdb`) of fixed size |
| Who runs the filesystem | The host, for all users | The one instance that mounts it |
| Several writers at once | Yes, the host filesystem coordinates them | Not with an ordinary filesystem such as ext4: two machines would each cache and update metadata and corrupt it |
| Performance guarantees | None | Provisioned IOPS, throughput and latency |
| Survives instance loss | Only while the host disk lives | Yes, you re-attach it to another instance |

### Step 4 — See a filesystem on a block device (optional)
You can't create a real block device without administrator rights, but an ordinary file works as a "virtual disk": a block device is just an array of blocks, and a file can hold one. You format the file with ext4 and read it with `debugfs`, a tool that inspects an ext4 image without mounting it. Skip this step if `mkfs.ext4` or `debugfs` isn't installed.

```bash
truncate -s 64M lab-storage-disk.img
mkfs.ext4 -q -F lab-storage-disk.img
file lab-storage-disk.img
echo "hello block" > note.txt
debugfs -w -R "write note.txt note.txt" lab-storage-disk.img
debugfs -R "ls -l" lab-storage-disk.img
debugfs -R "cat note.txt" lab-storage-disk.img
```

Expected output (trimmed; the UUID and times differ):
```text
lab-storage-disk.img: Linux rev 1.0 ext4 filesystem data, UUID=<uuid> (extents) (64bit) (large files) (huge files)
debugfs 1.47.3 (8-Jul-2025)
Allocated inode: 13
debugfs 1.47.3 (8-Jul-2025)
      2   40755 (2)      0      0    1024  7-Nov-2026 19:55 .
      2   40755 (2)      0      0    1024  7-Nov-2026 19:55 ..
     11   40700 (2)      0      0   12288  7-Nov-2026 19:55 lost+found
     13  100644 (1)      0      0      12  7-Nov-2026 19:55 note.txt
debugfs 1.47.3 (8-Jul-2025)
hello block
```

The 64 MiB file now holds a complete filesystem with an inode table, a `lost+found` directory and your file. In a cloud, a block volume is the same thing at a larger scale: you get raw capacity and *you* decide what filesystem goes on it.

### Step 5 — Measure IOPS and throughput
The same disk can look fast or slow depending on the size of each operation. You write the same amount of data in two ways with `dd`, bypassing the cache (`oflag=direct`) so you measure the storage and not memory.

```bash
dd if=/dev/zero of=lab-storage-test.bin bs=1M count=256 oflag=direct 2>&1 | tail -1
dd if=/dev/zero of=lab-storage-test.bin bs=4k count=20000 oflag=direct 2>&1 | tail -1
rm -f lab-storage-test.bin
```

Expected output (your numbers will differ; some locales print a decimal comma):
```text
268435456 bytes (268 MB, 256 MiB) copied, 0.181368 s, 1.5 GB/s
81920000 bytes (82 MB, 78 MiB) copied, 1.00239 s, 81.7 MB/s
```

The first command makes 256 operations of 1 MiB; the second makes 20,000 operations of 4 KiB. In this capture the second did roughly 20,000 operations in one second (about 20,000 IOPS) but moved far fewer bytes per second. **Throughput = IOPS × operation size**: small operations are limited by IOPS, large ones by throughput. That is why a database (many small operations) and a backup job (few large ones) need different volume settings.

> [!NOTE]
> This measures your computer's disk and filesystem, not a cloud volume. It is only meant to show the relation between operation size, IOPS and throughput. Real volumes publish their provisioned IOPS and throughput limits.

### Part B: object storage

### Step 6 — Start the object store and define the client
You run Moto, an S3-compatible server, in a container. Port 5000 inside the container is published only on `127.0.0.1:9101` of your computer. Then you define `aws`, a shell function that runs the AWS command-line interface (CLI) version 2 in a container, pointed at your server, with dummy keys. `:Z` relabels the folder for SELinux and is harmless elsewhere.

```bash
podman run -d --name lab-storage-moto -p 127.0.0.1:9101:5000 docker.io/motoserver/moto:5.2.4

aws() { podman run --rm --network host -v "$PWD:/work:Z" -w /work \
  -e AWS_ACCESS_KEY_ID=test -e AWS_SECRET_ACCESS_KEY=test -e AWS_DEFAULT_REGION=us-east-1 -e AWS_PAGER= \
  docker.io/amazon/aws-cli:2.38.0 --endpoint-url http://127.0.0.1:9101 "$@"; }

sleep 4
aws --version
```

Expected output:
```text
aws-cli/2.38.0 Python/3.14.6 Linux/7.2.4-ogc3.1.fc44.x86_64 docker/x86_64.amzn.2023
```

> [!IMPORTANT]
> The function exists only in this terminal. If you open a new one, define it again (and `cd ~/storage-lab`). Because the CLI runs in a container, each call starts a container (about a second).

The only difference from using a real cloud is `--endpoint-url`: remove it, use real keys, and the same commands talk to a real provider. That is what "S3 API as a standard" means in practice.

### Step 7 — Buckets, objects, prefixes and metadata
You create a bucket, upload two objects and list them. Watch how `reports/2026/q3.txt` appears as a "folder".

```bash
aws s3 mb s3://lab-docs
echo "Quarterly report v1" > report.txt
aws s3 cp --no-progress report.txt s3://lab-docs/reports/2026/q3.txt
aws s3 cp --no-progress report.txt s3://lab-docs/readme.txt --metadata project=apollo,owner=ana --content-type text/plain
aws s3 ls s3://lab-docs
aws s3 ls s3://lab-docs/reports/
aws s3 ls s3://lab-docs --recursive
```

Expected output:
```text
make_bucket: lab-docs
upload: ./report.txt to s3://lab-docs/reports/2026/q3.txt
upload: ./report.txt to s3://lab-docs/readme.txt
                           PRE reports/
2026-11-07 19:02:42         20 readme.txt
                           PRE 2026/
2026-11-07 19:02:42         20 readme.txt
2026-11-07 19:02:41         20 reports/2026/q3.txt
```

The listing shows `PRE reports/` ("prefix"), not a folder. With `--recursive` you see what is really stored: two objects whose **keys** are `readme.txt` and `reports/2026/q3.txt`. Now read the metadata, download an object, and delete it:

```bash
aws s3api head-object --bucket lab-docs --key readme.txt
aws s3 cp --no-progress s3://lab-docs/readme.txt downloaded.txt
cat downloaded.txt
aws s3 rm s3://lab-docs/readme.txt
aws s3 ls s3://lab-docs --recursive
```

Expected output (the `ETag` and date vary):
```text
{
    "AcceptRanges": "bytes",
    "LastModified": "2026-11-07T19:02:42+00:00",
    "ContentLength": 20,
    "ETag": "\"b9ee29d4dddd352401ae31295dbe672d\"",
    "ContentType": "text/plain",
    "Metadata": {
        "project": "apollo",
        "owner": "ana"
    }
}
download: s3://lab-docs/readme.txt to ./downloaded.txt
Quarterly report v1
delete: s3://lab-docs/readme.txt
2026-11-07 19:02:41         20 reports/2026/q3.txt
```

The `ETag` is the content's identifier (an MD5 hash for a simple upload). The `Metadata` block holds the custom pairs you set. Without versioning, the delete is final: the object is gone.

> [!NOTE]
> Bucket names must be at least 3 characters (`aws s3 mb s3://ab` fails with `InvalidBucketName`). On a real provider, names are also globally unique and follow DNS naming rules, so `lab-docs` is probably taken: add a random suffix.

### Step 8 — Versioning, a delete marker and a restore
You create a second bucket, turn on versioning, overwrite an object and delete it, then bring the first version back.

```bash
aws s3 mb s3://lab-versioned
aws s3api put-bucket-versioning --bucket lab-versioned --versioning-configuration Status=Enabled
aws s3api get-bucket-versioning --bucket lab-versioned
echo "version one" > notes.txt
aws s3 cp --no-progress notes.txt s3://lab-versioned/notes.txt
echo "version two, edited" > notes.txt
aws s3 cp --no-progress notes.txt s3://lab-versioned/notes.txt
aws s3api list-object-versions --bucket lab-versioned --query 'Versions[].[VersionId,IsLatest,Size]' --output text
```

Expected output (the version IDs differ; the first row is the newest):
```text
make_bucket: lab-versioned
{
    "Status": "Enabled"
}
upload: ./notes.txt to s3://lab-versioned/notes.txt
upload: ./notes.txt to s3://lab-versioned/notes.txt
1ca2a323-276d-4987-b27c-b8496e1ef123	True	20
35cfc607-5f97-489e-acb7-5e775f48bedf	False	12
```

Two uploads to one key created **two versions**: 20 bytes ("version two, edited") is current, and 12 bytes ("version one") is kept. Copy the ID of the 12-byte version: you need it below as `<version-id-1>`. Now delete the object:

```bash
aws s3 rm s3://lab-versioned/notes.txt
aws s3 ls s3://lab-versioned
aws s3 cp --no-progress s3://lab-versioned/notes.txt - ; echo "exit code: $?"
aws s3api list-object-versions --bucket lab-versioned --query 'Versions[].[VersionId,IsLatest,Size]' --output text
aws s3api list-object-versions --bucket lab-versioned --query 'DeleteMarkers[].[VersionId,IsLatest]' --output text
```

Expected output:
```text
delete: s3://lab-versioned/notes.txt
download failed: s3://lab-versioned/notes.txt to - An error occurred (404) when calling the HeadObject operation: Not Found
exit code: 1
1ca2a323-276d-4987-b27c-b8496e1ef123	False	20
35cfc607-5f97-489e-acb7-5e775f48bedf	False	12
bbe0b185-3e0a-496d-a569-e5b1387c43b5	True
```

The bucket listing is empty and a download says `404 Not Found`, yet both versions are still there and a **delete marker** (the line with a single `True`) is now the current version. Nothing was erased. Restore the first version by copying it back so it becomes the newest version:

```bash
aws s3api get-object --bucket lab-versioned --key notes.txt --version-id <version-id-1> old-notes.txt
cat old-notes.txt
aws s3api copy-object --bucket lab-versioned --key notes.txt \
  --copy-source "lab-versioned/notes.txt?versionId=<version-id-1>" --query VersionId
aws s3 cp --no-progress s3://lab-versioned/notes.txt -
aws s3api list-object-versions --bucket lab-versioned --query 'Versions[].[VersionId,IsLatest,Size]' --output text
```

`<version-id-1>` is the 12-byte version's ID from the listing. Expected output:
```text
{
    "AcceptRanges": "bytes",
    "LastModified": "2026-11-07T18:56:28+00:00",
    "ContentLength": 12,
    "ETag": "\"dd8f100298ff923592ab35dc15788abc\"",
    "VersionId": "35cfc607-5f97-489e-acb7-5e775f48bedf",
    "ContentType": "text/plain",
    "Metadata": {}
}
version one
"806268d5-a59a-4468-bfa7-7e724ba7de22"
version one
806268d5-a59a-4468-bfa7-7e724ba7de22	True	12
1ca2a323-276d-4987-b27c-b8496e1ef123	False	20
35cfc607-5f97-489e-acb7-5e775f48bedf	False	12
```

You recovered the object: the copy is a new, third version with the old content, and it is current again (the delete marker is still in the history, hidden below it). To remove the data for good you would delete a specific version with `aws s3api delete-object --bucket lab-versioned --key notes.txt --version-id <id>`.

> [!NOTE]
> Deleting the delete marker itself (`delete-object` with the marker's `--version-id`) is the other way to undo a delete: the previous version becomes current again. It worked in our test on Moto, as it does on real S3.

### Step 9 — Add a lifecycle rule
You write two rules: expire objects under `tmp/` after 30 days, and expire noncurrent versions after 90 days. The JSON file holds the rules, and the CLI stores and reads them back.

```bash
cat > lifecycle.json <<'EOF'
{
  "Rules": [
    {
      "ID": "expire-tmp-after-30-days",
      "Status": "Enabled",
      "Filter": { "Prefix": "tmp/" },
      "Expiration": { "Days": 30 }
    },
    {
      "ID": "clean-old-versions",
      "Status": "Enabled",
      "Filter": { "Prefix": "" },
      "NoncurrentVersionExpiration": { "NoncurrentDays": 90 }
    }
  ]
}
EOF
aws s3api put-bucket-lifecycle-configuration --bucket lab-versioned --lifecycle-configuration file://lifecycle.json
aws s3api get-bucket-lifecycle-configuration --bucket lab-versioned
```

Expected output (field order and the first line may differ):
```text
{
    "TransitionDefaultMinimumObjectSize": "all_storage_classes_128K",
    "Rules": [
        {
            "Expiration": {
                "Days": 30
            },
            "ID": "expire-tmp-after-30-days",
            "Filter": {
                "Prefix": "tmp/"
            },
            "Status": "Enabled"
        },
        {
            "ID": "clean-old-versions",
            "Filter": {
                "Prefix": ""
            },
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 90
            }
        }
    ]
}
```

> [!NOTE]
> Moto stores and returns lifecycle rules but **never runs them**: objects don't expire here, and you can't wait 30 days to see it. On a real provider, a background process applies the rules, usually once a day. You have practised the part you control: writing a correct rule. A rule with an empty `Prefix` and `NoncurrentVersionExpiration` is the usual companion of versioning, so old versions don't pile up forever.

### Step 10 — Share an object with a pre-signed URL
The object `lab-docs/reports/2026/q3.txt` is private. You try to read it without credentials, then create a URL that grants a minute of access.

```bash
curl -s -o /dev/null -w "unsigned request: HTTP %{http_code}\n" http://127.0.0.1:9101/lab-docs/reports/2026/q3.txt
URL=$(aws s3 presign s3://lab-docs/reports/2026/q3.txt --expires-in 60)
echo "$URL"
curl -s -w "pre-signed request: HTTP %{http_code}\n" "$URL"
```

Expected output (`X-Amz-Date` and the signature differ):
```text
unsigned request: HTTP 403
http://127.0.0.1:9101/lab-docs/reports/2026/q3.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=test%2F20261107%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20261107T185711Z&X-Amz-Expires=60&X-Amz-SignedHeaders=host&X-Amz-Signature=<signature>
Quarterly report v1
pre-signed request: HTTP 200
```

Look at the URL. The path is the object. `X-Amz-Expires=60` is the lifetime in seconds, `X-Amz-Credential` names the key that signed it (but never the secret), and `X-Amz-Signature` proves the signer authorised exactly this request. The bucket stayed private, and `curl` used no credentials: whoever holds the URL gets the object. Change one character of the path or signature and the request is refused.

> [!NOTE]
> Moto does **not** enforce the expiry: the URL keeps working after the 60 seconds. A real provider returns `403 Forbidden` after expiry, which is what makes short-lived URLs useful. Don't rely on this emulator to test expiry logic.

### Step 11 — Open one prefix with a bucket policy
You create a bucket with a public and a private object, confirm both are refused to anonymous users, then add a **bucket policy** that allows everyone to read only `reports/*`.

```bash
aws s3 mb s3://lab-pub
echo "public report" > hi.txt
echo "private note" > other.txt
aws s3 cp --no-progress hi.txt s3://lab-pub/reports/hi.txt
aws s3 cp --no-progress other.txt s3://lab-pub/other.txt
for k in reports/hi.txt other.txt; do curl -s -o /dev/null -w "$k before the policy: HTTP %{http_code}\n" http://127.0.0.1:9101/lab-pub/$k; done
```

Expected output:
```text
make_bucket: lab-pub
upload: ./hi.txt to s3://lab-pub/reports/hi.txt
upload: ./other.txt to s3://lab-pub/other.txt
reports/hi.txt before the policy: HTTP 403
other.txt before the policy: HTTP 403
```

Now write the policy. `"Principal": "*"` means everyone, including people with no account:

```bash
cat > policy.json <<'EOF'
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "PublicReadReportsOnly",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::lab-pub/reports/*"
    }
  ]
}
EOF
aws s3api put-bucket-policy --bucket lab-pub --policy file://policy.json
for k in reports/hi.txt other.txt; do curl -s -o /dev/null -w "$k after the policy: HTTP %{http_code}\n" http://127.0.0.1:9101/lab-pub/$k; done
curl -s http://127.0.0.1:9101/lab-pub/reports/hi.txt
aws s3api delete-bucket-policy --bucket lab-pub
curl -s -o /dev/null -w "reports/hi.txt after removing the policy: HTTP %{http_code}\n" http://127.0.0.1:9101/lab-pub/reports/hi.txt
```

Expected output:
```text
reports/hi.txt after the policy: HTTP 200
other.txt after the policy: HTTP 403
public report
reports/hi.txt after removing the policy: HTTP 403
```

One statement exposed one prefix to the whole Internet, and a different object in the same bucket stayed private. Now picture the same policy with `"Resource": "arn:aws:s3:::lab-pub/*"`: every object, including whatever somebody uploads next year. That one-character difference is how most public-bucket leaks happen. In the policy, `Resource` is where you control the damage; on a real provider, **block public access** is the safety net that refuses such a policy altogether.

> [!NOTE]
> Moto doesn't enforce block public access. If you run `aws s3api put-public-access-block --bucket lab-pub --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true`, it stores and returns the settings, but a public policy is still accepted and still served. On a real provider with `BlockPublicPolicy` enabled, the `put-bucket-policy` above is rejected, and `RestrictPublicBuckets` blocks public access even from an existing policy. We couldn't verify those effects here, so check them against your provider's documentation.

## Verification
Check each result; every line should match.

1. Part A: `podman volume inspect lab-storage-data --format '{{.Mountpoint}}'` prints a path under your home folder, and `ls "$(podman volume inspect lab-storage-data --format '{{.Mountpoint}}')"` lists `both.log`, `note.txt`, `reader.txt` and `shared.log`.
2. Part B, objects: `aws s3 ls s3://lab-docs --recursive` lists exactly one key, `reports/2026/q3.txt`.
3. Versioning: `aws s3 cp --no-progress s3://lab-versioned/notes.txt -` prints `version one`, and `aws s3api list-object-versions --bucket lab-versioned --query 'length(Versions)'` prints `3`.
4. Lifecycle: `aws s3api get-bucket-lifecycle-configuration --bucket lab-versioned --query 'Rules[].ID'` lists `expire-tmp-after-30-days` and `clean-old-versions`.
5. Pre-signed URL: a new `aws s3 presign` URL fetched with `curl` returns the text, while the unsigned URL returns `403`.
6. Policy: with the policy applied, `reports/hi.txt` returns `200` and `other.txt` returns `403` (repeat Step 11's policy command if you removed it).

## Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| `Could not connect to the endpoint URL: "http://127.0.0.1:9101/"` | The Moto container isn't running, or you restarted it | `podman ps --filter name=lab-storage`; start it with `podman start lab-storage-moto`. Moto keeps its data in memory, so a restart empties every bucket: redo Steps 7 to 11 |
| `Error: ... address already in use` for port 9101 | Another program or container uses the port | Find it with `ss -ltn \| grep 9101`; stop your own earlier container, or change the first number in `-p 127.0.0.1:9101:5000` and in `--endpoint-url` |
| `aws: command not found` | The shell function is defined only in the terminal where you ran Step 6 | Define it again in this terminal |
| `Permission denied` when the AWS CLI container reads `report.txt` | SELinux blocks the bind mount, or you removed `:Z` | Keep `:Z` in the function and run from `~/storage-lab` |
| `make_bucket failed ... InvalidBucketName` | The name is shorter than 3 characters or has invalid characters | Use lowercase letters, digits and hyphens, 3 characters or more |
| `dd: failed to open ... Invalid argument` in Step 5 | The folder is on a filesystem without direct I/O support, such as `tmpfs` | Run the lab from a folder on a normal disk, or remove `oflag=direct` (the numbers then measure the cache) |
| `debugfs: command not found` or `mkfs.ext4: command not found` | `e2fsprogs` isn't installed | Install it, or skip the optional Step 4 |
| `curl` returns `403` for the pre-signed URL | The URL was cut off by the shell (the `&` characters), or you changed it | Keep it in a variable and quote it: `curl "$URL"` |

## Challenge (optional)
Create a bucket named `lab-site` and upload a small `index.html` under the prefix `site/`. Write a bucket policy that allows anonymous `GET` only for `site/*`, confirm it with `curl`, then change the `Resource` so that it also exposes a `private/` prefix. Compare the two policies and list which of the lesson's controls (block public access, least-privilege policy, pre-signed URLs) would have stopped the second one on a real provider. Then use `aws s3 sync ./site s3://lab-site/site` to upload a folder and `aws s3 rm s3://lab-site --recursive` to remove it.

## Cleanup
Remove every container, volume and file the lab created. The last command, which removes the three images, is optional: skip it if you use them for something else.

```bash
podman rm -f -t 0 lab-storage-moto lab-storage-writer lab-storage-reader lab-storage-appender 2>/dev/null
podman volume rm lab-storage-data
cd ~ && rm -rf ~/storage-lab
unset -f aws
# Optional: remove the downloaded images
podman rmi docker.io/motoserver/moto:5.2.4 docker.io/amazon/aws-cli:2.38.0 docker.io/library/alpine:3.20
```

Confirm that nothing is left:
```bash
podman ps -a --filter name=lab-storage --format '{{.Names}}'
podman volume ls --filter name=lab-storage
```

Both commands should print nothing apart from the volume table header.

## What you learned
- A Podman volume is a directory, not a block device. A real block volume gives you raw capacity with its own IOPS, throughput and latency, and one instance runs the filesystem on it; a file share runs the filesystem for many clients.
- Throughput equals IOPS times operation size, so small operations are limited by IOPS and large ones by throughput.
- Object storage is reached through an HTTP API (the S3 API) with a bucket, a key and metadata; prefixes only look like folders. Changing the endpoint is all it takes to move between S3-compatible services.
- Versioning turns a delete into a delete marker and keeps every version, so a restore is a copy of an earlier version. Lifecycle rules, written once, expire data automatically on a real provider.
- A pre-signed URL gives short-lived access to one private object, while a broad bucket policy makes data public to everyone: keep block public access on and scope `Resource` tightly.