type Cloudflare = {
token: string;
email: string;
key: string;
};
/**
* List Buckets
* Lists all R2 buckets on your account
*/
export async function main(
auth: Cloudflare,
account_identifier: string,
name_contains: string | undefined,
start_after: string | undefined,
per_page: string | undefined,
order: "name" | undefined,
direction: "asc" | "desc" | undefined,
cursor: string | undefined
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/accounts/${account_identifier}/r2/buckets`
);
for (const [k, v] of [
["name_contains", name_contains],
["start_after", start_after],
["per_page", per_page],
["order", order],
["direction", direction],
["cursor", cursor],
]) {
if (v !== undefined && v !== "") {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
"X-AUTH-EMAIL": auth.email,
"X-AUTH-KEY": auth.key,
Authorization: "Bearer " + auth.token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 186 days ago