An example Cloudflare Worker that acts as an on-publish hook for Pub/Sub and copies a subset of messages to R2 object storage.
To deploy this to your own account:
- Update
account_id
inwrangler.toml
to use your own account ID - Update the
[[r2_buckets]]
configuration inwrangler.toml
to reference an R2 bucket that exists in your account (see the R2 docs for more details) - Update the
BROKER_PUBLIC_KEYS
environmental variable with the public key set from your own Broker — the.../pubsub/namespaces/{namespace}/brokers/{broker}/publickeys
endpoint in the API will return your broker-specific keys (docs) - Publish it with
wrangler publish
With the Worker deployed, you can then update your Broker configuration to use the Worker as an on-publish hook by setting the on_publish.url
field to the URL of your Worker (see the docs).
BSD-3-Clause licensed. Copyright Cloudflare, Inc, 2022.