> ## 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-TW/on-premises/configuration/overleaf-toolkit/s3.md)。

| | |
| - | - |
| 服務 | history-v1（專案檔案與歷史記錄）、filestore（範本檔案），位於每個 Ayakaleaf Pro Pod 中 |
| Pod → S3 | 每次讀取或寫入檔案時，連線至 S3 端點 |
| 必要時機 | 用於[多個副本](https://ayakaleaf-pro.mintlify.site/zh-TW/on-premises/configuration/kubernetes/horizontal-scaling.md)：各副本的資料磁碟區不共用，但儲存貯體是共用的 |
| 定期工作 | 無 |

> [!WARNING]
>
> 請在使用者建立專案之前選擇 S3。已將檔案保存在磁碟區上的執行個體，需要先進行 [S3 遷移](https://ayakaleaf-pro.mintlify.site/zh-TW/on-premises/maintenance/s3-migration.md)。

### 設定

**1. 儲存貯體與金鑰**

請依照 [S3 設定](https://ayakaleaf-pro.mintlify.site/zh-TW/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 加在其他項目旁邊：

```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-TW/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，然後建立配置（layout）、儲存貯體與兩組受限金鑰。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-TW/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-TW/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.