laBouz
laBouz

Reputation: 215

Get Data from Secure FTP with RCurl::getURL()

My organization set up an FTP server which holds data that needed to get read for a Shiny application. I was able to successfully connect and get the write files in the terminal via:

curl --ftp-ssl ftp://<HOSTNAME>:<PORT_NUM> --user "<MY_USER>:<MY_PW>" -o Wellness\ Quarantine\ and\ Isolation.csv

However, I was having the hardest time trying to get this read into R with the RCurl package, given that it is FTPS not FTP. When I tried, I was able to get in but then hit a 530 error because the SSL handshake fails:

library(RCurl)

opts <- curlOptions(
  dirlistonly = TRUE,
  sslversion = 6L,
  verbose = TRUE,
  ftp.use.epsv = FALSE,
  ssl.verifypeer=TRUE
  
)

x <- getURL("ftp://<HOSTNAME>:<PORT_NUM>", userpwd = "<MY_USER>:<MY_PW>",
            .opts = opts)

enter image description here

Using the curl package got me a little closer, but I couldn't figure out how to actually extract the files, just see the metadata

#using curl package - better maintained
library(curl)
h <- curl::new_handle(ftp_use_epsv=FALSE, dirlistonly=TRUE, crlf=TRUE,
                      ssl_verifypeer=FALSE, ftp_response_timeout=30)
curl::handle_setopt(h, userpwd = "<MY_USER>:<MY_PW>", use_ssl = 3)

tmp <- tempfile()
curl::curl_download("ftp://<HOSTNAME>:<PORT_NUM>",handle = h, tmp)

readLines(tmp)

Upvotes: 2

Views: 1138

Answers (1)

laBouz
laBouz

Reputation: 215

After some more browsing and head-scratching, this worked!!!
(JUST ADD ftp.ssl = TRUE TO THE getURL() )

library (RCurl)

# Input #
protocol <- "ftp"
server <- "<HOSTNAME>:<PORT_NAME>"
userpwd <- "<MY_USER>:<MY_PW>"
tsfrFilename <- "/Wellness\ Quarantine\ and\ Isolation.csv" #you can transfer other kinds, like text, as well.

# Run #
## Download Data
url <- paste0(protocol, "://", server, tsfrFilename)
data <- getURL(url = url, userpwd=userpwd, ftp.ssl = TRUE)


## read File to csv
quar_iso <- readr::read_csv(data)

Upvotes: 3

Related Questions