Conecta
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.accountIdstringobrigatórioIl conto. Fa parte del indirizzo per il cui petizione è firmata.
credentials.bucketNamestringobrigatórioIl cusco.
credentials.accessKeyIdstringobrigatórioLa chiave d'accesso di un token permise a quel cub.
credentials.secretAccessKeystringobrigatórioE' segret.
Non c'è una regione: questo provider serve un endpoint per conto, e region è ignorat se lo envia. Questa è la unica differenza da S3 al tempo di connezione.
Le cinque azioni
| Chamada | Função |
|---|---|
{ action: "list", prefix, limit, recursive, continuationToken } | Lista chiues e dosaris sotto un prefixiu. |
{ action: "overview" } | Le cartelle di alto livello, con conta, bytes e un campione de chiues. |
{ action: "object", key } | Lee un oggetto — testo come testo, binario come base64. |
{ action: "upload", key, contentBase64 | content, contentType } | Scrie un oggetto. |
{ action: "delete", key } | Retira un objete. |
Non es pas de folari, e list() mostra-le de totu modo
Un cusco tiene tasche plane. list() le raggrupa pel slash e le dà la mano folders al lado objects, che è ciò che rende possibile un arbore browsable — e recursive: true disliga la raggrupare e restitue ogni tecla sotto il 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
# }Invocazione
limit predefinita a 200 e é plafonata a 1000. Quando truncated è true, passa nextToken inapoi come continuationToken per la pagina seguente.
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:
breakBinario va in contentBase64
Una immagine o un PDF passat come il contenuto è scritto come la string non lo è. contentBase64 è il campo che mantiene i bytes, e contentType è a che un browser sarà servit poi.
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",
)objecte ti dice qual ti da
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": "…"} ← binaryLa scelta è fatta a partir del tipo di contenuto e, a defas, a la ricerca di un octet zero in 8 KB. Teste per il campo invece di suporre un.
Chiaves con espaços sono ben
Cada segmento di una chiave è codificat prima di la richiesta è firmata, quindi un spazio, un # o un accento in un nome è maneggiat. Una chiave non è mai un percorso su un disco — può contener slashs senza nessun dosar existent.