api.files

get()

api.files.get(sourceId) → Promise<IaGenifyFile>

Legge un file con il suo contenuto. Il testo torna come una string, qualsiasi altra cosa como bytes — e l'oggetto è pronto a aggiornare.

GET /api/files/:idfreecontent included
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Parâmetros

sourceIdstringobrigatório

De salve() or list(). Manca → il SDK lance.

Retorno

const file = await api.files.get(sourceId);

file.name;       // "report.md"
file.mimeType;   // "text/markdown"
file.content;    // a string, here — a Buffer for binary
file.sourceId;   // set, so update() works straight away
file.location;   // the kind of storage it is in
file.metadata;   // { size, mimeType, originalName }
file = api.files.get(source_id)

file.name        # "report.md"
file.mime_type   # "text/markdown"
file.content     # a str, here — bytes for binary
file.source_id   # set, so update() works straight away
file.location    # the kind of storage it is in
file.metadata    # {"size", "mimeType", "originalName"}

Corda o octes, decisa per il tipo

Un file di tipo text-ish torna come una string; tutto il resto come un Buffer.

Tipomimecontenuto
text/* · json · xml · yaml · csv · markdown · plainuna corda
Image/* · audio/* · video/* · pdf · ningun altroa a buf
const file = await api.files.get(sourceId);

if (typeof file.content === "string") {
  console.log(file.content.slice(0, 200));
} else {
  await fs.writeFile(`./${file.name}`, file.content);
}
from pathlib import Path

file = api.files.get(source_id)

if isinstance(file.content, str):
    print(file.content[:200])
else:
    Path(file.name).write_bytes(file.content)
La decisione è il tipo, non i bytes

Un file di testo salvat senza un mimeType torna come un buffer. Imposta il tipo a create() e questo non sale mai.

Lee l'intero fascicolo

Non c'è range e streaming: il contenuto arriva in una risposta, come testo o come base64 que il SDK decodifica per voi. Per qualcosa di grande, che è memoria da ambele lati — dimeniu-lo prima de recuperar.

const files = await api.files.list();
const target = files.find((f) => f.id === sourceId);

if (target.size > 20 * 1024 * 1024) {
  throw new Error(`${target.name} is ${Math.round(target.size / 1e6)} MB — too big to read in one go`);
}

const file = await api.files.get(sourceId);
files = api.files.list()
target = next(f for f in files if f["id"] == source_id)

if target["size"] > 20 * 1024 * 1024:
    raise ValueError(f"{target['name']} is {round(target['size'] / 1e6)} MB — too big to read in one go")

file = api.files.get(source_id)

Erros

CódigoStatusQuando
— (erro lançado localmente)—- Non ha stèu passè.
file_not_found404Nessuna lissíduo di questo cont con que id.
file_missing_on_disk404Il record existe e i suoi bytes non.
file_missing_on_disk vale la pena notar, non retest

Il record e i bytes s'hann spartit. Retesting responde la medesima cosa; il che aiuta è cancellare il record e salvando il file di nuovo.

Un file appartenente a un altro account risponde la stessa 404: ogni consulta è mirat al llamante.