Get an image

This sample retrieves detailed information about a single image from a project.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine Go API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 compute 
  
 "cloud.google.com/go/compute/apiv1" 
  
 computepb 
  
 "cloud.google.com/go/compute/apiv1/computepb" 
 ) 
 // Geg a disk image from the given project 
 func 
  
 getDiskImage 
 ( 
  
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 imageName 
  
 string 
 , 
 ) 
  
 error 
  
 { 
  
 // projectID := "your_project_id" 
  
 // imageName := "my_image" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 imagesClient 
 , 
  
 err 
  
 := 
  
 compute 
 . 
  NewImagesRESTClient 
 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "NewImagesRESTClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 imagesClient 
 . 
 Close 
 () 
  
 source_req 
  
 := 
  
& computepb 
 . 
 GetImageRequest 
 { 
  
 Project 
 : 
  
 projectID 
 , 
  
 Image 
 : 
  
 imageName 
 , 
  
 } 
  
 image 
 , 
  
 err 
  
 := 
  
 imagesClient 
 . 
 Get 
 ( 
 ctx 
 , 
  
 source_req 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "unable to get image: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Disk image %s was found\n" 
 , 
  
 * 
 image 
 . 
  Name 
 
 ) 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine Java API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.cloud.compute.v1. GetImageRequest 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. Image 
 
 ; 
 import 
  
 com.google.cloud.compute.v1. ImagesClient 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 GetImage 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to use. 
  
 String 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ; 
  
 // Name of the image you want to retrieve. 
  
 String 
  
 imageName 
  
 = 
  
 "your-image-name" 
 ; 
  
 getImage 
 ( 
 projectId 
 , 
  
 imageName 
 ); 
  
 } 
  
 // Retrieve detailed information about a single image from a project 
  
 public 
  
 static 
  
  Image 
 
  
 getImage 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 imageName 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  ImagesClient 
 
  
 client 
  
 = 
  
  ImagesClient 
 
 . 
 create 
 ()) 
  
 { 
  
  GetImageRequest 
 
  
 request 
  
 = 
  
  GetImageRequest 
 
 . 
 newBuilder 
 () 
  
 . 
 setProject 
 ( 
 projectId 
 ) 
  
 . 
 setImage 
 ( 
 imageName 
 ) 
  
 . 
 build 
 (); 
  
  Image 
 
  
 image 
  
 = 
  
 client 
 . 
 get 
 ( 
 request 
 ); 
  
 System 
 . 
 out 
 . 
 printf 
 ( 
 "Image '%s' has been retrieved successfully" 
 , 
  
 image 
 . 
  getName 
 
 ()); 
  
 return 
  
 image 
 ; 
  
 } 
  
 } 
 } 
 

Python

Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries . For more information, see the Compute Engine Python API reference documentation .

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 compute_v1 
 def 
  
 get_image 
 ( 
 project_id 
 : 
 str 
 , 
 image_name 
 : 
 str 
 ) 
 - 
> compute_v1 
 . 
 Image 
 : 
  
 """ 
 Retrieve detailed information about a single image from a project. 
 Args: 
 project_id: project ID or project number of the Cloud project you want to list images from. 
 image_name: name of the image you want to get details of. 
 Returns: 
 An instance of compute_v1.Image object with information about specified image. 
 """ 
 image_client 
 = 
 compute_v1 
 . 
 ImagesClient 
 () 
 return 
 image_client 
 . 
 get 
 ( 
 project 
 = 
 project_id 
 , 
 image 
 = 
 image_name 
 ) 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: