Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the scope of a Google Compute Engine service account to write data to a Google Cloud Storage bucket?

There is an instance running on GCE and there is a GCS bucket in same project. But I am not able to write data to it. How can I write data from a VM instance to the storage bucket?

like image 543
Sunil Garg Avatar asked Aug 06 '15 11:08

Sunil Garg


People also ask

How do I change my Cloud API access scopes?

To change scopes, in the Access scopes section, select Set access for each API and set the appropriate scopes for your needs. If you're not sure of the proper access scopes to set, choose Allow full access to all Cloud APIs and then make sure to restrict access by setting IAM roles on the service account.

How do I upload data to Google Cloud Storage?

Drag and drop the desired files from your desktop or file manager to the main pane in the Google Cloud console. Click the Upload Files button, select the files you want to upload in the dialog that appears, and click Open.


1 Answers

Update: as of 15 Dec 2016, the feature setServiceAccount is now in beta:

You can change the service account and/or access scopes of an existing instance if you want to run as a different identity, or you determine that the instance needs a different set of scopes to call the required APIs. For example, you can change access scopes to grant access to a new API or change an instance so that it runs as a service account you created instead of the Compute Engine Default Service Account.

To change an instance's service account and access scopes, the instance must be temporarily stopped. To stop your instance, read the documentation for Stopping an instance. After changing the service account or access scopes, remember to restart the instance.

The documentation provides instructions for how to use this feature using the Google Cloud Console, gcloud CLI tool, as well as API. Here's an example using gcloud; see the docs for more info.

gcloud beta compute instances set-scopes [INSTANCE_NAME] \
     --service-account [SERVICE_ACCOUNT_EMAIL] \
     [--no-scopes | --scopes [SCOPES,...]]

Previous answer (valid prior to 15 Dec 2016):

You can't change the scopes of a running VM instance. You can either:

  • create a new instance (possibly reusing the disks of your current instance) with the right scopes specified at creation time, e.g.:

    gcloud compute instances create [...] --scopes storage-rw
    
  • if you want to use gsutil manually from inside the VM, you can run gsutil config inside the VM and authenticate by following its prompts

  • if you are planning to use an automated tool using Google APIs, use a service account

like image 167
Misha Brukman Avatar answered Oct 15 '22 15:10

Misha Brukman