Enumera los discos de una zona
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En esta muestra, se enumeran todos los discos en una zona determinada.
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, and Python for listing all disks within a specified zone in Google Cloud.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample requires users to set up Application Default Credentials for authentication and provides links for setting it up.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples also require users to setup their respective environments through the quickstart instructions provided.\u003c/p\u003e\n"],["\u003cp\u003eThe samples demonstrate how to use the Compute Engine client libraries to send a request to list disks in a specified zone with the ability to filter the results.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples provide examples on how to handle and display the results, each tailored to the specific language.\u003c/p\u003e\n"]]],[],null,["# List disks in a zone\n\nThis sample lists all the disks in a given zone.\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 // listDisks lists disks in a project zone.\n func listDisks(w io.Writer, projectID, zone, filter string) error {\n \t// projectID := \"your_project_id\"\n \t// zone := \"us-central1-a\"\n \t// filter := \"\"\n\n \t// Formatting for filters:\n \t// https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListDisksRequest\n\n \tctx := context.Background()\n \tclient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_DisksClient_NewDisksRESTClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewDisksRESTClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \treq := &computepb.ListDisksRequest{\n \t\tProject: projectID,\n \t\tZone: zone,\n \t\tFilter: &filter,\n \t}\n\n \tit := client.List(ctx, req)\n \tfmt.Fprintf(w, \"Disks in zone %s:\\n\", zone)\n \tfor {\n \t\tdisk, err := it.Next()\n \t\tif err == context.Canceled || err == context.DeadlineExceeded {\n \t\t\treturn err\n \t\t}\n \t\tif err != nil {\n \t\t\tbreak\n \t\t}\n \t\tfmt.Fprintf(w, \"- %s\\n\", *disk.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_Operation_Name)\n \t}\n\n \tif err != nil {\n \t\treturn fmt.Errorf(\"ListDisks: %w\", err)\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.Disk.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.DisksClient.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListDisksRequest.html;\n import java.io.IOException;\n import java.util.concurrent.ExecutionException;\n import java.util.concurrent.TimeoutException;\n\n public class ListDisks {\n\n public static void main(String[] args)\n throws IOException, ExecutionException, InterruptedException, TimeoutException {\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 // The zone where the disks are located.\n String zone = \"europe-central2-b\";\n\n // Filter to be applied when listing disks. Learn more about filters here:\n // https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListDisksRequest\n String filter = \"FILTER_CONDITION\";\n\n listDisks(projectId, zone, filter);\n }\n\n // Lists disks from a project.\n public static void listDisks(String projectId, String zone, String filter)\n 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 `disksClient.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.DisksClient.html disksClient = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.DisksClient.html.create()) {\n\n // Create the request object.\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListDisksRequest.html listDisksRequest = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListDisksRequest.html.newBuilder()\n .setProject(projectId)\n .setZone(zone)\n .setFilter(filter)\n .build();\n\n System.out.println(\"List of disks:\");\n for (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Disk.html disk : disksClient.list(listDisksRequest).iterateAll()) {\n System.out.println(disk.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_disks(\n project_id: str, zone: str, filter_: str = \"\"\n ) -\u003e Iterable[compute_v1.Disk]:\n \"\"\"\n Lists disks in a project.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to use.\n zone: name of the zone\n filter_: filter to be applied when listing disks. Learn more about filters here:\n https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListDisksRequest\n \"\"\"\n disk_client = compute_v1.DisksClient()\n request = compute_v1.ListDisksRequest()\n request.project = project_id\n request.zone = zone\n request.filter = filter_\n return disk_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)."]]