0

I have looked up a few but some are outdated, the latest guide, however requested me to do this:

from gcloud import storage
client = storage.Client()

But no matter what I cannot even get pass this first step with error

OSError: Project was not passed and could not be determined from the environment.

I tried both in IDE and Google Colab, but both cannot work. Will appreciate a solution to this.

2
  • 4
    You'll need to authenticate your environment. Commented Nov 14, 2021 at 3:23
  • 1
    install gcloud sdk and perform gcloud auth application-default login Commented Nov 14, 2021 at 11:47

1 Answer 1

2

The error states : Project was not passed and could not be determined from the environment. Authenticating and setting the project correctly will solve the error.

Steps/ Ways in which the following can be done are :

After you have installed Python and enabled Cloud Storage API in Google Cloud Console, your next steps would be to :

  • Install the Cloud Client Libraries for Python for an individual API like Cloud Storage, using a command similar to the following:

    pip install --upgrade google-cloud-storage
    
  • Install Cloud SDK which can be used to access Cloud Storage services from the command line and then do gcloud auth application-default login.Note that this command generates credentials for client libraries. To authenticate the CLI itself, use:

    gcloud auth login
    

    Previously, gcloud auth login was used for both use cases. If your gcloud installation does not support the new command, please update it:

    gcloud components update
    
  • After gcloud is installed, you can do gcloud config set project <your-project-id> to automatically set the project. If you're using a service account with GOOGLE_APPLICATION_CREDENTIALS, it'll pull the project from the service account. Follow this documentation on how to set up authentication by setting the environmental variable GOOGLE_APPLICATION_CREDENTIALS using a service account.

  • Also, Cloud Storage Client takes in a parameter called project (str or None). Here you pass the parameter to specify the project-id in which the client acts on, like shown below. Passing no arguments at all will also “just work” if the credentials are inferred from your local environment by using Google Application Default Credentials.

    from google.cloud import storage
    storage_client = storage.Client(‘project-id’)
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot ! I am unfamiliar with the GCP API, I store some files in the GCP bucket, wonder if I can get any project_id directly from the bucket?
Creating a bucket, requires specifying a project. When you first created a project for a bucket, you either accepted the default generated project ID or created your own. The project ID is a unique identifier for a project and is used only within the console. You can find your Google Project ID in your Google API console. Go to the Google API console, choose your project from the drop-down selector, and then you will find the project ID in the “Home” screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.