跳转到正文

自定义存储

Capgo 支持为你的 app bundle 使用自定义存储方案,让你可以把更新托管在自有基础设施或第三方存储服务上。对于有特定安全要求、合规需求或现有存储基础设施的组织来说,这尤其有用。

Capgo 的自定义存储工作原理是:将你的 bundle 上传到外部位置,并把访问该位置的 URL 提供给 Capgo。随后,Capgo SDK 会直接从你的自定义存储位置下载更新,而不是使用 Capgo 的默认云存储。

使用自定义存储最简单的方法是将 bundle 上传到任何可公开访问的 URL,并将该 URL 提供给 Capgo。

终端窗口
npx @capgo/cli@latest bundle upload --external https://your-domain.com/bundles/v1.2.3.zip

该命令让 Capgo 引用指定 URL 上的 bundle,而不是将其上传到 Capgo 的云存储。

若要使用安全的外部存储,您可以加密 bundle 并提供解密密钥:

终端窗口
npx @capgo/cli@latest bundle upload --external https://your-domain.com/bundles/v1.2.3.zip --iv-session-key YOUR_IV_SESSION_KEY

Capgo 内置支持 Amazon S3 和兼容 S3 的存储服务。CLI 可自动将您的 bundle 上传到 S3,并将 Capgo 配置为使用 S3 URL。

终端窗口
npx @capgo/cli@latest bundle upload \
--s3-region us-east-1 \
--s3-apikey YOUR_ACCESS_KEY \
--s3-apisecret YOUR_SECRET_KEY \
--s3-bucket-name your-bucket-name

完整的 S3 配置

“完成 S3 配置”章节

针对兼容 S3 的服务或自定义端点:

终端窗口
npx @capgo/cli@latest bundle upload \
--s3-region us-east-1 \
--s3-apikey YOUR_ACCESS_KEY \
--s3-apisecret YOUR_SECRET_KEY \
--s3-endpoint https://s3.your-provider.com \
--s3-bucket-name your-bucket-name \
--s3-port 443 \
--no-s3-ssl # Only if your endpoint doesn't support SSL
参数描述必填
--s3-regionS3 bucket 所在的 AWS region是
--s3-apikeyS3 访问密钥 ID是
--s3-apisecretS3 秘密访问密钥是
--s3-bucket-name您的 S3 存储桶名称是
--s3-endpoint自定义 S3 端点 URL否
--s3-portS3 端点端口否
--no-s3-ssl禁用 S3 上传的 SSL否

使用自定义存储(尤其是加密时),需要正确准备 bundle。这包括创建 zip 文件,并可选择对其进行加密。

首先,创建应用 bundle 的 zip 文件:

终端窗口
npx @capgo/cli@latest bundle zip com.example.app --path ./dist

zip 命令会返回 zip 文件的校验和。如需加密 zip 文件,可使用该校验和。使用 --json 获取包含校验和的结构化输出的选项。

终端窗口
npx @capgo/cli@latest bundle zip [appId] \
--path ./dist \
--bundle 1.2.3 \
--name myapp-v1.2.3 \
--json \
--no-code-check \
--key-v2 \
--package-json ../../package.json,./package.json
选项描述
--path要压缩的文件夹路径(默认为 capacitor.config 中的 webDir)
--bundle用于命名 zip 文件的 Bundle 版本号
--namezip 文件的自定义名称
--json以 JSON 格式输出结果(包含校验和)
--no-code-check跳过对 notifyAppReady() 调用和 index 文件的检查
--key-v2使用 v2 加密
--package-jsonmonorepo 中 package.json 文件的路径(逗号分隔)

步骤 2:加密 Bundle(可选)

“步骤 2:加密 Bundle(可选)”章节

为提升安全性,请在上传前加密 zip bundle:

终端窗口
# Using default local key
npx @capgo/cli@latest bundle encrypt ./myapp.zip CHECKSUM
# Using custom key file
npx @capgo/cli@latest bundle encrypt ./myapp.zip CHECKSUM --key ./path/to/.capgo_key_v2
# Using key data directly
npx @capgo/cli@latest bundle encrypt ./myapp.zip CHECKSUM --key-data "PRIVATE_KEY_CONTENT"

本指南中介绍的 CHECKSUM 该参数为必填项,应为 zip 文件的校验和。你可以从 zip 命令的输出中获取校验和(使用 --json 选项获取结构化输出)。

默认情况下,encrypt 命令会使用你本地的私有签名密钥。你可以使用 --key 或 --key-data 选项来指定自定义密钥。

encrypt 命令会返回 ivSessionKey 上传或解密所需的内容。

选项描述
zipPath待加密 zip 文件的路径 (必填)
checksumzip 文件的校验和 (必填) - 通过 zip 命令获取
--key签名私钥的自定义路径(可选,默认使用本地密钥)
--key-data直接传入签名私钥数据(可选)
--json以 JSON 格式输出结果

示例 1:带加密的外部 URL

“示例 1:带加密的外部 URL”章节
  1. 构建应用:

    终端窗口
    npm run build
  2. 创建一个 zip bundle:

    终端窗口
    npx @capgo/cli@latest bundle zip com.example.app --path ./dist --bundle 1.2.3

    记下此命令返回的校验和。

  3. 加密该 bundle:

    终端窗口
    npx @capgo/cli@latest bundle encrypt ./com.example.app-1.2.3.zip CHECKSUM_FROM_STEP_2

    请记下 ivSessionKey 从输出中获取。

  4. 上传到你的存储: 将加密的 zip 文件上传到你的托管服务。

  5. 使用 Capgo 注册:

    终端窗口
    npx @capgo/cli@latest bundle upload \
    --external https://your-cdn.com/bundles/com.example.app-1.2.3.zip \
    --iv-session-key IV_SESSION_KEY_FROM_STEP_3
  1. 构建应用:

    终端窗口
    npm run build
  2. 直接上传到 S3:

    终端窗口
    npx @capgo/cli@latest bundle upload \
    --s3-region us-west-2 \
    --s3-apikey YOUR_ACCESS_KEY \
    --s3-apisecret YOUR_SECRET_KEY \
    --s3-bucket-name your-app-bundles \
    --channel Production
  1. 构建并压缩:

    终端窗口
    npm run build
    npx @capgo/cli@latest bundle zip com.example.app --path ./dist --key-v2
  2. 加密该 bundle:

    终端窗口
    npx @capgo/cli@latest bundle encrypt ./com.example.app.zip CHECKSUM
  3. 使用加密上传到 S3:

    终端窗口
    npx @capgo/cli@latest bundle upload \
    --s3-region us-west-2 \
    --s3-apikey YOUR_ACCESS_KEY \
    --s3-apisecret YOUR_SECRET_KEY \
    --s3-bucket-name your-app-bundles \
    --iv-session-key IV_SESSION_KEY_FROM_STEP_2 \
    --channel Production

使用自定义存储时,请考虑以下安全最佳实践:

  • 确保存储 URL 可被应用用户访问,但无法被公开发现
  • 尽可能使用签名 URL 或基于 token 的身份验证
  • 为基于 Web 的应用实现正确的 CORS 头
  • 始终使用 Capgo 加密工具对敏感 bundle 进行加密
  • 安全存储加密密钥并定期轮换
  • 所有 bundle URL 均使用 HTTPS(移动和 Electron 应用必需)
  • 监控访问日志以检测异常的下载模式
  • 为 bundle 下载失败设置告警
  • 定期审计存储权限

Bundle 无法下载:

  • 确认 URL 可公开访问且使用 HTTPS(移动应用和 Electron 应用必需)
  • 检查 Web 应用的 CORS 头
  • 确保 bundle 格式正确

加密错误:

  • 请检查 ivSessionKey 与加密 bundle 匹配
  • 检查 bundle 是否已使用正确的密钥加密
  • 确保新 bundle 使用 encryption v2

S3 upload 失败:

  • 验证 S3 凭据和权限
  • 检查 bucket 策略和 CORS 配置
  • 确保指定的 region 正确

检查 bundle 状态:

终端窗口
npx @capgo/cli@latest app debug

验证 bundle 完整性:

终端窗口
npx @capgo/cli@latest bundle list
  • 了解 通道 以管理不同的部署环境
  • 探索 更新行为 自定义更新的应用方式
  • 设置 CI/CD 集成 以自动化您的自定义存储工作流

从自定义存储继续

“从自定义存储继续”部分

如果你正在使用 自定义存储 来规划安全与合规,请结合 加密 了解 Encryption 的实现细节, 合规性 了解合规部分的实现细节, Capgo 安全扫描器 了解 Capgo Security Scanner 的产品工作流, Capgo 安全 用于 Capgo 安全的产品工作流,以及 Capgo 信任中心 用于 Capgo 信任中心的产品工作流。