---
title: Cloudflare R2
icon: cloud
description: Set up the R2 resource in Python.
---

## Dependencies

```bash
uv sync --extra s3
```

R2 uses the same `aiobotocore` dependency as S3 (R2 is S3-compatible).

For credential setup, see the [R2 Setup](/home/setup/r2) guide.

## Configuration

```python
import os
from mirage import Workspace, MountMode
from mirage.resource.r2 import R2Config, R2Resource

config = R2Config(
    bucket=os.environ["R2_BUCKET"],
    account_id=os.environ["R2_ACCOUNT_ID"],
    access_key_id=os.environ["R2_ACCESS_KEY_ID"],
    secret_access_key=os.environ["R2_SECRET_ACCESS_KEY"],
)
resource = R2Resource(config)
ws = Workspace({"/r2/": resource}, mode=MountMode.READ)
```

## Config Reference

| Field               | Required | Default  | Description                                     |
| ------------------- | -------- | -------- | ----------------------------------------------- |
| `bucket`            | Yes      |          | R2 bucket name                                  |
| `account_id`        | No       | `None`   | Cloudflare account ID (used to derive endpoint) |
| `endpoint_url`      | No       | `None`   | Custom endpoint (overrides account_id)          |
| `access_key_id`     | No       | `None`   | R2 API token access key                         |
| `secret_access_key` | No       | `None`   | R2 API token secret key                         |
| `aws_profile`       | No       | `None`   | AWS CLI profile name                            |
| `region`            | No       | `"auto"` | Region                                          |
| `timeout`           | No       | `30`     | Request timeout in seconds                      |
| `proxy`             | No       | `None`   | HTTP proxy URL                                  |

Either `account_id` or `endpoint_url` must be provided. The endpoint is derived as `https://{account_id}.r2.cloudflarestorage.com`.
