Snapshot abrufen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Beispiel werden Informationen zu einem Snapshot abgerufen.
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis webpage provides code samples in Go, Java, and Python for retrieving information about a specific Compute Engine snapshot.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples demonstrate how to authenticate using Application Default Credentials and set up the necessary client libraries.\u003c/p\u003e\n"],["\u003cp\u003eEach sample requires the project ID and the snapshot name to function correctly, as the content references.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples provided utilize the Compute Engine APIs and client libraries for their respective language.\u003c/p\u003e\n"]]],[],null,["# Get a snapshot\n\nThis sample retrieves information about a snapshot.\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 // getSnapshot prints a name of a disk snapshot in the specified project.\n func getSnapshot(w io.Writer, projectID, snapshotName string) error {\n \t// projectID := \"your_project_id\"\n \t// snapshotName := \"your_snapshot_name\"\n\n \tctx := context.Background()\n \tsnapshotsClient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_SnapshotsClient_NewSnapshotsRESTClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewSnapshotsRESTClient: %w\", err)\n \t}\n \tdefer snapshotsClient.Close()\n\n \treqSnapshot := &computepb.GetSnapshotRequest{\n \t\tProject: projectID,\n \t\tSnapshot: snapshotName,\n \t}\n\n \tsnapshot, err := snapshotsClient.Get(ctx, reqSnapshot)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"unable to get snapshot: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Found snapshot: %s\\n\", snapshot.GetName())\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 package compute.disks;\n\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Snapshot.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.SnapshotsClient.html;\n import java.io.IOException;\n\n public class GetSnapshot {\n\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 Cloud project you want to use.\n String projectId = \"YOUR_PROJECT_ID\";\n\n // Name of the snapshot to look up.\n String snapshotName = \"YOUR_SNAPSHOT_NAME\";\n\n getSnapshot(projectId, snapshotName);\n }\n\n // Get information about a snapshot.\n public static void getSnapshot(String projectId, String snapshotName) 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. After completing all of your requests, call\n // the `snapshotsClient.close()` method on the client to safely\n // clean up any remaining background resources.\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.SnapshotsClient.html snapshotsClient = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.SnapshotsClient.html.create()) {\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Snapshot.html snapshot = snapshotsClient.get(projectId, snapshotName);\n System.out.printf(\"Retrieved the snapshot: %s\", snapshot.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Snapshot.html#com_google_cloud_compute_v1_Snapshot_getName__());\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 def get_snapshot(project_id: str, snapshot_name: str) -\u003e compute_v1.Snapshot:\n \"\"\"\n Get information about a Snapshot.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to use.\n snapshot_name: the name of the snapshot you want to look up.\n\n Returns:\n A Snapshot object.\n \"\"\"\n\n snapshot_client = compute_v1.SnapshotsClient()\n\n return snapshot_client.get(project=project_id, snapshot=snapshot_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)."]]