Verbindung
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.accountIdstringerforderlichDas Konto. Es ist Teil der Adresse, für die die Anfrage unterzeichnet wird.
credentials.bucketNamestringerforderlichDer Eimer.
credentials.accessKeyIdstringerforderlichDer Zugangsschlüssel eines Tokens erlaubt auf dem Eimer.
credentials.secretAccessKeystringerforderlichSein Geheimnis.
Es gibt keine Region: Dieser Anbieter dient einem Endpunkt pro Konto und region wird ignoriert, wenn Sie ihn senden. Das ist der einzige Unterschied zu S3 zum Zeitpunkt der Verbindung.
Die fünf Aktionen
| Aufruf | Funktion |
|---|---|
{ action: "list", prefix, limit, recursive, continuationToken } | Listet Schlüssel und Ordner unter einem Präfix auf. |
{ action: "overview" } | Die Ordner auf der obersten Ebene, mit Zählungen, Bytes und einer Auswahl von Schlüsseln. |
{ action: "object", key } | Liest ein Objekt — Text als Text, binär als Basis64. |
{ action: "upload", key, contentBase64 | content, contentType } | Schreibt ein Objekt. |
{ action: "delete", key } | Entfernt ein Objekt. |
Es gibt keine Ordner und list() zeigt sie sowieso an
Ein Eimer hält flache Schlüssel. list() gruppiert sie auf dem Schrägstrich und die Hände zurück folders neben objects, was einen Brausbaum möglich macht – und recursive: true schaltet die Gruppierung aus und gibt jeden Schlüssel unter dem Präfix zurück.
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
# }Seite
limit Standardmäßig auf 200 und ist auf 1000 begrenzt. truncated ist wahr, pass nextToken zurück als continuationToken für die folgende Seite.
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 geht in ContentBase64
Ein Bild oder ein PDF übergeben, da Inhalt geschrieben wird, da der String nicht ist. contentBase64 ist das Feld, das die Bytes behält, und contentType ist, was ein Browser später bedient wird.
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",
)Objekt sagt Ihnen, was es Ihnen gegeben hat
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": "…"} ← binaryDie Wahl erfolgt vom Inhaltstyp und, falls nicht, durch die Suche nach einem Nullbyte in den ersten 8 KB. Testen Sie für das Feld statt von einem.
Schlüssel mit Leerzeichen sind in Ordnung
Jedes Segment eines Schlüssels wird vor der Unterschrift der Anfrage verschlüsselt, so dass ein Leerzeichen, ein # oder ein Akzent in einem Namen behandelt wird. Ein Schlüssel ist niemals ein Pfad auf einer Festplatte — er kann Slashes ohne einen vorhandenen Ordner enthalten.