api.files

get()

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

读取含有文件内容的文件。文本以字符串形式返回,其他任何文本以字节形式返回,对象已准备更新。

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.

参数

sourceIdstring必填

调自 (已) 或 (列表)失踪 SDK 扔掷

返回值

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

字符串或字字节,由类型决定

类型为文本- ish 的文件以字符串形式返回; 其它所有文件都以字符串形式返回 Buffer.

MIME 类型内容内容内容内容
· 平原字符串
· 音频/ * 视频/ * 视频/ * · pdf · 任何其他a 缓冲器
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)
此项决定是类型,而不是字节

保存的文本文件, 不带 mimeType , 则以缓冲形式返回。 设置创建() 时的类型, 并且此类型永远不会出现 。

读了整个文件

没有范围,也没有流流:内容以一个答复、文本或基数64 到达,即 SDK 为你解码。对于大的东西, 即两边的记忆-- 在你取回之前大小。

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)

错误

代码状态触发条件
—(在本地抛出)—没有源Id 过 。
file_not_found404此账户没有此id 的文档 。
file_missing_on_disk404记录存在, 字节没有。
file_missing_on_disk 值得注意,而不是重新尝试

记录和字节已经分开。 重试回答同样的问题; 有助于删除记录和保存文件。

属于另一个账户的文件也回答相同 404:每个查找都被定位到呼叫者。