0

List All Block Storage Volumes

by
Published Dec 20, 2024

To list all of the block storage volumes available on your account, send a GET request to `/v2/volumes`.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * List All Block Storage Volumes
7
 * To list all of the block storage volumes available on your account, send a GET request to `/v2/volumes`.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  name: string | undefined,
12
  region:
13
    | "ams1"
14
    | "ams2"
15
    | "ams3"
16
    | "blr1"
17
    | "fra1"
18
    | "lon1"
19
    | "nyc1"
20
    | "nyc2"
21
    | "nyc3"
22
    | "sfo1"
23
    | "sfo2"
24
    | "sfo3"
25
    | "sgp1"
26
    | "tor1"
27
    | "syd1"
28
    | undefined,
29
  per_page: string | undefined,
30
  page: string | undefined,
31
) {
32
  const url = new URL(`https://api.digitalocean.com/v2/volumes`);
33
  for (const [k, v] of [
34
    ["name", name],
35
    ["region", region],
36
    ["per_page", per_page],
37
    ["page", page],
38
  ]) {
39
    if (v !== undefined && v !== "" && k !== undefined) {
40
      url.searchParams.append(k, v);
41
    }
42
  }
43
  const response = await fetch(url, {
44
    method: "GET",
45
    headers: {
46
      Authorization: "Bearer " + auth.token,
47
    },
48
    body: undefined,
49
  });
50
  if (!response.ok) {
51
    const text = await response.text();
52
    throw new Error(`${response.status} ${text}`);
53
  }
54
  return await response.json();
55
}
56