[Python] Permission to Google Cloud Storage via service account in Python

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get a service account to create blobs in Google Cloud Storage from within a Python script, but I am having issues with the credentials.

    1) I create the service account for my project and then download the key file in json:

    "home/user/.config/gcloud/service_admin.json"


    2) I give the service account the necessary credentials (via gcloud in a subprocess)

    roles/viewer, roles/storage.admin, roles/resourcemanager.projectCreator, roles/billing.user


    Then I would like to access a bucket in GCS

    from google.cloud import storage
    import google.auth

    credentials, project = google.auth.default()
    client = storage.Client('myproject', credentials=credentials)
    bucket = client.get_bucket('my_bucket')


    Unfortunately, this results in:

    google.api_core.exceptions.Forbidden: 403 GET
    https://www.googleapis.com/storage/v1/b/my_bucket?projection=noAcl:
    s_account@myproject.iam.gserviceaccount.com does not have
    storage.buckets.get access to my_bucket


    I have somewhat better luck if I set the environment variable

    export GOOGLE_APPLICATION_CREDENTIALS="home/user/.config/gcloud/service_admin.json"


    and rerun the script. However, I want it all to run in one single instance of the script that creates the accounts and continues to create the necessary files in the buckets. How can I access my_bucket if I know where my json credential file is.

    Continue reading...

Compartilhe esta Página