R Dropbox File Download

ⅰ亾dé卋堺 提交于 2020-01-05 09:53:55

问题


I have a dropbox link that was shared to me to download but unlike other link, its says I am forbidden to download it.

My functions:

source_DropboxData <- function(file, key, sha1 = NULL, sep = ",", header = TRUE){
  URL <- paste0('https://dl.dropboxusercontent.com/s/', 
            key, '/', file)

  stopifnot(is.character(URL), length(URL) == 1)

  temp_file <- tempfile()
  on.exit(unlink(temp_file))

  request <- GET(URL)
  stop_for_status(request)
  writeBin(content(request, type = "raw"), temp_file)

  file_sha1 <- digest(file = temp_file, algo = "sha1")

  if (is.null(sha1)) {
    message("SHA-1 hash of file is ", file_sha1)
  }
  else {
    if (!identical(file_sha1, sha1)) {
      stop("SHA-1 hash of downloaded file (", file_sha1, 
       ")\n  does not match expected value (", sha1, 
           ")", call. = FALSE)
    }
  }

  read.table(temp_file, sep = sep, header = header)
}    

My link looks like this:

https://www.dropbox.com/sh/od6ymc4wu8uht5e/IxPX-EOhNx/a%b%x  #fake, for demonstration

Formal ones look like this:

http://dl.dropbox.com/s/c18lcwnnrodsevt/test_dropbox_source.R

My question is whats the difference between the two link, is one secure and not downloadable while another is possible? I was under the impression that the function from repmis is able to do both private and public files.

Thanks


回答1:


The key here is

  1. tell dropbox to serve up the raw file by appending ?raw=1 to the URL
  2. as dropbox uses https, you (currently at least) need to open the connection (which is smart enough to use secure transport) and give this connection to the file read or load function (not just a raw URL string)

This is an example using load and a .rda file, but same works for read.csv etc.

myURL = "https://www.dropbox.com/s/randomIDstring/YourFilename.rda?raw=1"
myConnection = url(myURL)
print(load(myConnection))
close(myConnection) # good practice to close connection opened by url()


来源:https://stackoverflow.com/questions/17553808/r-dropbox-file-download

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!