Search code examples
rgoogle-cloud-platformgoogle-cloud-storagegoogle-colaboratory

Accessing a Datastore from a GCP project in R


I am trying to set up a Google Co-lab notebook that runs in R and can read a GCS bucket from a GCP project. I am using the googleCloudStorageR package. To authenticate and read the bucket, the initial Co-lab notebook runs the following Python commands:

!gcloud auth login
!gcloud config set project project_name
!gcloud sql instances describe project_name 

How can I run the above commands in R using the googleCloudStorageR package ? In the documentation for the package, they mention using the gcs_auth function that reads an authentication JSON file. However, since I will be accessing the buckets through a Co-Lab notebook running on R, I do not want to use an authentication file and instead want to authenticate and connect to the GCP storage in real-time from the Co-Lab notebook. Thank you!


Solution

  • Figured this out. In a Co-lab notebook, run the following code snippet:

    install.packages("httr")
    install.packages("R.utils")
    install.packages("googleCloudStorageR")
    if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab/_ipython.py")) {
          library(R.utils)
          library(httr)
          reassignInPackage("is_interactive", pkgName = "httr", function() return(TRUE))
        }
    library(googleCloudStorageR)
     options(
          rlang_interactive = TRUE,
          gargle_oauth_email = "email_address",
          gargle_oauth_cache = TRUE
        )
    token <- gargle::token_fetch(scopes = "https://www.googleapis.com/auth/cloud-platform")
        googleAuthR::gar_auth(token = token)
    

    There is an issue with gargle authentication that the googleCloudStorageR package uses. A workaround that is similar to the one listed here (https://github.com/r-lib/gargle/issues/140) is to generate a token for cloud scopes, which would give us a token object that we would then use in the gar_auth function.