0

Create R2 Bucket

by
Published today

Create an R2 bucket in the account, optionally with a location hint.

Script cloudflare Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
2

3
/**
4
 * Create R2 Bucket
5
 * Create an R2 bucket in the account, optionally with a location hint.
6
 */
7
export async function main(
8
  auth: RT.Cloudflare,
9
  name: string,
10
  location_hint: "apac" | "eeur" | "enam" | "weur" | "wnam" | "oc" | undefined
11
) {
12
  const response = await fetch(
13
    `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/r2/buckets`,
14
    {
15
      method: "POST",
16
      headers: {
17
        Authorization: `Bearer ${auth.token}`,
18
        "Content-Type": "application/json",
19
        Accept: "application/json",
20
      },
21
      body: JSON.stringify({ name, locationHint: location_hint ?? undefined }),
22
    }
23
  )
24

25
  if (!response.ok) {
26
    throw new Error(`${response.status} ${await response.text()}`)
27
  }
28

29
  return await response.json()
30
}
31