# 上传数据库

> 上传数据库 — Turso 文档。
> Canonical HTML: https://tursodb.cn/zh-cn/docs/api-reference/databases/upload/
> Upstream Source (MIT): https://docs.turso.tech/api-reference/databases/upload
> Language: zh-CN

---
将 SQLite 数据库文件上传到通过 `seed.type = "database_upload"` 创建的数据库。

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

此端点位于**数据库主机名**（例如 `my-db-my-org.turso.io`），而非 `api.turso.tech`。

</div></div>


<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

你必须先[创建数据库](/zh-cn/docs/api-reference/databases/create/)，并设置 `seed.type = "database_upload"`。

</div></div>


<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

此端点需要**数据库令牌**，而非平台 API 令牌。你可以在创建数据库后[创建数据库令牌](/zh-cn/docs/api-reference/databases/create-token/)。

</div></div>


## 请求头

| 请求头 | 是否必需 | 描述 |
| - | - | - |
| `Authorization` | 是 | 数据库的 Bearer 令牌 |
| `Content-Length` | 是 | 数据库文件的大小（以字节为单位） |
| `x-turso-encryption-key` | 否 | Base64 编码的加密密钥（256 位加密算法为 32 字节，128 位加密算法为 16 字节） |
| `x-turso-encryption-cipher` | 否 | 加密算法。可选项见下表。 |

## 请求体

原始二进制 SQLite 数据库文件。

## 数据库文件要求

SQLite 数据库文件必须满足以下要求：

* **日志模式**：WAL（`PRAGMA journal_mode = WAL`）
* **页大小**：4096 字节（`PRAGMA page_size = 4096`）
* **自动清理（auto vacuum）**：禁用（`PRAGMA auto_vacuum = 0`）
* **编码**：UTF-8（`PRAGMA encoding = 'UTF-8'`）

对于加密数据库，你还必须将保留字节数设置为与加密算法相匹配的值：

| 加密算法 | 保留字节数 |
| - | - |
| `aes256gcm` | 28 |
| `aes128gcm` | 28 |
| `chacha20poly1305` | 28 |
| `aegis128l` | 32 |
| `aegis128x2` | 32 |
| `aegis128x4` | 32 |
| `aegis256` | 48 |
| `aegis256x2` | 48 |
| `aegis256x4` | 48 |

## 响应

### 成功

成功时返回状态为 `200 OK` 的空响应。

### 错误

| 状态码 | 描述 |
| - | - |
| `400` | 无效的数据库文件（格式或设置错误，或文件损坏） |
| `401` | 无效或缺失的授权令牌 |

```bash cURL
curl -X POST 'https://{database_hostname}/v1/upload' \
  -H 'Authorization: Bearer DATABASE_TOKEN' \
  -H 'Content-Length: FILE_SIZE' \
  --data-binary '@/path/to/database.db'
```

```bash cURL (with encryption)
curl -X POST 'https://{database_hostname}/v1/upload' \
  -H 'Authorization: Bearer DATABASE_TOKEN' \
  -H 'Content-Length: FILE_SIZE' \
  -H 'x-turso-encryption-key: BASE64_ENCRYPTION_KEY' \
  -H 'x-turso-encryption-cipher: aes256gcm' \
  --data-binary '@/path/to/database.db'
```
