124

I am logged in to a GCE instance via SSH. From there I would like to access the Storage with the help of a Service Account:

GCE> gcloud auth list
Credentialed accounts:
 - 1234567890-compute@developer.gserviceaccount.com (active)

I first made sure that this Service account is flagged "Can edit" in the permissions of the project I am working in. I also made sure to give him the Write ACL on the bucket I would like him to copy a file:

local> gsutil acl ch -u 1234567890-compute@developer.gserviceaccount.com:W gs://mybucket

But then the following command fails:

GCE> gsutil cp test.txt gs://mybucket/logs

(I also made sure that "logs" is created under "mybucket").

The error message I get is:

Copying file://test.txt [Content-Type=text/plain]...
AccessDeniedException: 403 Insufficient Permission               0 B  

What am I missing?

Christophe
  • 1,942
  • 3
  • 21
  • 29
  • 7
    Was the GCE VM created with full control or read/write GCS scope? – jterrace Dec 03 '14 at 17:09
  • 1
    Thank you for pointing that out. I was not aware of that option indeed. I re-created the instance with the option enabled and it worked. If you could suggest to turn on the flag as an answer I would happily flag it. – Christophe Dec 04 '14 at 09:54

9 Answers9

188

One other thing to look for is to make sure you set up the appropriate scopes when creating the GCE VM. Even if a VM has a service account attached, it must be assigned devstorage scopes in order to access GCS.

For example, if you had created your VM with devstorage.read_only scope, trying to write to a bucket would fail, even if your service account has permission to write to the bucket. You would need devstorage.full_control or devstorage.read_write.

See the section on Preparing an instance to use service accounts for details.

Note: the default compute service account has very limited scopes (including having read-only to GCS). This is done because the default service account has Project Editor IAM permissions. If you use any user service account this is not typically a problem since user created service accounts get all scope access by default.

After adding necessary scopes to the VM, gsutil may still be using cached credentials which don't have the new scopes. Delete ~/.gsutil before trying the gsutil commands again. (Thanks to @mndrix for pointing this out in the comments.)

starball
  • 20,030
  • 7
  • 43
  • 238
jterrace
  • 64,866
  • 22
  • 157
  • 202
  • 40
    As of now, you can edit the scopes. Stop the machine - edit - then change the Cloud API access scopes. I believe this has only be available for about a month now. – Warren Dec 13 '16 at 00:08
65

You have to log in with an account that has the permissions you need for that project:

gcloud auth login
Javier Giovannini
  • 2,302
  • 1
  • 19
  • 21
56

gsutil config -b

Then surf to the URL it provides, [ CLICK Allow ]

Then copy the verification code and paste to terminal.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
Dr. Tyrell
  • 2,829
  • 1
  • 16
  • 9
  • Note this doesn’t work if you run `gsutil` from `ssh`. – bfontaine Dec 30 '16 at 14:17
  • 3
    It "doesn't work" in so far as not opening your browser. Instead, it provides a url for you to manually copy and paste. – BuvinJ Sep 08 '17 at 19:34
  • 6
    This also works fine for everyone else _if you drop the `-b`_. That will not open a browser, but will simply spit out a url that can be opened outside of the shell. – oligofren Mar 23 '18 at 13:36
  • Nice - This is the first solution I have found which does not require restarting the VM (and thereby potentially losing data). – Jonathan Dec 17 '21 at 20:11
  • This solution worked for me without restart. Thank you!! – Ravi Feb 25 '22 at 19:06
30
  1. Stop VM
  2. goto --> VM instance details.
  3. in "Cloud API access scopes" select "Allow full access to all Cloud APIs" then Click "save".
  4. restart VM and Delete ~/.gsutil .
Omar Bara
  • 409
  • 4
  • 5
13

I have written an answer to this question since I can not post comments:

This error can also occur if you're running the gsutil command with a sudo prefix in some cases.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
TheLoneDeranger
  • 1,161
  • 9
  • 13
4
  1. After you have created the bucket, go to the permissions tab and add your email and set Storage Admin permission.

screenshot

  1. Access VM instance via SSH >> run command: gcloud auth login and follow the steps.

Ref: https://groups.google.com/d/msg/gce-discussion/0L6sLRjX8kg/kP47FklzBgAJ

Văn Quyết
  • 2,384
  • 14
  • 29
3

So I tried a bunch of things trying to copy from GCS bucket to my VM. Hope this post helps someone.

Via SSHed connection: enter image description here

and following this script:

sudo gsutil cp gs://[BUCKET_NAME]/[OBJECT_NAME] [OBJECT_DESTINATION_IN_LOCAL]

Got this error:

AccessDeniedException: 403 Access Not Configured. Please go to the Google Cloud Platform Console (https://cloud.google.com/console#/project) for your project, select APIs and Auth and enable the Google Cloud Storage JSON API.

What fixed this was following "Activating the API" section mentioned in this link - https://cloud.google.com/storage/docs/json_api/

enter image description here

Once I activated the API then I authenticated myself in SSHed window via

gcloud auth login

Following authentication procedure I was finally able to download from Google Storage Bucket to my VM.

PS

I did make sure to:

  1. Make sure that gsutils are installed on my VM instance.
  2. Go to my bucket, go to the permissions tab and add desired service accounts and set Storage Admin permission / role. enter image description here

    3.Make sure my VM had proper Cloud API access scopes: enter image description here

enter image description here

Yev Guyduy
  • 1,371
  • 12
  • 13
2

From the docs: https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#changeserviceaccountandscopes

You need to first stop the instance -> go to edit page -> go to "Cloud API access scopes" and choose "storage full access or read/write or whatever you need it for"

Changing the service account and access scopes for an instance If you want to run the VM as a different identity, or you determine that the instance needs a different set of scopes to call the required APIs, you can change the service account and the access scopes of an existing instance. 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 that 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. Use one of the following methods to the change service account or access scopes of the stopped instance.

Adelin
  • 18,144
  • 26
  • 115
  • 175
-10

Change the permissions of bucket.

Add a user for "All User" and give "Storage Admin" access.

Lamanus
  • 12,898
  • 4
  • 21
  • 47