Get the newest image from a given family
Stay organized with collections
Save and categorize content based on your preferences.
This sample retrieves the newest image that is part of a given family in a project.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, and Python to retrieve the newest disk image from a specified family within a Google Cloud project.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample utilizes the Compute Engine API client libraries for its respective language and requires setting up Application Default Credentials for authentication.\u003c/p\u003e\n"],["\u003cp\u003eThe primary function demonstrated is \u003ccode\u003egetDiskImageFromFamily\u003c/code\u003e (in Go), \u003ccode\u003egetImageFromFamily\u003c/code\u003e (in Java), and \u003ccode\u003eget_image_from_family\u003c/code\u003e (in Python), which takes a project ID and image family name as input.\u003c/p\u003e\n"],["\u003cp\u003eThe code retrieves and returns the most recent image associated with the specified family, as demonstrated by each language's respective code sample.\u003c/p\u003e\n"],["\u003cp\u003eUsers are provided with links to set up their client library environments, as well as links to the google cloud API reference documentations for the three languages used.\u003c/p\u003e\n"]]],[],null,["# Get the newest image from a given family\n\nThis sample retrieves the newest image that is part of a given family in a project.\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/compute/latest/apiv1).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tcompute \"cloud.google.com/go/compute/apiv1\"\n \tcomputepb \"cloud.google.com/go/compute/apiv1/computepb\"\n )\n\n // Geg a disk image from family for the given project\n func getDiskImageFromFamily(\n \tw io.Writer,\n \tprojectID, family string,\n ) (*computepb.Image, error) {\n \t// projectID := \"your_project_id\"\n \t// family := \"my_family\"\n\n \tctx := context.Background()\n \timagesClient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_ImagesClient_NewImagesRESTClient(ctx)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"NewImagesRESTClient: %w\", err)\n \t}\n \tdefer imagesClient.Close()\n\n \tsource_req := &computepb.GetFromFamilyImageRequest{\n \t\tProject: projectID,\n \t\tFamily: family,\n \t}\n\n \tnewestImage, err := imagesClient.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_ImagesClient_GetFromFamily(ctx, source_req)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"unable to get image: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Newest disk image was found: %s\\n\", *newestImage.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_Operation_Name)\n\n \treturn newestImage, nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Java API\nreference documentation](/java/docs/reference/google-cloud-compute/latest/overview).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.GetFromFamilyImageRequest.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Image.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ImagesClient.html;\n import java.io.IOException;\n\n public class GetImageFromFamily {\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // Project ID or project number of the Google Cloud project you want to use.\n String projectId = \"debian-cloud\";\n // Name of the image family you want to retrieve the image from.\n // List of public operating system (OS) images:\n // https://cloud.google.com/compute/docs/images/os-details\n String family = \"debian-11\";\n\n getImageFromFamily(projectId, family);\n }\n\n // Retrieve the newest image that is part of a given family in a project.\n public static https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Image.html getImageFromFamily(String projectId, String family) throws IOException {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ImagesClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ImagesClient.html.create()) {\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.GetFromFamilyImageRequest.html request = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.GetFromFamilyImageRequest.html.newBuilder()\n .setProject(projectId)\n .setFamily(family)\n .build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Image.html image = client.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ImagesClient.html#com_google_cloud_compute_v1_ImagesClient_getFromFamily_com_google_cloud_compute_v1_GetFromFamilyImageRequest_(request);\n\n System.out.printf(\"Image '%s' has been retrieved successfully\", image.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Image.html#com_google_cloud_compute_v1_Image_getName__());\n\n return image;\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Python API\nreference documentation](/python/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import compute_v1\n\n\n\n def get_image_from_family(project: str, family: str) -\u003e compute_v1.Image:\n \"\"\"\n Retrieve the newest image that is part of a given family in a project.\n\n Args:\n project: project ID or project number of the Cloud project you want to get image from.\n family: name of the image family you want to get image from.\n\n Returns:\n An Image object.\n \"\"\"\n image_client = compute_v1.ImagesClient()\n # List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details\n newest_image = image_client.get_from_family(project=project, family=family)\n return newest_image\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]