Skip to content

R2BucketEventNotification

Source: src/Cloudflare/R2/BucketEventNotification.ts

Event notifications for a Cloudflare R2 bucket, delivered to a Queue.

When objects in the bucket are created, deleted, or copied, R2 publishes an event message to the configured Queue. One configuration exists per (bucket, queue) pair and holds a list of rules; consume the messages with a Queue consumer Worker.

The configuration’s identity is the (bucket, queue) pair — changing either triggers a replacement, while rule changes are applied in place (the provider converges the pair’s configuration to exactly the declared rule set).

Notify on every upload and delete

const bucket = yield* Cloudflare.R2Bucket("Uploads");
const queue = yield* Cloudflare.Queue("UploadEvents");
yield* Cloudflare.R2BucketEventNotification("UploadNotifications", {
bucketName: bucket.bucketName,
queueId: queue.queueId,
rules: [
{
actions: ["PutObject", "CompleteMultipartUpload", "DeleteObject"],
},
],
});

Scope notifications to a key prefix and suffix

yield* Cloudflare.R2BucketEventNotification("ImageNotifications", {
bucketName: bucket.bucketName,
queueId: queue.queueId,
rules: [
{
actions: ["PutObject"],
prefix: "images/",
suffix: ".png",
description: "new PNG images",
},
],
});
// Rules must cover non-overlapping key ranges — Cloudflare rejects
// overlapping prefixes/suffixes even when the actions are disjoint.
yield* Cloudflare.R2BucketEventNotification("Notifications", {
bucketName: bucket.bucketName,
queueId: queue.queueId,
rules: [
{ actions: ["PutObject"], prefix: "incoming/" },
{ actions: ["DeleteObject", "LifecycleDeletion"], prefix: "logs/" },
],
});