0

List R2 Buckets

by
Published today

List the R2 buckets in the account. Use the S3 integration with an R2 endpoint to read and write objects.

Script cloudflare Verified

The script

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

3
/**
4
 * List R2 Buckets
5
 * List the R2 buckets in the account. Use the S3 integration with an R2 endpoint to read and write objects.
6
 */
7
export async function main(
8
  auth: RT.Cloudflare,
9
  name_contains: string | undefined
10
) {
11
  const url = new URL(
12
    `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/r2/buckets`
13
  )
14
  if (name_contains != null && name_contains !== "") {
15
    url.searchParams.append("name_contains", name_contains)
16
  }
17

18
  const response = await fetch(url, {
19
    headers: {
20
      Authorization: `Bearer ${auth.token}`,
21
      Accept: "application/json",
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