# get()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/files/get/
Language: zh

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

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

Status: GET /api/files/:id · free · content included

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `sourceId` | string | 必填 | 调自 [(已)](/zh/sdk/ref/files/save) 或 [(列表)](/zh/sdk/ref/files/list)失踪 SDK 扔掷 |

## 返回值 {#returns}

```javascript
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 }
```

## 字符串或字字节,由类型决定 {#text}

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

| MIME 类型 | 内容内容内容内容 |
| --- | --- |
| · 平原 | 字符串 |
| · 音频/ * 视频/ * 视频/ * · pdf · 任何其他 | a 缓冲器 |

```javascript
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);
}
```

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

## 读了整个文件 {#whole}

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

```javascript
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);
```

## 错误 {#errors}

| 代码 | 状态 | 触发条件 |
| --- | --- | --- |
| —（在本地抛出） | — | 没有源Id 过 。 |
| file_not_found | 404 | 此账户没有此id 的文档 。 |
| file_missing_on_disk | 404 | 记录存在, 字节没有。 |

> **file_missing_on_disk 值得注意,而不是重新尝试**
>
> 记录和字节已经分开。 重试回答同样的问题; 有助于删除记录和保存文件。

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

- [file.update()](/zh/sdk/ref/files/update): 物体回来准备就绪
- [list()](/zh/sdk/ref/files/list): 在阅读前先找到一个ID和一个大小。

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

```python
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)
```

```python
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)
```
