# PermissionError: [Errno 13] 权限被拒绝：'/app/data/index_store.json'——LlamaIndex 无法将索引持久化到存储

- **ID:** `llm/llamaindex-persist-dir-permission`
- **领域:** llm
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

LlamaIndex 需要对持久化目录的写权限；在容器或受限环境中运行时，应用程序用户对指定路径缺乏写访问权限。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| llama-index>=0.10.0 | active | — | — |
| llama-index-core>=0.10.0 | active | — | — |

## 解决方案

1. ```
   确保持久化目录存在且应用程序用户可写：mkdir -p /app/data && chown -R appuser:appuser /app/data
   ```
2. ```
   使用具有适当权限的 Docker 卷：docker run -v ./data:/app/data my-app，并在 Dockerfile 中设置用户 ID 以匹配主机用户。
   ```
3. ```
   配置 LlamaIndex 使用其他存储后端（如 S3 或 GCS）以绕过文件系统权限：from llama_index.storage.storage_context import StorageContext; from llama_index.storage.docstore.s3 import S3DocumentStore
   ```

## 无效尝试

- **** — Running as root is a security risk and may not be allowed in production environments with security policies (70% 失败率)
- **** — /tmp is ephemeral and may be cleared on container restart or by system cleanup, losing persisted index data (80% 失败率)
- **** — Disabling persistence means the index must be rebuilt on every restart, increasing latency and API costs (60% 失败率)
