简体   繁体   English

使用 RCurl::getURL() 从安全 FTP 获取数据

[英]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.我的组织设置了一个 FTP 服务器,它保存了 Shiny 应用程序需要读取的数据。 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.但是,考虑到它是 FTPS 而不是 FTP,我在尝试使用 RCurl 包将其读入 R 时遇到了最困难的时间。 When I tried, I was able to get in but then hit a 530 error because the SSL handshake fails:当我尝试时,我能够进入但由于 SSL 握手失败而遇到 530 错误:

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)

在此处输入图片说明

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使用curl包让我更接近,但我无法弄清楚如何实际提取文件,只需查看元数据

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

After some more browsing and head-scratching, this worked!!!经过更多浏览和头疼之后,这奏效了!!!
(JUST ADD ftp.ssl = TRUE TO THE getURL() ) ftp.ssl = TRUE到 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM