api.files

create()

api.files.create({ name, content, mimeType }) → IaGenifyFile

Baut ein Dateiobjekt im Speicher. Nichts wird gesendet, nichts wird geschrieben, nichts wird geladen – das ist der Schritt vor all dem.

no networksynchronousreturns an object to fill
Using Python?

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

Parameter

namestring

Der Dateiname. Wird als-auf dem Konto verwendet; der Server reduziert es auf einen sicheren auf seiner eigenen Festplatte.

contentstring|Buffer|Blob

Die Bytes. Kann nach der Konstruktion eingestellt werden - es wird bei save() gelesen, nicht hier.

mimeTypestring

Voreinstellungen application/octet-stream, die die Kategorie macht document.

Es gibt synchron zurück und es gibt keine await: Das ist ein Konstrukteur in allen Namen.

Das Objekt

const file = api.files.create({ name: "notes.md", content: "…", mimeType: "text/markdown" });

file.name;
file.content;
file.mimeType;
file.sourceId;   // null until it is saved
file.location;   // null until it is saved
file.metadata;   // null until it is saved
file = api.files.create(name="notes.md", content="…", mime_type="text/markdown")

file.name
file.content
file.mime_type
file.source_id   # None until it is saved
file.location    # None until it is saved
file.metadata    # None until it is saved

Legen Sie den MimeType ein, obwohl er optional ist

Es entscheidet zwei Dinge, die später ungeschickt sind, sich zu ändern: die Kategorie und ob get() den Inhalt als Text oder als Bytes zurückgibt.

Du gehst durchKategorieget() gibt zurück
text /markdownDokumentInhalt als String
Anwendung /jsonDatenInhalt als String
Bild/pngBildInhalt als Puffer
(nichts)DokumentInhalt als Puffer
Wenn man es auslässt, kommt Text als Bytes zurück

Ohne Typ ist die Voreinstellung eine binäre, so dass eine perfekt lesbare Markdown-Datei als Puffer zurückgereicht wird. Eine Zeile der Arbeit jetzt, oder eine verwirrende spätere lesen.

Welcher Inhalt akzeptiert

// A string
api.files.create({ name: "notes.md", content: "# Notes", mimeType: "text/markdown" });

// Bytes you already have
api.files.create({ name: "chart.png", content: pngBuffer, mimeType: "image/png" });

// Something generated — write it out the moment you have it
const { outputs } = await assistant.generate({ inputs, outputs: [{ type: "image" }] });
api.files.create({ name: "cover.png", content: outputs[0].content, mimeType: outputs[0].mime });
import base64

# A string
api.files.create(name="notes.md", content="# Notes", mime_type="text/markdown")

# Bytes you already have
api.files.create(name="chart.png", content=png_bytes, mime_type="image/png")

# Something generated — write it out the moment you have it
outputs = assistant.generate(inputs=inputs, outputs=[{"type": "image"}])["outputs"]
api.files.create(name="cover.png", content=base64.b64decode(outputs[0]["content"]), mime_type=outputs[0].get("mime"))

Nachher ausfüllen

Der Inhalt wird gelesen, wenn Sie speichern, so dass das Objekt zuerst zu bauen und es später zu füllen ist in Ordnung — und es ist, wie update() Arbeit.

const file = api.files.create({ name: "log.txt", mimeType: "text/plain" });

file.content = lines.join("\n");
await file.save();

// And to replace it later
file.content = "…";
await file.update();
file = api.files.create(name="log.txt", content="", mime_type="text/plain")

file.content = "\n".join(lines)
file.save()

# And to replace it later
file.content = "…"
file.update()
Das Speichern ohne Inhalt wird vor Ort verweigert

Der SDK wirft vor jeder Anfrage, wenn Inhalt nicht definiert ist oder null — ein leerer String ist dagegen eine legitime leere Datei.