How can I access R2 buckets binding when binding multiple buckets

Hi I am currently trying to bind 2 R2 buckets to my worker. When I do it like this it works for one bucket:
[[r2_buckets]]
binding = 'Bucket1'
bucket_name = 'bucket1'
[[r2_buckets]]
binding = 'Bucket1'
bucket_name = 'bucket1'
However when binding like this I cant access the bindings in the worker:
r2_buckets = [
{ binding = "Bucket1", bucket_name = "bucket1", preview_bucket_name = "bucket1" },
{ binding = "Bucket2", bucket_name = "bucket2", preview_bucket_name = "bucket2" }
]
r2_buckets = [
{ binding = "Bucket1", bucket_name = "bucket1", preview_bucket_name = "bucket1" },
{ binding = "Bucket2", bucket_name = "bucket2", preview_bucket_name = "bucket2" }
]
I have tried accessing the bucket via
env.Bucket1
env.Bucket1
(this is undefined) or
env.r2_buckets[0]
env.r2_buckets[0]
(this is just an Object not a bucket and has no .get(), ...) What am I doing wrong?
11 Replies
Hello, I’m Allie!
What does your entire toml look like? Sometimes there are some really weird inheritance issues...
bot Lx
bot LxOP15mo ago
not much there:
name = "storage"
main = "src/index.ts"
compatibility_date = "2023-09-18"
account_id = "xxx"

routes = [
{ pattern = "xxx", zone_name = "xxx" }
]
workers_dev = false

[vars]
SUPABASE_URL = "xxx"
SUPABASE_ANON_KEY = "xxx"

r2_buckets = [
{ binding = "Bucket1", bucket_name = "bucket1", preview_bucket_name = "bucket1" },
{ binding = "Bucket2", bucket_name = "bucket2", preview_bucket_name = "bucket2" }
]
name = "storage"
main = "src/index.ts"
compatibility_date = "2023-09-18"
account_id = "xxx"

routes = [
{ pattern = "xxx", zone_name = "xxx" }
]
workers_dev = false

[vars]
SUPABASE_URL = "xxx"
SUPABASE_ANON_KEY = "xxx"

r2_buckets = [
{ binding = "Bucket1", bucket_name = "bucket1", preview_bucket_name = "bucket1" },
{ binding = "Bucket2", bucket_name = "bucket2", preview_bucket_name = "bucket2" }
]
Hello, I’m Allie!
Sorry Sent too fast
bot Lx
bot LxOP15mo ago
ah wait i see it now i think
bot Lx
bot LxOP15mo ago
its putting it in vars isnt it?
Hello, I’m Allie!
Yes
bot Lx
bot LxOP15mo ago
hehe thanks for the help
Hello, I’m Allie!
Or if you want to avoid the mess of inheritance of TOML, take wrangler.json out for a spin!
bot Lx
bot LxOP15mo ago
will do!
Hello, I’m Allie!
Just convert the above to JSON, and then use the -j flag to make sure wrangler picks it up(i.e. wrangler dev -j).
Want results from more Discord servers?
Add your server