Issue managing r2 buckets for different environments

Hi there,

I’m beginning to use workers and R2 to store data.
My company has 2 environments: dev (staging) and live (production).

So I would like to be able to have, per environment:

  • 1 bucket with a name like: resources-bucket-{dev|live}
  • the binded worker that comes with the bucket: resource-authorizer-{dev|live}

And the preview would use the same resource with a name that ends with -local.

And I would have only one typescript file to execute with a env.RESOURCE_BUCKET variable.

I don’t understand how I can configure my TOML to achieve this.
The notation r2_buckets = [{...}, {...}] seems to not be supported by wrangler.

The r2_buckets object accepts a preview_bucket_name which is used in wrangler dev.

The issue with the currently documented notation is that inline tables must be on their own, single line. See the relevant discussion here: Update TOML formatting on "Configure wrangler.toml" page · Issue #4695 · cloudflare/cloudflare-docs · GitHub

To summarise, use either of these.

r2_buckets  = [
	{ binding = "MY_BUCKET", bucket_name = "BUCKET-PROD", preview_bucket_name =  "BUCKET-LOCAL" }
binding = 'MY_BUCKET'
bucket_name = 'BUCKET-PROD'
preview_bucket_name =  'BUCKET-LOCAL'

When published, env.MY_BUCKET refers to BUCKET-PROD whereas in wrangler dev it will refer to BUCKET-LOCAL.

In hindsight, you may have been asking about Service Environments where you actually have a - in that case, you might just utilise the above TOML under the environments keys in your wrangler.toml.

Haven’t used them personally so I can’t guarantee that’ll be supported.

Thanks for the tip for the inline tables.

I understood the preview thing so that I can have one bucket for “production” and one for the preview.

But I’m looking for a way to have a local bucket when developing -local, a second one when publishing to the dev environment, and a third one when publishing to the live environment.

Is is possible?

Are you using Service Environments like this?


Or the ‘legacy’ environments which published it as an entirely separate Worker?

Service Environments are currently disabled whilst they’re reworked so I’ll assume you have a [] key within your wrangler.toml and then deploy it using wrangler publish --env dev.

You should be able to declare your r2_buckets keys under the [] key as r2_buckets is not inherited.

# stuff

r2_buckets = [
	{ binding = "MY_BUCKET", bucket_name = "BUCKET-PROD" }

r2_buckets = [
	{ binding = "MY_BUCKET", bucket_name = "BUCKET-DEV" }

It seems to be working!
Thanks a lot.
The documentation is not very consistent so it’s hard to find the right info.

1 Like

The wrangler documentation has an overhaul on the way but in the interim, the configuration page has a few issues open to amend the TOML.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.