1

I have a public Google Drive folder containing dozens of raster files (.tif; folder here). I would like to download one file at the time and pass it through a function in r (then delete/replace the raster), without using oauth tokens (i.e. the tidyverse api packages).

The package "googledrive" seems to always ask for access to my Drive even though the folder is public. Is this correct? I am feeling hesitant handling oauth tokens and would prefer using a script that does not require it. Here is what I tried with the first file, but stopped there because tidyverse requested access:

library(googledrive)
temp <- tempfile(tmpdir = "C:/Temp", fileext = ".tif")
test <- drive_download(as_id("1StU410Ox_azZUmsFISElB8uXVsNx72mG"), path = temp, overwrite = TRUE)

I also tried the package curl:

library(curl)
id <- "1StU410Ox_azZUmsFISElB8uXVsNx72mG"
URL <- sprintf("https://drive.google.com/drive/folders/%s", id)
con <- curl(URL)

But don't know how to extract the raster from there.

Finally, I tried:

myUrl <- "https://drive.google.com/file/d/1StU410Ox_azZUmsFISElB8uXVsNx72mG/view?usp=sharing"
test <- raster(url(myUrl))

But got the following error message: Error in (function (classes, fdef, mtable): unable to find an inherited method for function ‘raster’ for signature ‘"url"’

Let me know if it is possible to read, in r, raster files saved in Google Drive, without tokens. Related to this question, is it possible to have a loop that goes from one file to the next in the Google Drive folder? Is there a way to extract the id of all the files in a Google Drive folder? I'm concerned about space if I download the whole folder.

JoeBird
  • 65
  • 1
  • 10
  • In your `sprintf` attempt, you didn't actually paste the id into your URL – camille Jan 25 '19 at 00:21
  • I followed an online example. I could actually simply use "URL <- paste("https://drive.google.com/drive/folders/", id, sep = "")" instead? If so, I'll update my post. – JoeBird Jan 25 '19 at 00:32
  • 1
    I don't think it's going to fix the way that Drive handles downloads, but yes you can use `paste` to put together a URL. Or look at the docs for `sprintf`, you probably want something like `sprintf("https://first.bunch.of.url/%s", id)` so that `id` gets inserted in place of `%s` – camille Jan 25 '19 at 02:09
  • Thanks Camille! Now the URL looks appropriate (I edited the code in my original question). – JoeBird Jan 25 '19 at 04:28
  • I am still unclear how to get the curl connection to download the raster file. I did try "curl_download(URL,tmp)" but I got the error "HTTP error 404". I may need to set up a handle but have yet to figure out what info I need to put into the handle. I would really appreciate some guidance if possible. – JoeBird Jan 25 '19 at 19:18
  • I don't know a way to do it because of how Google Drive handles downloads. I've only rarely done this, but I've done it through `googledrive` package. But there are SO posts on downloading from the API (like [this one](https://stackoverflow.com/q/20665881/5325862)) that may have language-agnostic ideas – camille Jan 25 '19 at 19:37
  • Please check here https://stackoverflow.com/questions/40851509/download-file-from-google-drive-using-r – Anya Sti Mar 30 '19 at 23:27
  • I'm really interested on this, any solution yet ? – César Arquero Cabral May 20 '19 at 08:43
  • 1
    I found this answer really helpful: https://stackoverflow.com/a/50533232/9022665 – Jonathan V. Solórzano Mar 05 '20 at 21:18
  • A long overdue response ... Jonathan, the link you recommended had a solution that worked for me. Thank you very much! – JoeBird Mar 17 '21 at 17:13

0 Answers0