0

Create a New Block Storage Volume

by
Published Dec 20, 2024

To create a new volume, send a POST request to `/v2/volumes`. Optionally, a `filesystem_type` attribute may be provided in order to automatically format the volume's filesystem. Pre-formatted volumes are automatically mounted when attached to Ubuntu, Debian, Fedora, Fedora Atomic, and CentOS Droplets created on or after April 26, 2018. Attaching pre-formatted volumes to Droplets without support for auto-mounting is not recommended.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 537 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create a New Block Storage Volume
7
 * To create a new volume, send a POST request to `/v2/volumes`. Optionally, a `filesystem_type` attribute may be provided in order to automatically format the volume's filesystem. Pre-formatted volumes are automatically mounted when attached to Ubuntu, Debian, Fedora, Fedora Atomic, and CentOS Droplets created on or after April 26, 2018. Attaching pre-formatted volumes to Droplets without support for auto-mounting is not recommended.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  body: {
12
    id?: string;
13
    droplet_ids?: number[];
14
    name?: string;
15
    description?: string;
16
    size_gigabytes?: number;
17
    created_at?: string;
18
    tags?: string[];
19
  } & { snapshot_id?: string } & { filesystem_type?: string } & {
20
    region:
21
      | "ams1"
22
      | "ams2"
23
      | "ams3"
24
      | "blr1"
25
      | "fra1"
26
      | "lon1"
27
      | "nyc1"
28
      | "nyc2"
29
      | "nyc3"
30
      | "sfo1"
31
      | "sfo2"
32
      | "sfo3"
33
      | "sgp1"
34
      | "tor1"
35
      | "syd1";
36
    filesystem_label?: string & {};
37
  },
38
) {
39
  const url = new URL(`https://api.digitalocean.com/v2/volumes`);
40

41
  const response = await fetch(url, {
42
    method: "POST",
43
    headers: {
44
      "Content-Type": "application/json",
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: JSON.stringify(body),
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55