Récupérer une image
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cet exemple récupère des informations détaillées sur une seule image d'un projet.
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code provides examples in Go, Java, and Python for retrieving detailed information about a specific disk image from a Google Cloud project.\u003c/p\u003e\n"],["\u003cp\u003eThe samples require setting up Application Default Credentials (ADC) for authentication, which can be done by following the provided link to the instructions.\u003c/p\u003e\n"],["\u003cp\u003eEach language-specific example outlines the necessary setup steps, including referencing the Compute Engine quickstart and API documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples are designed to fetch and display information about a specified image, identified by its name and the project it belongs to.\u003c/p\u003e\n"],["\u003cp\u003eThe content directs the user to a documentation that can help in their search for other code samples.\u003c/p\u003e\n"]]],[],null,["# Get an image\n\nThis sample retrieves detailed information about a single image from 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 the given project\n func getDiskImage(\n \tw io.Writer,\n \tprojectID, imageName string,\n ) error {\n \t// projectID := \"your_project_id\"\n \t// imageName := \"my_image\"\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 fmt.Errorf(\"NewImagesRESTClient: %w\", err)\n \t}\n \tdefer imagesClient.Close()\n\n \tsource_req := &computepb.GetImageRequest{\n \t\tProject: projectID,\n \t\tImage: imageName,\n \t}\n\n \timage, err := imagesClient.Get(ctx, source_req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"unable to get image: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Disk image %s was found\\n\", *image.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 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.GetImageRequest.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 GetImage {\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 = \"your-project-id\";\n // Name of the image you want to retrieve.\n String imageName = \"your-image-name\";\n\n getImage(projectId, imageName);\n }\n\n // Retrieve detailed information about a single image from a project\n public static https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Image.html getImage(String projectId, String imageName) 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.GetImageRequest.html request = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.GetImageRequest.html.newBuilder()\n .setProject(projectId)\n .setImage(imageName)\n .build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Image.html image = client.get(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 def get_image(project_id: str, image_name: str) -\u003e compute_v1.Image:\n \"\"\"\n Retrieve detailed information about a single image from a project.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to list images from.\n image_name: name of the image you want to get details of.\n\n Returns:\n An instance of compute_v1.Image object with information about specified image.\n \"\"\"\n image_client = compute_v1.ImagesClient()\n return image_client.get(project=project_id, image=image_name)\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)."]]