0

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!

1 Answer 1

3

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.

2
  • Hey, it is great to see that you found a solution for this :) Please consider accepting your own answer so the community with similar issues will see this question as solved Jun 19, 2020 at 0:10
  • 1
    yup, i can only accept my answer after two days, and I plan on accepting it then! Jun 19, 2020 at 0:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.