# Nube R2

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

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

Un cubo di tes, firmato e rtèalt sul protocollo S3. Cinqua azioni: lista, vista d'insieme, objete, upload, elimina.

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

## Conecta {#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 | obrigatório | Il conto. Fa parte del indirizzo per il cui petizione è firmata. |
| `credentials.bucketName` | string | obrigatório | Il cusco. |
| `credentials.accessKeyId` | string | obrigatório | La chiave d'accesso di un token permise a quel cub. |
| `credentials.secretAccessKey` | string | obrigatório | E' segret. |

Non c'è una regione: questo provider serve un endpoint per conto, e `region` è ignorat se lo envia. Questa è la unica differenza da [S3](/pt/sdk/ref/providers/s3) al tempo di connezione.

## Le cinque azioni {#actions}

| 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 {#folders}

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.

```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
// }
```

## Invocazione {#paging}

`limit` predefinita a 200 e é plafonata a 1000. Quando `truncated` è true, passa `nextToken` inapoi come `continuationToken` per la pagina seguente.

```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);
```

## Binario va in contentBase64 {#binary}

> **contenuto è solo per text**
>
> 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.

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

## objecte ti dice qual ti da {#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
```

La 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 {#keys}

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.

- [connect()](/pt/sdk/ref/storage/connect): Il grido in sé, e onde va il secrete.
- [Amazònia S3](/pt/sdk/ref/providers/s3): Le medesime cinque azioni, con una regione.
- [run()](/pt/sdk/ref/connected-storage/run): Ogni azione, e lo que cada una toma.

```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
```
