googledrive icon indicating copy to clipboard operation
googledrive copied to clipboard

drive_download() is uncooperative re: downloading into a folder

Open jennybc opened this issue 6 years ago • 2 comments

I wish drive_download(WHATEVER, path = "pre_existing_local_folder/") would download WHATEVER into pre_existing_local_folder/WHATEVER instead of:

Error: Path exists and overwrite is FALSE

jennybc avatar Apr 02 '19 15:04 jennybc

I share the same wish!
I try to download multiple files into an empty folder that already exists using: walk(file_list$id, ~ drive_download(as_id(.x), overwrite = TRUE, path = here("raw_data_files/")))

and instead receive Error in curl::curl_fetch_disk(url, x$path, handle = handle) : Failed to open file /home/username/my-git-repo/raw_data_files.

reblake avatar Jan 26 '22 20:01 reblake

+1 for this feature! In the meantime I found this quite helpful in case anyone stumbles on this issue:

library(googledrive)

drive_down_to_dir <- function(x, dir, overwrite = TRUE, ...) {
  if (!dir.exists(dir))
    dir.create(dir)
  purrr::pmap(x, function(name, id, drive_resource, ...) {
    drive_download(as_id(id), file.path(dir, name),
                   overwrite = overwrite, ...)
  },
  ...)
}

file_drib <- drive_examples_remote()[1:3,]

drive_down_to_dir(file_drib, "new_folder")
#> File downloaded:
#> • 'chicken_doc' <id: 1X9pd4nOjl33zDFfTjw-_eFL7Qb9_g6VfVFDp1PPae94>
#> Saved locally as:
#> • 'new_folder/chicken_doc.docx'
#> File downloaded:
#> • 'chicken_sheet' <id: 1SeFXkr3XdzPSuWauzPdN-XnaryOYmZ7sFiUF5t-wSVU>
#> Saved locally as:
#> • 'new_folder/chicken_sheet.xlsx'
#> File downloaded:
#> • 'chicken.csv' <id: 1VOh6wWbRfuQLxbLg87o58vxJt95SIiZ7>
#> Saved locally as:
#> • 'new_folder/chicken.csv'
#> [[1]]
#> # A dribble: 1 × 4
#>   name        local_path                  id                      drive_resource
#>   <chr>       <chr>                       <drv_id>                <list>        
#> 1 chicken_doc new_folder/chicken_doc.docx 1X9pd4nOjl33zDFfTjw-_e… <named list>  
#> 
#> [[2]]
#> # A dribble: 1 × 4
#>   name          local_path                    id                  drive_resource
#>   <chr>         <chr>                         <drv_id>            <list>        
#> 1 chicken_sheet new_folder/chicken_sheet.xlsx 1SeFXkr3XdzPSuWauz… <named list>  
#> 
#> [[3]]
#> # A dribble: 1 × 4
#>   name        local_path             id                           drive_resource
#>   <chr>       <chr>                  <drv_id>                     <list>        
#> 1 chicken.csv new_folder/chicken.csv 1VOh6wWbRfuQLxbLg87o58vxJt9… <named list>

Created on 2022-06-16 by the reprex package (v2.0.1)

Thanks to everyone for this great package!

h-a-graham avatar Jun 16 '22 19:06 h-a-graham