תיעוד 260
תיעוד ושמירת אירועי העלאה ב-R2 עם התראות אירועים
הדוגמה הזו מספקת מדריך צעד אחר צעד לשימוש בהתראות אירועים כדי ללכוד ולשמור יומני העלאה של R2 בדלי נפרד.
#1. התקנת Wrangler
כדי להתחיל, עיינו בהתקנה או עדכון של Wrangler כדי להתקין את Wrangler, ה-CLI של פלטפורמת המפתחים של Cloudflare.
#2. יצירת דלי R2
תצטרכו ליצור שני דלי 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#3. יצירת תור
התראות אירועים לוכדות שינויים בנתונים ב-example-upload-bucket. תצטרכו ליצור תור חדש כדי לקבל התראות:
npx wrangler queues create example-event-notification-queue#4. יצירת Worker
לפני שתפעילו התראות אירועים עבור example-upload-bucket, עליכם ליצור Worker צרכן כדי לקבל את ההתראות.
צרו Worker חדש עם C3 (ה-CLI של create-cloudflare). C3 הוא כלי שורת פקודה שנועד לעזור לכם להגדיר ולפרוס יישומים חדשים, כולל Workers, ל-Cloudflare.
npm:
npm create cloudflare@latest -- consumer-workeryarn:
yarn create cloudflare consumer-workerpnpm:
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 add an AGENTS.md file to help AI coding tools understand Cloudflare APIs?, בחרו
Yes. - עבור Do you want to deploy your application?, בחרו
No(נבצע כמה שינויים לפני הפריסה).
אחר כך, עברו אל התיקייה החדשה שיצרתם:
cd consumer-worker#5. הגדרת ה-Worker
בקובץ הגדרות Wrangler של פרויקט ה-Worker, הוסיפו צרכן תור וקישור דלי R2. קישורי צרכן התורים ירשמו את ה-Worker שלכם כצרכן של התראות האירועים העתידיות, וקישורי דלי R2 יאפשרו ל-Worker שלכם לגשת לדלי R2 שלכם.
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "event-notification-writer",
"main": "src/index.ts",
"upload_source_maps": true,
// Set this to today's date
"compatibility_date": "2026-10-12",
"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"
upload_source_maps = true
# Set this to today's date
compatibility_date = "2026-10-12"
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
הוסיפו מטפל queue ל-src/index.ts כדי לטפל בכתיבת אצוות של התראות לדלי יעד היומנים שלנו (אין צורך במטפל 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
כדי לפרוס את Worker הצרכן, הריצו את הפקודה wrangler deploy:
npx wrangler deploy#8. הפעלת התראות אירועים
עכשיו, כש-Worker הצרכן מוכן לטפל בהודעות התראות אירועים נכנסות, עליכם להפעיל התראות אירועים עם הפקודה wrangler r2 bucket notification create עבור example-upload-bucket:
npx wrangler r2 bucket notification create example-upload-bucket --event-type object-create --queue example-event-notification-queue#9. בדיקה
עכשיו אפשר לבדוק את הזרימה המלאה מקצה לקצה על ידי העלאת אובייקט ל-example-upload-bucket בלוח הבקרה של Cloudflare. אחרי שהעליתם אובייקט, יומנים יופיעו ב-example-log-sink-bucket תוך כמה שניות.