Connecting
const bucket = await api.storage.connect({
label: "Assets",
type: "object",
provider: "r2",
endpoint: {
credentials: {
accountId: "…",
bucketName: "assets",
accessKeyId: "…",
secretAccessKey: "…",
},
},
});bucket = api.storage.connect(
label="Assets",
type="object",
provider="r2",
endpoint={
"credentials": {
"accountId": "…",
"bucketName": "assets",
"accessKeyId": "…",
"secretAccessKey": "…",
},
},
)credentials.accountIdstringrequiredThe account. It is part of the address the request is signed for.
credentials.bucketNamestringrequiredThe bucket.
credentials.accessKeyIdstringrequiredThe access key of a token allowed on that bucket.
credentials.secretAccessKeystringrequiredIts 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 at connection time.
The five 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
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.
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
// }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
# }Paging
limit defaults to 200 and is capped at 1000. When truncated is true, pass nextToken back as continuationToken for the following page.
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);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:
breakBinary goes in contentBase64
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.
await bucket.run({
action: "upload",
key: "assets/fox.png",
contentBase64: buffer.toString("base64"),
contentType: "image/png",
});import base64
bucket.run(
action="upload",
key="assets/fox.png",
contentBase64=base64.b64encode(png_bytes).decode(),
contentType="image/png",
)object tells you which it gave you
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: "…" } ← binaryfile = 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": "…"} ← binaryThe 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
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.