Associer un disque régional à une instance de VM Compute Engine en mode lecture seule
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Associer un disque régional sur une instance de VM Compute Engine en mode lecture-écriture
Associer un disque régional à une machine virtuelle unique exécutée dans Google Compute Engine. Avant de pouvoir être utilisé, le disque doit également être associé par le système d'exploitation.
Le disque est associé en mode lecture seule. Le disque peut être associé simultanément à plusieurs instances de VM.
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","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)."]]