この例では、イベント通知 を使って R2 のアップロードログを別バケットへ取り込み、保存する手順を説明します。
まず、Wrangler のインストール / 更新 を参照して、Cloudflare Developer Platform CLI の Wrangler をインストールします。
次の 2 つの R2 バケットを作成します。
example-upload-bucket: このバケットへ新しいオブジェクトがアップロードされると、コンシューマー Worker がログを書き込みます。example-log-sink-bucket:example-upload-bucketのアップロードログを、このバケットへ書き込みます。
バケットを作成するには、次の Wrangler コマンドを実行します。
npx wrangler r2 bucket create example-upload-bucket
npx wrangler r2 bucket create example-log-sink-bucketイベント通知は、example-upload-bucket 内のデータの変化を捕捉します。通知を受け取る新しいキューを作成します。
npx wrangler queues create example-event-notification-queueexample-upload-bucket のイベント通知を有効にする前に、通知を受け取る コンシューマー Worker を作成します。
C3(create-cloudflare CLI)で新しい Worker を作成します。C3 は、Workers を含む新しいアプリケーションを Cloudflare へセットアップしてデプロイするためのコマンドラインツールです。
npm create cloudflare@latest -- consumer-workeryarn create cloudflare consumer-workerpnpm create cloudflare@latest consumer-workerセットアップでは、次のオプションを選びます。
- What would you like to start with? では、
Hello World exampleを選びます。 - Which template would you like to use? では、
Worker onlyを選びます。 - Which language do you want to use? では、
TypeScriptを選びます。 - Do you want to use git for version control? では、
Yesを選びます。 - Do you want to deploy your application? では、
Noを選びます(デプロイ前にいくつか変更します)。
作成したディレクトリへ移動します。
cd consumer-workerWorker プロジェクトの [Wrangler 設定ファイル](/workers/wrangler/configuration/) に、キューコンシューマー と R2 バケットバインド を追加します。キューコンシューマーのバインドは、今後のイベント通知のコンシューマーとして Worker を登録します。R2 バケットバインドは、Worker から R2 バケットへアクセスできるようにします。
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "event-notification-writer",
"main": "src/index.ts",
// Set this to today's date
"compatibility_date": "2026-09-20",
"compatibility_flags": [
"nodejs_compat"
],
"queues": {
"consumers": [
{
"queue": "example-event-notification-queue",
"max_batch_size": 100,
"max_batch_timeout": 5
}
]
},
"r2_buckets": [
{
"binding": "LOG_SINK",
"bucket_name": "example-log-sink-bucket"
}
]
}"$schema" = "./node_modules/wrangler/config-schema.json"
name = "event-notification-writer"
main = "src/index.ts"
# Set this to today's date
compatibility_date = "2026-09-20"
compatibility_flags = [ "nodejs_compat" ]
[[queues.consumers]]
queue = "example-event-notification-queue"
max_batch_size = 100
max_batch_timeout = 5
[[r2_buckets]]
binding = "LOG_SINK"
bucket_name = "example-log-sink-bucket"src/index.ts に queue ハンドラー を追加し、通知のバッチをログシンクバケットへ書き込みます(fetch ハンドラー は不要です)。
export interface Env {
LOG_SINK: R2Bucket;
}
export default {
async queue(batch, env): Promise<void> {
const batchId = new Date().toISOString().replace(/[:.]/g, "-");
const fileName = `upload-logs-${batchId}.json`;
// Serialize the entire batch of messages to JSON
const fileContent = new TextEncoder().encode(
JSON.stringify(batch.messages),
);
// Write the batch of messages to R2
await env.LOG_SINK.put(fileName, fileContent, {
httpMetadata: {
contentType: "application/json",
},
});
},
} satisfies ExportedHandler<Env>;コンシューマー Worker をデプロイするには、wrangler deploy コマンドを実行します。
npx wrangler deployコンシューマー Worker が受信するイベント通知メッセージを処理できるようになったので、example-upload-bucket に対して wrangler r2 bucket notification create コマンド でイベント通知を有効にします。
npx wrangler r2 bucket notification create example-upload-bucket --event-type object-create --queue example-event-notification-queueCloudflare ダッシュボードで example-upload-bucket へオブジェクトをアップロードし、エンドツーエンドの流れを確認できます。オブジェクトをアップロードすると、数秒後に example-log-sink-bucket へログが現れます。