A template for interfacing with an R2 bucket from within a Cloudflare Worker.
Please refer to the Use R2 from Workers documentation when using this template.
To create a my-project
directory using this template, run:
$ npm init cloudflare my-project worker-r2
# or
$ yarn create cloudflare my-project worker-r2
# or
$ pnpm create cloudflare my-project worker-r2
Note: Each command invokes
create-cloudflare
for project creation.
Run the following commands in the console:
# Next, make sure you've logged in
npx wrangler login
# Create your R2 bucket
npx wrangler r2 bucket create <YOUR_BUCKET_NAME>
# Add config to wrangler.toml as instructed
# Deploy the worker
npx wrangler publish
Then test out your new Worker!
With the default code in this template, every incoming request has the ability to interact with your R2 bucket. This means your bucket is publicly exposed and its contents can be accessed and modified by undesired actors.
You must define authorization logic to determine who can perform what actions to your bucket. To know more about this take a look at the Bucket access and privacy section of the Use R2 from Workers documentation