> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# S3

> 在 Kubernetes 部署中，将项目文件及其历史记录保存在 S3 兼容存储中。

项目文件及其历史记录由 **history-v1** 服务存储，模板文件由 **filestore** 服务存储，二者都位于每个 Ayakaleaf Pro pod 中。默认情况下，它们写入数据卷；使用 S3 时，它们写入存储桶。所需的存储桶和权限请参见 [S3](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/configuration/overleaf-toolkit/s3.md)。

| | |
| - | - |
| 服务 | history-v1（项目文件和历史记录）、filestore（模板文件），位于每个 Ayakaleaf Pro pod 中 |
| Pod → S3 | S3 端点，每次读取或写入文件时访问 |
| 必需 | 对于[多个副本](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/configuration/kubernetes/horizontal-scaling.md)：它们的数据卷不共享，而存储桶是共享的 |
| 定期任务 | 无 |

> [!WARNING]
>
> 请在用户创建项目之前选择 S3。已经将文件保存在卷上的实例需要先进行 [S3 迁移](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/maintenance/s3-migration.md)。

### 配置

**1. 存储桶和密钥**

按照 [S3 设置](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/configuration/overleaf-toolkit/s3.md#s3-setup) 中的说明，在你的 S3 服务上创建三个存储桶 `overleaf-template-files`、`overleaf-project-blobs` 和 `overleaf-chunks` 以及两个受限用户，或者按下文所示在集群中运行 Garage。将四个密钥放入一个 Secret：

```bash wrap theme={null}
kubectl -n overleaf create secret generic overleaf-s3 \
  --from-literal=OVERLEAF_FILESTORE_S3_ACCESS_KEY_ID=<filestore key> \
  --from-literal=OVERLEAF_FILESTORE_S3_SECRET_ACCESS_KEY=<filestore secret> \
  --from-literal=OVERLEAF_HISTORY_S3_ACCESS_KEY_ID=<history key> \
  --from-literal=OVERLEAF_HISTORY_S3_SECRET_ACCESS_KEY=<history secret>
```

**2. ConfigMap**

```yaml title="30-config.yaml (additions)" wrap theme={null}
  OVERLEAF_FILESTORE_BACKEND: s3
  OVERLEAF_FILESTORE_TEMPLATE_FILES_BUCKET_NAME: overleaf-template-files
  OVERLEAF_FILESTORE_S3_REGION: eu-west-1
  OVERLEAF_HISTORY_BACKEND: s3
  OVERLEAF_HISTORY_PROJECT_BLOBS_BUCKET: overleaf-project-blobs
  OVERLEAF_HISTORY_CHUNKS_BUCKET: overleaf-chunks
  OVERLEAF_HISTORY_S3_REGION: eu-west-1
  # a self-hosted S3 service also needs its endpoint and path-style addressing
  # OVERLEAF_FILESTORE_S3_ENDPOINT: http://garage:3900
  # OVERLEAF_FILESTORE_S3_PATH_STYLE: "true"
  # OVERLEAF_HISTORY_S3_ENDPOINT: http://garage:3900
  # OVERLEAF_HISTORY_S3_PATH_STYLE: "true"
```

**3. 将 Secret 添加到 pod 并重启**

在 Deployment（或 StatefulSet）中，将该 Secret 添加到其他 Secret 旁边：

```yaml theme={null}
          envFrom:
            - configMapRef: {name: overleaf-env}
            - secretRef: {name: overleaf-secrets}
            - secretRef: {name: overleaf-s3}
```

```bash theme={null}
kubectl apply -f 30-config.yaml -f 40-overleaf.yaml
kubectl -n overleaf rollout restart deployment/overleaf     # or statefulset/overleaf
```

### 集群中的 Garage

如果没有 S3 服务，[Garage](https://garagehq.deuxfleurs.fr/) 是一个可在集群中运行的小型 S3 服务器。[水平扩展](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/configuration/kubernetes/horizontal-scaling.md) 的配置就是使用它进行测试的。下面的命令会自行生成密钥并创建 Secret `overleaf-s3`，因此请使用它们代替第 1 步中的 Secret 命令。

> [!NOTE]
>
> Overleaf 示例中使用的 MinIO 已不再发布容器镜像：`minio/minio` 已从 Docker Hub 和 quay.io 上消失。

```yaml title="50-garage.yaml" wrap theme={null}
apiVersion: v1
kind: ConfigMap
metadata:
  name: garage-config
  namespace: overleaf
data:
  garage.toml: |
    metadata_dir = "/var/lib/garage/meta"
    data_dir = "/var/lib/garage/data"
    db_engine = "lmdb"
    replication_factor = 1
    rpc_bind_addr = "[::]:3901"
    rpc_public_addr = "127.0.0.1:3901"
    # rpc_secret and admin_token come from GARAGE_RPC_SECRET / GARAGE_ADMIN_TOKEN
    [s3_api]
    s3_region = "garage"          # Overleaf's OVERLEAF_*_S3_REGION must match
    api_bind_addr = "[::]:3900"
    [admin]
    api_bind_addr = "[::]:3903"
---
apiVersion: v1
kind: Service
metadata:
  name: garage
  namespace: overleaf
spec:
  selector:
    app: garage
  ports:
    - name: s3
      port: 3900
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: garage
  namespace: overleaf
spec:
  serviceName: garage
  replicas: 1
  selector:
    matchLabels:
      app: garage
  template:
    metadata:
      labels:
        app: garage
    spec:
      containers:
        - name: garage
          image: dxflrs/garage:v2.5.0
          env:
            - name: GARAGE_RPC_SECRET
              valueFrom: {secretKeyRef: {name: garage-secrets, key: rpc_secret}}
            - name: GARAGE_ADMIN_TOKEN
              valueFrom: {secretKeyRef: {name: garage-secrets, key: admin_token}}
          ports:
            - containerPort: 3900
          readinessProbe:
            tcpSocket:
              port: 3900
          resources:
            requests: {cpu: 50m, memory: 64Mi}
            limits: {memory: 512Mi}
          volumeMounts:
            - {name: config, mountPath: /etc/garage.toml, subPath: garage.toml}
            - {name: data, mountPath: /var/lib/garage}
      volumes:
        - name: config
          configMap:
            name: garage-config
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: [ReadWriteOnce]
        resources:
          requests:
            storage: 10Gi
```

生成密钥、启动 Garage，然后创建布局、存储桶以及两个受限密钥。Garage 的密钥 ID 为 `GK` 加 24 个十六进制字符，密钥为 64 个十六进制字符：

```bash wrap theme={null}
kubectl -n overleaf create secret generic garage-secrets \
  --from-literal=rpc_secret="$(openssl rand -hex 32)" \
  --from-literal=admin_token="$(openssl rand -base64 32)"
kubectl -n overleaf create secret generic overleaf-s3 \
  --from-literal=OVERLEAF_FILESTORE_S3_ACCESS_KEY_ID="GK$(openssl rand -hex 12)" \
  --from-literal=OVERLEAF_FILESTORE_S3_SECRET_ACCESS_KEY="$(openssl rand -hex 32)" \
  --from-literal=OVERLEAF_HISTORY_S3_ACCESS_KEY_ID="GK$(openssl rand -hex 12)" \
  --from-literal=OVERLEAF_HISTORY_S3_SECRET_ACCESS_KEY="$(openssl rand -hex 32)"
kubectl apply -f 50-garage.yaml
kubectl -n overleaf rollout status statefulset/garage

g() { kubectl -n overleaf exec garage-0 -- /garage "$@"; }
s() { kubectl -n overleaf get secret overleaf-s3 -o jsonpath="{.data.$1}" | base64 -d; }
g layout assign -z dc1 -c 8G "$(g status 2>/dev/null | awk '/^[0-9a-f]{16}/{print $1; exit}')"
g layout apply --version 1
for b in overleaf-template-files overleaf-project-blobs overleaf-chunks; do g bucket create "$b"; done
g key import --yes -n overleaf-filestore "$(s OVERLEAF_FILESTORE_S3_ACCESS_KEY_ID)" "$(s OVERLEAF_FILESTORE_S3_SECRET_ACCESS_KEY)"
g key import --yes -n overleaf-history "$(s OVERLEAF_HISTORY_S3_ACCESS_KEY_ID)" "$(s OVERLEAF_HISTORY_S3_SECRET_ACCESS_KEY)"
g bucket allow --read --write overleaf-template-files --key overleaf-filestore
g bucket allow --read overleaf-project-blobs --key overleaf-filestore
g bucket allow --read --write overleaf-project-blobs --key overleaf-history
g bucket allow --read --write overleaf-chunks --key overleaf-history
```

这些权限与 [S3](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/configuration/overleaf-toolkit/s3.md#access-policies) 中的策略一致。Garage 会检查区域：此处为 `garage`，`OVERLEAF_*_S3_REGION` 必须与之相同。

使用 Garage 时，请在 ConfigMap 中为两个服务设置端点、`PATH_STYLE: "true"` 以及区域 `garage`，如 [水平扩展](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/configuration/kubernetes/horizontal-scaling.md) 中的 `60-config.yaml` 所示。

### 多个副本

所有副本必须使用相同的存储桶和密钥。请使用你的 S3 提供商的工具备份存储桶；这样副本自身的磁盘上就不会保存需要备份的数据。

### 在 Kubernetes 上的测试

在 Kubernetes 1.37 上以三个副本并按上述方式使用 Garage 运行时，这些页面中所有创建项目、上传文件、使用历史记录或发布模板的端到端测试全部通过，之后存储桶 `overleaf-project-blobs`、`overleaf-chunks` 和 `overleaf-template-files` 中保存了相应对象，而副本自身的 `/var/lib/overleaf/data/history` 保持为空。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.