Listar snapshots
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Neste exemplo, listamos todos os snapshots de um projeto. Para filtrar os resultados, especifique uma expressão de filtro.
Exemplo de código
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, and Python for listing snapshots within a Google Cloud project.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples allow filtering of snapshots using a filter expression, offering more control over the results.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample requires setting up Application Default Credentials for authentication to Compute Engine and refers to the relevant Compute Engine client libraries documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe provided samples showcase how to list all snapshots and print their names using the Compute Engine API client libraries, and has links to other Compute Engine API reference documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe page guides users on locating more Google Cloud product samples through the Google Cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# List snapshots\n\nThis sample lists all snapshots in a project. You can filter the results by specifying a filter expression.\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 \t\"google.golang.org/api/iterator\"\n )\n\n // listSnapshots prints a list of disk snapshots in the given project\n func listSnapshots(w io.Writer, projectID, filter string) error {\n \t// projectID := \"your_project_id\"\n \t// filter := \"\"\n \t// Learn more about filters:\n \t// https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListSnapshotsRequest\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 \treq := &computepb.ListSnapshotsRequest{\n \t\tProject: projectID,\n \t\tFilter: &filter,\n \t}\n\n \tit := snapshotsClient.List(ctx, req)\n \tfmt.Fprintf(w, \"Found snapshots:\\n\")\n \tfor {\n \t\tsnapshot, err := it.Next()\n \t\tif err == iterator.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_Operation_Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"- %s\\n\", snapshot.GetName())\n \t}\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.ListSnapshotsRequest.html;\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 ListSnapshots {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n\n // Project ID or project number of the Cloud project you want to use.\n String projectId = \"YOUR_PROJECT_ID\";\n\n // Filter to be applied when listing snapshots. Learn more about filters here:\n // https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListSnapshotsRequest\n String filter = \"FILTER_CONDITION\";\n\n listSnapshots(projectId, filter);\n }\n\n // List snapshots from a project.\n public static void listSnapshots(String projectId, String filter) throws IOException {\n\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\n // Create the List Snapshot request.\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListSnapshotsRequest.html listSnapshotsRequest = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListSnapshotsRequest.html.newBuilder()\n .setProject(projectId)\n .setFilter(filter)\n .build();\n\n System.out.println(\"List of snapshots:\");\n for (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Snapshot.html snapshot : snapshotsClient.list(listSnapshotsRequest).iterateAll()) {\n System.out.println(snapshot.getName());\n }\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 __future__ import annotations\n\n from collections.abc import Iterable\n\n from google.cloud import compute_v1\n\n\n def list_snapshots(project_id: str, filter_: str = \"\") -\u003e Iterable[compute_v1.Snapshot]:\n \"\"\"\n List snapshots from a project.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to use.\n filter_: filter to be applied when listing snapshots. Learn more about filters here:\n https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListSnapshotsRequest\n\n Returns:\n An iterable containing all Snapshots that match the provided filter.\n \"\"\"\n\n snapshot_client = compute_v1.SnapshotsClient()\n request = compute_v1.ListSnapshotsRequest()\n request.project = project_id\n request.filter = filter_\n\n return snapshot_client.list(request)\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)."]]