Parâmetros
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.
| Tipomime | contenuto |
|---|---|
| text/* · json · xml · yaml · csv · markdown · plain | una corda |
| Image/* · audio/* · video/* · pdf · ningun altro | a 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)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ódigo | Status | Quando |
|---|---|---|
| — (erro lançado localmente) | — | - Non ha stèu passè. |
| file_not_found | 404 | Nessuna lissíduo di questo cont con que id. |
| file_missing_on_disk | 404 | Il record existe e i suoi bytes non. |
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.