0

Update an Image

by
Published Dec 20, 2024

To update an image, send a `PUT` request to `/v2/images/$IMAGE_ID`. Set the `name` attribute to the new value you would like to use. For custom images, the `description` and `distribution` attributes may also be updated.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Update an Image
7
 * To update an image, send a `PUT` request to `/v2/images/$IMAGE_ID`.
8
Set the `name` attribute to the new value you would like to use.
9
For custom images, the `description` and `distribution` attributes may also be updated.
10

11
 */
12
export async function main(
13
  auth: Digitalocean,
14
  image_id: string,
15
  body: {
16
    name?: string;
17
    distribution?:
18
      | "Arch Linux"
19
      | "CentOS"
20
      | "CoreOS"
21
      | "Debian"
22
      | "Fedora"
23
      | "Fedora Atomic"
24
      | "FreeBSD"
25
      | "Gentoo"
26
      | "openSUSE"
27
      | "RancherOS"
28
      | "Rocky Linux"
29
      | "Ubuntu"
30
      | "Unknown";
31
    description?: string;
32
  },
33
) {
34
  const url = new URL(`https://api.digitalocean.com/v2/images/${image_id}`);
35

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