Skip to content

上手

ocd 必须已经就绪,并且配置了 S3([s3] + r2_prefix)。创建逻辑 bucket,在 open-compute.json 中绑定,再用 oc 运行 Worker。oc run 不会再起一个 workerd。见 ocd 上手配置

1. 创建逻辑 bucket

以下为本平台控制面。不提供 Cloudflare REST / client.v4。对象字节进入配置的 S3 prefix,不进入 SQLite。

sh
ACCOUNT_ID=$(curl -sS http://127.0.0.1:8787/v1/account | python3 -c 'import json,sys; print(json.load(sys.stdin)["accountId"])')
curl -sS -X POST "http://127.0.0.1:8787/v1/accounts/$ACCOUNT_ID/r2/buckets" \
  -H "content-type: application/json" \
  -H "idempotency-key: r2-create-1" \
  -d '{"name":"my-bucket"}'

成功返回 { "resourceId": "...", "state": "ready" }

2. 绑定

json
{
  "name": "r2-app",
  "main": "src/index.ts",
  "bindings": {
    "BUCKET": { "type": "r2_bucket", "id": "<resourceId>" }
  }
}
sh
bun run oc types --config open-compute.json

3. Worker

ts
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    if (request.method === "PUT") {
      await env.BUCKET.put("hello", request.body);
      return new Response("ok");
    }
    const object = await env.BUCKET.get("hello");
    if (!object) return new Response("missing", { status: 404 });
    return new Response(object.body);
  },
} satisfies ExportedHandler<Env>;

4. 运行

sh
bun run oc run --config open-compute.json --ocd <path-to-ocd>

CLI 为 oc,不是 Wrangler。下一步:概念指南