이벤트 알림으로 R2에 업로드 이벤트 기록·저장 (Log and store upload events in R2 with event notifications)
이 예시는 이벤트 알림을 사용해 R2 업로드 로그를 별도의 버킷에 포착하고 저장하는 방법을 단계별로 안내해요.
출처: 문서
본문
1. Wrangler 설치
시작하려면 Cloudflare Developer Platform CLI인 Wrangler를 설치하기 위해 Install/Update Wrangler를 참고해 주세요.
2. R2 버킷 만들기
두 개의 R2 버킷을 만들어야 해요:
example-upload-bucket: 새 객체가 이 버킷에 업로드되면 consumer 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
3. 큐(Queue) 만들기
이벤트 알림은 example-upload-bucket의 데이터 변경을 포착해요. 알림을 받을 새 큐를 만들어야 해요:
npx wrangler queues create example-event-notification-queue
4. Worker 만들기
example-upload-bucket에 대한 이벤트 알림을 활성화하기 전에 알림을 받을 consumer Worker를 만들어야 해요.
C3(create-cloudflare CLI)로 새 Worker를 만들어요. C3는 Workers를 포함한 새 애플리케이션을 Cloudflare에 설정하고 배포하는 데 도움을 주는 명령줄 도구예요.
npm create cloudflare@latest -- consumer-worker
yarn create cloudflare consumer-worker
pnpm 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-worker
5. Worker 구성
Worker 프로젝트의 Wrangler 구성 파일에 큐 consumer와 R2 버킷 바인딩을 추가해요. queues consumer 바인딩은 Worker를 향후 이벤트 알림의 consumer로 등록하고, 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-10-07",
"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-10-07"
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"
6. 이벤트 알림 메시지를 R2에 기록
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>;
7. Worker 배포
consumer Worker를 배포하려면 wrangler deploy 명령을 실행해요:
npx wrangler deploy
8. 이벤트 알림 활성화
이제 consumer 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-queue
9. 테스트
이제 Cloudflare 대시보드에서 example-upload-bucket에 객체를 업로드해 전체 end-to-end 흐름을 테스트할 수 있어요. 객체를 업로드하면 몇 초 안에 example-log-sink-bucket에 로그가 나타나요.
더 알아보기 (Learn more)
- R2 이벤트 알림 — 버킷 데이터 변경 감지
- Cloudflare Queues 시작하기 — 큐와 consumer Worker 만들기
- Wrangler R2 명령 — 버킷 및 알림 관리