Log and store upload events in R2 with event notifications
This example provides a step-by-step guide on using event notifications to capture and store R2 upload logs in a separate bucket.
Prerequisites
To continue, you will need:
- A subscription to Workers Paid, required for using queues.
1. Install Wrangler
To begin, refer to Install/Update Wrangler to install Wrangler, the Cloudflare Developer Platform CLI.
2. Create R2 buckets
You will need to create two R2 buckets:
example-upload-bucket
: When new objects are uploaded to this bucket, your consumer Worker will write logs.example-log-sink-bucket
: Upload logs fromexample-upload-bucket
will be written to this bucket.
To create the buckets, run the following Wrangler commands:
3. Create a queue
Event notifications capture changes to data in example-upload-bucket
. You will need to create a new queue to receive notifications:
4. Create a Worker
Before you enable event notifications for example-upload-bucket
, you need to create a consumer Worker to receive the notifications.
Create a new Worker with C3 (create-cloudflare
CLI). C3 is a command-line tool designed to help you set up and deploy new applications, including Workers, to Cloudflare.
C3 will then prompt you for some information on your Worker.
- Provide a name for your consumer Worker. This is also the name of the new directory where the Worker will be created.
- For the question “What type of application do you want to create?”, select “Hello World” Worker.
- For the question “Would you like to use TypeScript? (y/n)”, select y.
- For the question “Do you want to deploy your application?”, select n. This will create your Worker.
5. Configure your Worker
In your Worker project’s wrangler.toml
file, add a queue consumer and R2 bucket binding. The queues consumer bindings will register your Worker as a consumer of your future event notifications and the R2 bucket bindings will allow your Worker to access your R2 bucket.
6. Write event notification messages to R2
Add a queue
handler to src/index.ts
to handle writing batches of notifications to our log sink bucket (you do not need a fetch handler):
7. Deploy your Worker
To deploy your consumer Worker, run the wrangler deploy
command:
8. Enable event notifications
Now that you have your consumer Worker ready to handle incoming event notification messages, you need to enable event notifications with the wrangler r2 bucket notification create
command for example-upload-bucket
:
9. Test
Now you can test the full end-to-end flow by uploading an object to example-upload-bucket
in the Cloudflare dashboard. After you have uploaded an object, logs will appear in example-log-sink-bucket
in a few seconds.