# Cloudflare R2

Canonical URL: https://developers.iagenify.com/sdk/ref/providers/r2/
Language: en

`api.storage.connect({ type: "object", provider: "r2", endpoint })`

A bucket of your own, signed and reached over the S3 protocol. Five actions: list, overview, object, upload, delete.

Status: type: object · S3-compatible · no region

## Connecting {#connect}

```javascript
const bucket = await api.storage.connect({
  label: "Assets",
  type: "object",
  provider: "r2",
  endpoint: {
    credentials: {
      accountId: "…",
      bucketName: "assets",
      accessKeyId: "…",
      secretAccessKey: "…",
    },
  },
});
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `credentials.accountId` | string | required | The account. It is part of the address the request is signed for. |
| `credentials.bucketName` | string | required | The bucket. |
| `credentials.accessKeyId` | string | required | The access key of a token allowed on that bucket. |
| `credentials.secretAccessKey` | string | required | Its secret. |

There is no region: this provider serves one endpoint per account, and `region` is ignored if you send it. That is the only difference from [S3](/sdk/ref/providers/s3) at connection time.

## The five actions {#actions}

| Call | Does |
| --- | --- |
| `{ action: "list", prefix, limit, recursive, continuationToken }` | Lists keys and folders under a prefix. |
| `{ action: "overview" }` | The top-level folders, with counts, bytes and a sample of keys. |
| `{ action: "object", key }` | Reads one object — text as text, binary as base64. |
| `{ action: "upload", key, contentBase64 \| content, contentType }` | Writes one object. |
| `{ action: "delete", key }` | Removes one object. |

## There are no folders, and list() shows them anyway {#folders}

A bucket holds flat keys. `list()` groups them on the slash and hands back `folders` beside `objects`, which is what makes a browsable tree possible — and `recursive: true` turns the grouping off and returns every key under the prefix.

```javascript
await bucket.run({ action: "list", prefix: "invoices/" });
// {
//   prefix: "invoices/",
//   folders: ["invoices/2025/", "invoices/2026/"],
//   objects: [{ key: "invoices/README.md", size: 184, modifiedAt: "…" }],
//   truncated: false,
//   nextToken: null
// }
```

## Paging {#paging}

`limit` defaults to 200 and is capped at 1000. When `truncated` is true, pass `nextToken` back as `continuationToken` for the following page.

```javascript
let token = null;
const keys = [];

do {
  const page = await bucket.run({
    action: "list", prefix: "invoices/", recursive: true, limit: 1000,
    ...(token ? { continuationToken: token } : {}),
  });
  keys.push(...page.objects.map((o) => o.key));
  token = page.truncated ? page.nextToken : null;
} while (token);
```

## Binary goes in contentBase64 {#binary}

> **content is for text only**
>
> An image or a PDF passed as content is written as the string it is not. contentBase64 is the field that keeps the bytes, and contentType is what a browser will later be served.

```javascript
await bucket.run({
  action: "upload",
  key: "assets/fox.png",
  contentBase64: buffer.toString("base64"),
  contentType: "image/png",
});
```

## object tells you which it gave you {#reading}

```javascript
const file = await bucket.run({ action: "object", key: "invoices/README.md" });
// { key, size, contentType, text: "…" }        ← text

const image = await bucket.run({ action: "object", key: "assets/fox.png" });
// { key, size, contentType, base64: "…" }      ← binary
```

The choice is made from the content type and, failing that, by looking for a zero byte in the first 8 KB. Test for the field rather than assuming one.

## Keys with spaces are fine {#keys}

Each segment of a key is encoded before the request is signed, so a space, a `#` or an accent in a name is handled. A key is never a path on a disk — it may contain slashes without any folder existing.

- [connect()](/sdk/ref/storage/connect): The call itself, and where the secret goes.
- [Amazon S3](/sdk/ref/providers/s3): The same five actions, with a region.
- [run()](/sdk/ref/connected-storage/run): Every action, and what each one takes.

```python
bucket = api.storage.connect(
    label="Assets",
    type="object",
    provider="r2",
    endpoint={
        "credentials": {
            "accountId": "…",
            "bucketName": "assets",
            "accessKeyId": "…",
            "secretAccessKey": "…",
        },
    },
)
```

```python
bucket.run(action="list", prefix="invoices/")
# {
#   "prefix": "invoices/",
#   "folders": ["invoices/2025/", "invoices/2026/"],
#   "objects": [{"key": "invoices/README.md", "size": 184, "modifiedAt": "…"}],
#   "truncated": False,
#   "nextToken": None
# }
```

```python
token = None
keys = []

while True:
    page = bucket.run(action="list", prefix="invoices/", recursive=True, limit=1000,
                      **({"continuationToken": token} if token else {}))
    keys.extend(o["key"] for o in page["objects"])
    token = page["nextToken"] if page["truncated"] else None
    if not token:
        break
```

```python
import base64

bucket.run(
    action="upload",
    key="assets/fox.png",
    contentBase64=base64.b64encode(png_bytes).decode(),
    contentType="image/png",
)
```

```python
file = bucket.run(action="object", key="invoices/README.md")
# {"key", "size", "contentType", "text": "…"}        ← text

image = bucket.run(action="object", key="assets/fox.png")
# {"key", "size", "contentType", "base64": "…"}      ← binary
```
