api.files

file.save()

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

将文件写入账户, 本地磁盘, 或者两者都写入。 这里的通话需要信用。

POST /api/files/save1 creditrefunded if the write fails
Using Python?

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

参数

storageIdstring

要写入哪个存储。 没有它, 账户违约 。

pathstring

Node 仅此。同时将字节写入本地路径,创建文件夹。

localOnlyboolean

与 path:在本地和 停止停止。没有上载任何信息,也没有指控。

使用它的三个方法

// 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" }
# 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"}
本地写作失败 本地签名无法失败

如果路径无法写入, 则 SDK 在控制台上警告并直接上传。 当本地副本是重要的时, 请自己检查文件—— 仅读文件夹不会上升 。

返回值

{
  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
}
{
    "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,
}

物体从中学习: file.sourceId, file.location 和 file.metadata 设置,这是它制造 (更新) 之后可以直接进行。

选择它去到哪里

// 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 });
# 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)
储存种类保存到它
对象 □ 数据库是,-账户上保留字节
文件系统( 计算机)拒绝: storage_is_local
电话 浏览器拒绝: storage_is_local

拒绝的来来 之前 信用已经用完。 这不是一次不重复的尝试: 那些设备保存着自己的文件, 写它们的方法来自设备 。

功劳,当它回来的时候

  • 写作前先记一个分数
  • 如果写作失败,它就是 全额退还 并且你得到 502 storage_failed.
  • 首先出现的拒绝——没有储存、当地储存、没有内容——根本不花费任何费用。

得到回复后,文件正在不断整理

当类别是 document,文本在回复发送后就索引化为语义搜索索引。因此,文件可以立即读取,并在稍后某个时刻逐个查找。

你不要在同呼吸中寻找你刚刚保存的东西

刻意不等待索引——它能迅速保存,随后立即进行搜索可能还没有看到。

错误

代码状态触发条件信贷信贷信贷信贷信贷
—(在本地抛出)—内容未定义或无效。无
missing_file_content400服务器上既没有文件也没有到达文本内容 。无
storage_not_found404该账户根本没有存储。无
storage_is_local400目标是一台电脑或一部电话无
not_enough_credits402余额不会弥补。无
storage_failed502写作本身失败了 。已退还退退
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;
}
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)