# file.save()

Canonical URL: https://developers.iagenify.com/pt/sdk/ref/files/save/
Language: pt

`file.save({ storageId, path, localOnly }) → Promise<Result>`

Scrie il file — al cont, al disco locale, o a ambos. L'un d'aqui che costa un credito.

Status: POST /api/files/save · 1 credit · refunded if the write fails

## Parâmetros {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `storageId` | string |  | A qual stoccatura scriver. Senza di essa, il default del conto. |
| `path` | string |  | Nodesolo. Scrive anche i bytes a questo percorso local, creando i folders. |
| `localOnly` | boolean |  | Con `path`: scrive localmente e **stop**. Nada è caricato, niente è caricato. |

## Tres modos d'utilitîla {#three}

```javascript
// 1. To the account — the usual one
await file.save();

// 2. To the account AND to this machine, in one call
await file.save({ path: "./out/report.md" });

// 3. To this machine only — no request, no credit
await file.save({ path: "./out/report.md", localOnly: true });
// { ok: true, status: "saved_local", path: "./out/report.md" }
```

> **Una scrittura locale non fa fail non fail la chiamata**
>
> Se il percorso non puèr scrivi, il SDK advertiu la console e prosegui al upload. Controla il file tu stesso quando la copia locale è quella che importa — un dossier solo di lettura non va sollevare.

## Retorno {#returns}

```javascript
{
  ok: true,
  status: "saved",
  sourceId: "…",            // how you find it again
  name: "report.md",
  category: "document",
  mimeType: "text/markdown",
  location: "object",       // the kind of storage it landed in
  metadata: {
    size: 2048,
    storageId: "…",
    remainingBytes: null,   // what is left, when the storage tracks it
    durationMs: 260
  },
  creditsUsed: 1
}
```

L'ogget impara da lui: `file.sourceId`, `file.location` e `file.metadata` sono set, che è ciò che rende [update()](/pt/sdk/ref/files/update)pozibil drept.

## Scelse a dove va {#target}

```javascript
// The account's default
await file.save();

// A storage you named
const all = await api.storage.list();
const archive = all.find((s) => s.label === "Archive");
await file.save({ storageId: archive.id });
```

| Tipo di stoccaggio | Salve a lui |
| --- | --- |
| Objeto · base de données | si — bytes sono mantenuti a s'accusa |
| sistema de fiches (un computer) | Refusat: storage_is_local |
| Telefono · Navegador | Refusat: storage_is_local |

Il rejet viene **ante** il credito è spendet. Non è un obliguât di riprovare: quei dispositivi mantene i propri files, e la forma di scrii-li è dal dispositivo.

## Il credito, e quando torna {#cost}

- Un credito è preso prima de la scrittura.
- Se la scrittura poi failed, è **refinançât in full** e si obteve `502 storage_failed`.
- Un renegazione che prima occasiona — ni stoccatura, ni local, ni content — non costa nulla.

## Un documente continua a ser lavorat sul dopo che si obten la risposta {#indexing}

Quando la categoria è `document`, il testo è indexat per la ricerca semantica una volta che la risposta è inviata. Quindi un file è immediatamente leggibile e reperibile-mediante-significati un moment dopo.

> **Non cercare per algo che appena salvat, con la stessa respirazione**
>
> Indiczation non è deliberatamente atteso — mantiene la salvata veloci. Una ricerca in seguida puè non vedr.

## Erros {#errors}

| Código | Status | Quando | Crédito |
| --- | --- | --- | --- |
| — (erro lançado localmente) | — | il contenuto non è definito o null. | non è preso |
| missing_file_content | 400 | Ni un file ni un contenit textuèt arrivè al server. | non è preso |
| storage_not_found | 404 | Il cont non has stoccat dumnum. | non è preso |
| storage_is_local | 400 | L'obiettivo è un computer o un telefono. | non è preso |
| not_enough_credits | 402 | Il saldo non lo coprirà. | non è preso |
| storage_failed | 502 | La scrittura stessa ha fallit. | restituito |

```javascript
import { IaGenifyError } from "iagenify";

try {
  await file.save({ storageId });
} catch (err) {
  if (err instanceof IaGenifyError && err.code === "storage_is_local") {
    // That storage is a device. Write it there instead.
    const device = await api.storage.get({ id: storageId });
    await device.files.write("report.md", file.content);
  } else throw err;
}
```

- [file.update()](/pt/sdk/ref/files/update): Sostituire il contenuto dopo.
- [Fichîes Locales](/pt/sdk/ref/local-files): Scrivindo a dosi proprio de una máquina in lugar.
- [Créditos](/pt/sdk/concepts/credits): Como un gasto è preso e restituit.

```python
# 1. To the account — the usual one
file.save()

# 2. To the account AND to this machine, in one call
file.save(path="./out/report.md")

# 3. To this machine only — no request, no credit
file.save(path="./out/report.md", local_only=True)
# {"ok": True, "status": "saved_local", "path": "./out/report.md"}
```

```python
{
    "ok": True,
    "status": "saved",
    "sourceId": "…",            # how you find it again
    "name": "report.md",
    "category": "document",
    "mimeType": "text/markdown",
    "location": "object",       # the kind of storage it landed in
    "metadata": {
        "size": 2048,
        "storageId": "…",
        "remainingBytes": None,   # what is left, when the storage tracks it
        "durationMs": 260,
    },
    "creditsUsed": 1,
}
```

```python
# The account's default
file.save()

# A storage you named
archive = next(s for s in api.storage.list() if s.label == "Archive")
file.save(storage_id=archive.id)
```

```python
from iagenify import IaGenifyError

try:
    file.save(storage_id=storage_id)
except IaGenifyError as error:
    if error.code != "storage_is_local":
        raise
    # That storage is a device. Write it there instead.
    device = api.storage.get(id=storage_id)
    device.files.write("report.md", file.content)
```
