user1234440
user1234440

Reputation: 23587

R Dropbox File Download

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

Upvotes: 1

Views: 1974

Answers (1)

tim
tim

Reputation: 3608

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()

Upvotes: 1

Related Questions