# 上传

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/connected-storage/run-write/upload/
Language: zh

`storage.run({ action: "upload", key, content | contentBase64, contentType })`

将字节放在键下。文字不变,其他的则作为基数64——这种区分不是可选的。

Status: object storage · free · replaces without warning

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `key` | string | 必填 | 鞭子是字符,不是文件夹 |
| `content` | string |  | 文本内容, 按原样发送 。 |
| `contentBase64` | string |  | **任何二进制的东西** 以字节64为基数 |
| `contentType` | string |  | 值得考虑——它决定读者如何看待它。 |

```javascript
// Text
await storage.run({ action: "upload", key: "notes/today.md",
  content: "# Today\n\n…", contentType: "text/markdown" });

// Binary — base64, always
await storage.run({ action: "upload", key: "invoices/2026/01/a.pdf",
  contentBase64: pdfBuffer.toString("base64"), contentType: "application/pdf" });
// { ok: true, key: "invoices/2026/01/a.pdf", size: 183204 }
```

> **当文本到达时发送的二进制已损坏**
>
> 缓冲字符串通过文本编码,该编码无法通过任意字节生存。上传成功和文件被打破——这是最糟糕的失败,因为没有报告。

## 以无要求取代 {#replace}

现有密钥被覆盖, 回复与新写一样。 没有前一版本 。

```javascript
// Check first, if replacing would be wrong
const page = await storage.run({ action: "list", prefix: key });
if ((page.objects || []).some((o) => o.key === key)) {
  throw new Error(`${key} already exists`);
}

await storage.run({ action: "upload", key, contentBase64, contentType });
```

## 在第一个写入前设计密钥 {#keys}

- 鞭笞使前置置置件,而前置置件是唯一的物体存储分组。
- 将您要过滤的东西放在左边: `invoices/2026/01/` 列出一个月; `01/2026/invoices/` 没有。
- 键在稍后很难更改——重命名是复制和删除,一个对象一次。

## 空间满时拒绝 {#quota}

`quota_exceeded`,在发送字节之前检查。 [使用](/zh/sdk/ref/connected-storage/run/usage) 之前,一个大宗进口比发现中途路程便宜。

- [对象对象](/zh/sdk/ref/connected-storage/run/object): 给一个女主人送礼
- [列表中](/zh/sdk/ref/connected-storage/run/list): 和尾巴,和小尾巴。
- [run()](/zh/sdk/ref/connected-storage/run): 行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动

```python
import base64

# Text
storage.run(action="upload", key="notes/today.md",
            content="# Today\n\n…", contentType="text/markdown")

# Binary — base64, always
storage.run(action="upload", key="invoices/2026/01/a.pdf",
            contentBase64=base64.b64encode(pdf_bytes).decode(), contentType="application/pdf")
# {"ok": True, "key": "invoices/2026/01/a.pdf", "size": 183204}
```

```python
# Check first, if replacing would be wrong
page = storage.run(action="list", prefix=key)
if any(o["key"] == key for o in page.get("objects") or []):
    raise FileExistsError(f"{key} already exists")

storage.run(action="upload", key=key, contentBase64=content_base64, contentType=content_type)
```
