> ## Documentation Index
> Fetch the complete documentation index at: https://docs.magpiedb.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Magpie is a trace store for AI agents, used as a library (crate `magpiedb`, npm `magpiedb`, PyPI `magpiedb`) or as `magpie-server`, which takes OpenTelemetry (OTLP over HTTP on 127.0.0.1:4318 and gRPC on 127.0.0.1:4317 by default).
> Use only the commands, flags, routes and JSON shapes that these pages show. Do not guess a flag or a query field: if a page does not show it, say that the docs do not show it.
> Install the library with `cargo add magpiedb`, `npm install magpiedb` or `pip install magpiedb`. Get the server from the GitHub Releases download or the Docker image `ghcr.io/intojhanurag/magpie-server`, as the quick start shows.
> Only one process may write to a store folder at a time. For many apps, send traces to one `magpie-server`.
> `magpie-server doctor` checks a running server. An AI agent can query the traces over MCP at `/mcp` on the HTTP port (page guides/mcp).

# Set up Amazon S3

> The bucket, the IAM policy, the credentials and the lifecycle rules that a Magpie store on Amazon S3 needs, and what is not supported.

```sh theme={null}
export AWS_REGION=us-east-1     # the region of the bucket: it is never looked up
magpie-server --dir ./writer --s3 s3://BUCKETNAME/PREFIX
```

This setup ran once on AWS, from EC2 with an instance role and the writer policy below. It ran a writer, a restarted writer, a read-only server and a new writer from the bucket ([`docs/FINDINGS.md`](https://github.com/intojhanurag/magpie/blob/main/docs/FINDINGS.md) F95).

Every other point is read from the AWS docs and the code. The full list, with an AWS link for each point: [Amazon S3: what to set up](https://github.com/intojhanurag/magpie/blob/main/docs/S3.md#7-amazon-s3-what-to-set-up-and-what-is-not-supported) in `docs/S3.md`.

## Make the bucket

| setting | value |
| - | - |
| bucket type | A general purpose bucket in a commercial AWS region |
| storage class | S3 Standard. No archive classes (Glacier, Deep Archive, the archive tiers of Intelligent-Tiering): a GET of an archived object fails until it is restored. |
| encryption | The bucket's default, SSE-S3. SSE-KMS works with the key permissions below; no test ran it. |
| versioning | Off. With versioning on, the store works, but deletes cost more than they look ([Versioning](#versioning)). |
| [Block Public Access](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-control-block-public-access.html) | All four settings on. Magpie never makes an object public. |
| `AWS_REGION` | The region of the bucket. Without it, `us-east-1`. |
| prefix | Up to 800 bytes, no empty part, no part `.` or `..`, no control character. The keys are exactly `<prefix>/manifest/...`, `<prefix>/seg/...` and `<prefix>/probe/...`. |

### Not supported

| do not use | why |
| - | - |
| directory buckets (S3 Express One Zone) | `magpie-server` refuses a bucket name that ends in `--x-s3`, and `AWS_S3_EXPRESS`. |
| Object Lock with a default retention | It wants a checksum header on every PUT, which Magpie does not send, so the start-up check fails. With a checksum turned on by hand, the cleanup's deletes only add delete markers, and the locked versions stay and cost. Not tested. |
| access points, multi-region access points, other partitions (China, GovCloud) | `--s3` takes a plain bucket name. Not tested. |
| a profile or an SSO login alone | The server does not read `~/.aws/credentials`, `~/.aws/config`, `AWS_PROFILE`, the SSO cache or `credential_process`. |

## Give it credentials

What `magpie-server` can use, in the order it tries them:

| where it runs | what to set |
| - | - |
| anywhere, with keys | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_SESSION_TOKEN` for a session. Read once and not renewed: a session that ends stops the uploads (`lastError` shows `HTTP 400 ExpiredToken` or `HTTP 403`). They win over every role below. |
| EKS, a role for the service account ([IRSA](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html)) | Nothing: EKS sets `AWS_WEB_IDENTITY_TOKEN_FILE` and `AWS_ROLE_ARN`. Renewed by itself. |
| ECS, a task role ([container credentials](https://docs.aws.amazon.com/sdkref/latest/guide/feature-container-credentials.html)) | Nothing: ECS sets `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`. |
| EKS Pod Identity | Nothing: `AWS_CONTAINER_CREDENTIALS_FULL_URI` and `AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE`. |
| EC2, an [instance role](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-metadata-security-credentials.html) | Nothing: read from IMDSv2 and renewed. Set no keys in the environment, or they win. |
| a laptop with a profile or `aws sso login` | Export the profile's keys: `eval "$(aws configure export-credentials --profile <name> --format env)"`. They end with the session: for a server that runs long, use a role. |

The EC2 row was run (IMDSv2 only). A renewal of the role's keys was not seen: they lived 6.5 hours, the run 20 minutes. The other role rows are read from the source.

The endpoint and the other variables: [Environment variables](https://docs.magpiedb.com/reference/server-s3.md#environment-variables).

## Grant the IAM policy

The least that a **writer** needs, for the bucket `BUCKETNAME` and the prefix `PREFIX` (with no prefix, leave `PREFIX/` out). It was run on AWS for a writer, a restarted writer, a new writer from the bucket and a read-only server.

```json theme={null}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ListTheStore",
      "Effect": "Allow",
      "Action": "s3:ListBucket",
      "Resource": "arn:aws:s3:::BUCKETNAME",
      "Condition": { "StringLike": { "s3:prefix": ["PREFIX/manifest/*", "PREFIX/seg/*", "PREFIX/probe/*"] } }
    },
    {
      "Sid": "ReadTheStore",
      "Effect": "Allow",
      "Action": "s3:GetObject",
      "Resource": ["arn:aws:s3:::BUCKETNAME/PREFIX/manifest/*", "arn:aws:s3:::BUCKETNAME/PREFIX/seg/*", "arn:aws:s3:::BUCKETNAME/PREFIX/probe/*"]
    },
    {
      "Sid": "WriteAndCleanUp",
      "Effect": "Allow",
      "Action": ["s3:PutObject", "s3:DeleteObject"],
      "Resource": ["arn:aws:s3:::BUCKETNAME/PREFIX/manifest/*", "arn:aws:s3:::BUCKETNAME/PREFIX/seg/*", "arn:aws:s3:::BUCKETNAME/PREFIX/probe/*"]
    },
    {
      "Sid": "GiveUpAMultipartUpload",
      "Effect": "Allow",
      "Action": "s3:AbortMultipartUpload",
      "Resource": "arn:aws:s3:::BUCKETNAME/PREFIX/seg/*"
    }
  ]
}
```

| case | what to grant |
| - | - |
| a read-only server | `s3:ListBucket` for `PREFIX/manifest/*`, and `s3:GetObject` on `PREFIX/manifest/*` and `PREFIX/seg/*`. Not run. Keep `ListBucket`: without it a missing object answers 403, not 404, and the reader needs the 404 to load the newest manifest ([GetObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html)). |
| PREFIX holds `*` or `?` | Use `StringEquals` on `PREFIX/manifest/`, `PREFIX/seg/` and `PREFIX/probe/`: `StringLike` reads them as wildcards. Magpie lists with exactly those three prefixes. |
| SSE-KMS | Writer: `kms:GenerateDataKey` and `kms:Decrypt`; reader: `kms:Decrypt`; on the one key `arn:aws:kms:REGION:ACCOUNT_ID:key/KEY_ID`, and the key policy allows the role ([SSE-KMS](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingKMSEncryption.html)). Never a wildcard on KMS. |
| a bucket policy that demands the KMS header | `AWS_SERVER_SIDE_ENCRYPTION=aws:kms` and `AWS_SSE_KMS_KEY_ID=<key ARN>` (`AWS_SSE_BUCKET_KEY_ENABLED=true` for bucket keys). With the bucket's default set to that key, Magpie sends no header. |

`s3:PutObject` allows the start, the parts and the end of a multipart upload; giving one up needs `s3:AbortMultipartUpload` ([multipart upload](https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpuoverview.html)). A HEAD is allowed by `s3:GetObject`.

Not needed: `s3:CreateBucket`, `s3:ListAllMyBuckets`, `s3:GetBucketLocation`, any `...Version`, ACL or tagging action. Magpie makes no bucket and changes no bucket setting.

## Set the lifecycle rules

> [!WARNING]
>
> **No rule may expire or move current objects under the store's prefix** (for example "delete after 30 days", or a move to Glacier). A file of the first day is still read on day 400. Such a rule deletes live files: readers fail, and a new writer cannot download the store.

| rule | why |
| - | - |
| `AbortIncompleteMultipartUpload` after 1 to 7 days, on the prefix or the bucket | **Wanted.** A writer killed in the middle of a big file leaves parts that are billed until given up, and the cleanup cannot see them ([the rule](https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpu-abort-incomplete-mpu-lifecycle-config.html)). It never touches a finished object. |
| expire **noncurrent** versions, remove expired delete markers | Safe with versioning, and saves money. |

### Versioning

With versioning on, a delete without a version id only adds a delete marker; the old version stays and is billed ([versioning](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Versioning.html)). Magpie's cleanup deletes without version ids, and `gc.deletedFiles` counts keys, not versions. So the cleanup frees no storage by itself, and data deleted for privacy stays as an old version: add the rule for noncurrent versions.

A file uploaded again after a failed try adds a version too. After you turn versioning on for the first time, wait 15 minutes before the first write, as AWS says ([enabling](https://docs.aws.amazon.com/AmazonS3/latest/userguide/manage-versioning-examples.html)). Not tested: the MinIO of the tests has no versioning.

## Test your bucket

`scripts/s3-real-run.sh` runs the whole S3 test list against a bucket that exists, under one new prefix, and deletes everything it made:

```sh theme={null}
S3_BUCKET=<bucket> AWS_REGION=<region> AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... scripts/s3-real-run.sh --out results/s3-real-<date>.json
```

It needs a bucket, a region, credentials in the environment, the AWS CLI and `jq`. A profile alone is refused, with the command that exports its keys.

Before any test it proves that the credential can clean up: a missing permission stops the run with exit 2 and its name. The fault tests need a proxy on plain HTTP, so against AWS they show as NOT RUN.

> [accordion] What the script cleans up, and when it cannot
>
> After the tests it deletes every object, old version, delete marker and unfinished multipart upload under its prefix, and fails if anything is left. A stop in the middle (Ctrl-C, SIGTERM, a closed terminal) runs the same cleanup first.
>
> Not covered: `kill -9` of the script, a machine that loses power, a network that is gone at that moment. Then delete by hand what is under the prefix that the script printed at its start.
>
> The run uses one credential for everything, more than a server needs, because the cleanup deletes versions and unfinished uploads. Use a bucket that holds nothing you would miss. `scripts/s3-real-run.sh --help` shows the options; [The run on a real bucket](https://github.com/intojhanurag/magpie/blob/main/docs/S3.md#8-the-run-on-a-real-bucket) in `docs/S3.md` has the details.

Size limits and the cost of requests: [S3 with the server](https://docs.magpiedb.com/reference/server-s3.md#requests-and-cost).

> [card] [Store on S3](https://docs.magpiedb.com/operations/s3.md)
>
> Run the writer and read-only servers.

> [card] [S3 with the server](https://docs.magpiedb.com/reference/server-s3.md)
>
> Every flag, rule and status field.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.