ilovewt
ilovewt

Reputation: 1023

How to write files from local to GCP using python

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.

Upvotes: 0

Views: 592

Answers (1)

Priyashree Bhadra
Priyashree Bhadra

Reputation: 3597

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’)
    

Upvotes: 2

Related Questions