Snapshot abrufen

In diesem Beispiel werden Informationen zu einem Snapshot abgerufen.

Codebeispiel

Go

Bevor Sie dieses Beispiel anwenden, folgen Sie den Schritten zur Einrichtung von Go in der Compute Engine-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Informationen finden Sie in der Referenzdokumentation zur Compute Engine Go API.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Compute Engine zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
)

// getSnapshot prints a name of a disk snapshot in the specified project.
func getSnapshot(w io.Writer, projectID, snapshotName string) error {
	// projectID := "your_project_id"
	// snapshotName := "your_snapshot_name"

	ctx := context.Background()
	snapshotsClient, err := compute.NewSnapshotsRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewSnapshotsRESTClient: %w", err)
	}
	defer snapshotsClient.Close()

	reqSnapshot := &computepb.GetSnapshotRequest{
		Project:  projectID,
		Snapshot: snapshotName,
	}

	snapshot, err := snapshotsClient.Get(ctx, reqSnapshot)
	if err != nil {
		return fmt.Errorf("unable to get snapshot: %w", err)
	}

	fmt.Fprintf(w, "Found snapshot: %s\n", snapshot.GetName())

	return nil
}

Java

Bevor Sie dieses Beispiel anwenden, folgen Sie den Schritten zur Einrichtung von Java in der Compute Engine-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Informationen finden Sie in der Referenzdokumentation zur Compute Engine Java API.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Compute Engine zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.


package compute.disks;

import com.google.cloud.compute.v1.Snapshot;
import com.google.cloud.compute.v1.SnapshotsClient;
import java.io.IOException;

public class GetSnapshot {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Name of the snapshot to look up.
    String snapshotName = "YOUR_SNAPSHOT_NAME";

    getSnapshot(projectId, snapshotName);
  }

  // Get information about a snapshot.
  public static void getSnapshot(String projectId, String snapshotName) 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. After completing all of your requests, call
    // the `snapshotsClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (SnapshotsClient snapshotsClient = SnapshotsClient.create()) {
      Snapshot snapshot = snapshotsClient.get(projectId, snapshotName);
      System.out.printf("Retrieved the snapshot: %s", snapshot.getName());
    }
  }
}

Python

Bevor Sie dieses Beispiel anwenden, folgen Sie den Schritten zur Einrichtung von Python in der Compute Engine-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Informationen finden Sie in der Referenzdokumentation zur Compute Engine Python API.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Compute Engine zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

from google.cloud import compute_v1

def get_snapshot(project_id: str, snapshot_name: str) -> compute_v1.Snapshot:
    """
    Get information about a Snapshot.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        snapshot_name: the name of the snapshot you want to look up.

    Returns:
        A Snapshot object.
    """

    snapshot_client = compute_v1.SnapshotsClient()

    return snapshot_client.get(project=project_id, snapshot=snapshot_name)

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.