{
  "openapi": "3.1.0",
  "info": {
    "title": "Yaoxi R2 Unified Gateway API",
    "version": "1.1.0",
    "description": "多 Cloudflare R2 账号串联虚拟存储池对外服务接口与 EdgeOne 对象分发网关。保留 100 MB 安全空间，超过 9.9 GB 自动锁定写入并切换下一可用账号，空出后自动恢复。"
  },
  "servers": [
    {
      "url": "https://r2.yaoxi.wiki",
      "description": "统一对外服务入口（EdgeOne 加速）"
    }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "r2k_...",
        "description": "在管理台创建的对外 API Key（Authorization: Bearer r2k_...）"
      }
    }
  },
  "paths": {
    "/healthz": {
      "get": {
        "summary": "健康检查",
        "responses": {
          "200": {
            "description": "服务正常"
          }
        }
      }
    },
    "/o/{pool}/{logicalKey}": {
      "get": {
        "summary": "统一对象读取（支持 Range/206、ETag/304 与 EdgeOne 缓存）",
        "description": "若存储池已开启「公开读取」，支持匿名访问并由 EdgeOne 缓存加速；否则需携带 Authorization: Bearer API Key。",
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "default": "default"
            }
          },
          {
            "name": "logicalKey",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "返回完整对象流"
          },
          "206": {
            "description": "返回 Range 分片内容"
          },
          "304": {
            "description": "ETag 命中未修改"
          },
          "401": {
            "description": "私有池缺少或无效 API Key"
          },
          "404": {
            "description": "逻辑对象不存在"
          }
        }
      }
    },
    "/f/{logicalKey}": {
      "get": {
        "summary": "默认存储池（default）对象读取简写",
        "parameters": [
          {
            "name": "logicalKey",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "等价于 /o/default/{logicalKey}"
          }
        }
      }
    },
    "/v1/pools": {
      "get": {
        "summary": "列出已授权的虚拟存储池总览与安全可写余量",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "返回存储池数组"
          }
        }
      }
    },
    "/v1/pools/{pool}": {
      "get": {
        "summary": "查看单个存储池状态、当前主写账号与链路顺序",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "返回存储池详情"
          }
        }
      }
    },
    "/v1/pools/{pool}/pick": {
      "post": {
        "summary": "模拟写入选号（不签发链接、不写入索引）",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "logicalKey": {
                    "type": "string"
                  },
                  "sizeBytes": {
                    "type": "integer",
                    "minimum": 0
                  },
                  "refresh": {
                    "type": "boolean"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "返回命中的账号与跳过列表"
          },
          "423": {
            "description": "池内账号已触发 9.9 GB 写锁"
          }
        }
      }
    },
    "/v1/pools/{pool}/put-url": {
      "post": {
        "summary": "自动选号并签发 5 分钟 PUT 预签名上传链接，登记逻辑键路由索引",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "logicalKey"
                ],
                "properties": {
                  "logicalKey": {
                    "type": "string",
                    "example": "media/2026/demo.webp"
                  },
                  "sizeBytes": {
                    "type": "integer",
                    "minimum": 0,
                    "example": 245760
                  },
                  "contentType": {
                    "type": "string",
                    "example": "image/webp"
                  },
                  "mediaType": {
                    "type": "string",
                    "enum": [
                      "image",
                      "video",
                      "audio",
                      "document",
                      "other"
                    ]
                  },
                  "sha256": {
                    "type": "string",
                    "description": "64 位十六进制 SHA-256 哈希，用于去重与完整性校验"
                  },
                  "width": {
                    "type": "integer",
                    "minimum": 0
                  },
                  "height": {
                    "type": "integer",
                    "minimum": 0
                  },
                  "durationMs": {
                    "type": "integer",
                    "minimum": 0
                  },
                  "tags": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    },
                    "example": [
                      "wallpaper",
                      "4k"
                    ]
                  },
                  "metadata": {
                    "type": "object",
                    "description": "自定义媒体元数据 JSON（<= 4 KB）"
                  },
                  "dedupe": {
                    "type": "boolean",
                    "default": false,
                    "description": "为 true 且提供 sha256 时命中同池已有文件直接复用"
                  },
                  "refresh": {
                    "type": "boolean",
                    "default": false
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "返回 5 分钟 PUT 预签名链接、目标账号/桶及统一对外访问地址 publicUrl"
          },
          "423": {
            "description": "所有启用账号已达到 9.9 GB 写锁线"
          },
          "507": {
            "description": "存储池无足够安全可写余量"
          }
        }
      }
    },
    "/v1/pools/{pool}/get-url": {
      "post": {
        "summary": "按逻辑键自动寻址并签发 5 分钟直连 R2 的 GET 预签名链接",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "logicalKey"
                ],
                "properties": {
                  "logicalKey": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "返回 5 分钟 GET 预签名链接及 publicUrl"
          },
          "404": {
            "description": "逻辑对象不存在"
          }
        }
      }
    },
    "/v1/pools/{pool}/objects": {
      "get": {
        "summary": "媒体库多维索引查询（支持前缀、媒体类型、标签、关键词、SHA-256、账号筛选与排序）",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "prefix",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "mediaType",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "image",
                "video",
                "audio",
                "document",
                "other"
              ]
            }
          },
          {
            "name": "tag",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "q",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "sha256",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "alias",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "sort",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "updated_desc",
                "updated_asc",
                "size_desc",
                "key_asc"
              ]
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 50,
              "maximum": 200
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "分页逻辑对象列表（含元数据、标签与统一访问地址 publicUrl）"
          }
        }
      },
      "delete": {
        "summary": "按逻辑键从物理 R2 桶删除对象并移除索引（锁写账号亦可删）",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "logicalKey",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "删除成功"
          },
          "404": {
            "description": "逻辑对象不存在"
          }
        }
      }
    },
    "/v1/pools/{pool}/objects/meta": {
      "get": {
        "summary": "查询单个逻辑对象的完整索引元数据（不请求 R2）",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "logicalKey",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "返回完整索引记录与 publicUrl"
          }
        }
      },
      "patch": {
        "summary": "更新已索引对象的标签、宽高、时长、哈希或自定义 metadata（无需重传文件）",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "返回更新后的索引记录"
          }
        }
      }
    },
    "/v1/pools/{pool}/objects/batch": {
      "post": {
        "summary": "批量解析最多 100 个 logicalKeys 或 sha256s（适合媒体库画廊/播放列表一次渲染）",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "返回 items 与 missingKeys"
          }
        }
      }
    },
    "/v1/pools/{pool}/objects/index": {
      "post": {
        "summary": "手动登记/导入已有 R2 对象至逻辑索引（适合存量媒体库迁移）",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "201": {
            "description": "返回已登记的索引记录"
          }
        }
      }
    },
    "/v1/pools/{pool}/index/stats": {
      "get": {
        "summary": "查看媒体库索引聚合统计（按 mediaType 分布、按账号分布与 Top 50 标签）",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "pool",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "返回统计聚合结果"
          }
        }
      }
    }
  }
}