Anexar um disco regional a uma instância de VM do Compute Engine no modo somente leitura
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Anexar um disco regional a uma instância de VM do Compute Engine no modo de leitura e gravação
Anexar um disco regional a uma única máquina virtual em execução no Google Compute Engine. O disco também precisa ser montado pelo sistema operacional antes de ser usado.
O disco é anexado no modo somente leitura. O disco pode ser anexado a várias instâncias de VM simultaneamente.
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 code sample demonstrates how to attach a regional disk to a Google Compute Engine virtual machine instance.\u003c/p\u003e\n"],["\u003cp\u003eThe disk is attached in read-only mode, allowing it to be simultaneously attached to multiple VM instances.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Go code snippet uses the Compute Engine API to perform the disk attachment operation.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication is required to use the Compute Engine API, using Application Default Credentials for a local development environment.\u003c/p\u003e\n"],["\u003cp\u003eBefore the disk can be used it needs to be mounted by the operating system.\u003c/p\u003e\n"]]],[],null,["# Attach a regional disk to a Compute Engine VM instance in read-only mode\n\nAttach a regional disk to a Compute Engine VM instance in read-write mode\n\nAttach a regional disk to a single virtual machine running in Google Compute Engine. The disk also needs to be mounted by the operating system before it can be used.\n\nThe disk is attached in read-only mode. The disk can be attached to multiple VM instances simultaneously.\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 // Attaches the provided regional disk in read-only mode to the given VM.\n // Read-only disks can be attached to multiple VMs at once.\n func attachRegionalDiskReadOnly(w io.Writer, projectID, zone, instanceName, diskUrl string) error {\n \t// projectID := \"your_project_id\"\n \t// zone := \"us-west3-a\" // refers to the instance, not the disk\n \t// instanceName := \"your_instance_name\"\n \t// diskUrl := \"projects/your_project/regions/europe-west3/disks/your_disk\"\n\n \tctx := context.Background()\n \tinstancesClient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_InstancesClient_NewInstancesRESTClient(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer instancesClient.Close()\n\n \tmode := \"READ_ONLY\"\n\n \treq := &computepb.AttachDiskInstanceRequest{\n \t\tAttachedDiskResource: &computepb.AttachedDisk{\n \t\t\tSource: &diskUrl,\n \t\t\tMode: &mode,\n \t\t},\n \t\tInstance: instanceName,\n \t\tProject: projectID,\n \t\tZone: zone,\n \t}\n\n \top, err := instancesClient.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_InstancesClient_AttachDisk(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"unable to attach disk: %w\", err)\n \t}\n\n \tif err = op.Wait(ctx); err != nil {\n \t\treturn fmt.Errorf(\"unable to wait for the operation: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Disk attached\\n\")\n\n \treturn nil\n }\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)."]]