Azure / Microsoft365R

R SDK for interacting with Microsoft 365 APIs
Other
311 stars 43 forks source link

Cannot Download Files from Onedrive #175

Closed hongyileoxu closed 1 year ago

hongyileoxu commented 1 year ago

Thank you in advance.

I am very new to this and currently using Rstudio Server to do some data work. I have successfully connect my business Onedrive with Rstudio server and can find see the files in Onedrive. However, when I try to download the file from Onedrive, e.g. my_onedrive$download_file("SHoF/2021_info.zip", overwrite = T), it gives me the following error message:

Error in curl::curl_fetch_disk(url, x$path, handle = handle) : Failed writing body (5814 != 16384).

Meanwhile, a file with this name will show up in the directory but cannot be opened and 0 file size.

This issue seems not to link to the file size. I also updated the "curl" packages and the issue did not go away.

Did anyone encounter the same issue? Really appreciated.

raahlstrom commented 1 year ago

I am having the same issue. It will eventually work if I run download_file multiple times, but I can't get it to consistently work on the first few tries.

This is what I'm running:


load_data_file <- function(file_name){
  temp_file_path <- tempfile("file_name")

  site <- Microsoft365R::get_sharepoint_site("my site")
  drive <- site$get_drive("Documents")
  folder <- drive$get_item("my dir")
  file <- folder$get_item(file_name)

  success <- FALSE  # Flag to check if download was successful

  for(i in 1:5) {  # Try to download 3 times
    result <- tryCatch({
      file$download(overwrite = T, dest = temp_file_path)
      success <- TRUE  # If we reach this line, download was successful
      readxl::read_excel(temp_file_path)
    },
    error = function(e) {
      print(paste("Attempt", i, "failed:", e))
      if(i < 5) {
        Sys.sleep(3)  # Wait for 3 seconds before the next attempt
      }
      NULL  # Return NULL on error
    })

    if(success) {
      return(result)  # If successful, exit the loop and return result
    }
  }

  stop("Failed to download the file after 5 attempts.")  # If all attempts fail, throw an error
}
hongooi73 commented 1 year ago

Since you can eventually download successfully, this indicates that Microsoft365R is working and the problem is in a lower-level package, most likely curl. I'm not sure what to do about this, but you can ask for help on the Posit/Rstudio forum, or post an issue on the curl repo.