Search code examples
pythonpython-3.xgoogle-cloud-storagegsutil

Google cloud get bucket - works with cli but not in python


I was asked to preform integration with an external google storage bucket, I had received a credentials json,

And while trying to do gsutil ls gs://bucket_name (after configuring myself with the creds json) I had received a valid response, as well as when I tried to upload a file into the bucket.

When trying to do it with Python3, it does not work:

While using google-cloud-storage==1.16.0 (tried also the newer versions), I'm doing:

project_id = credentials_dict.get("project_id")
credentials = service_account.Credentials.from_service_account_info(credentials_dict)
client = storage.Client(credentials=credentials, project=project_id)

bucket = client.get_bucket(bucket_name)

But on the get_bucket line, I get:

google.api_core.exceptions.Forbidden: 403 GET https://www.googleapis.com/storage/v1/b/BUCKET_NAME?projection=noAcl: USERNAME@PROJECT_ID.iam.gserviceaccount.com does not have storage.buckets.get access to the Google Cloud Storage bucket.

The external partner which I'm integrating with, saying that the user is set correctly, and to prove it they're showing that I can preform the action with gsutil.

Can you please assist? Any idea what might be the problem?


Solution

  • The answer was that the creds were indeed wrong, but it did worked when I tried to preform on the client client.bucket(bucket_name) instead of client.get_bucket(bucket_name).