Attempting to download files from SFTP using R

牧云@^-^@ 提交于 2019-11-28 09:54:33

问题


I'm trying to implement R in the workplace and save a bit of time from all the data churning we do.

A lot of files we receive are sent to us via SFTP as they contain sensitive information.

I've looked around on StackOverflow & Google but nothing seems to work for me. I tried using the RCurl Library from an example I found online but it doesn't allow me to include the port(22) as part of the login details.

library(RCurl)
protocol <- "sftp"
server <- "hostname"
userpwd <- "user:password"
tsfrFilename <- "Reports/Excelfile.xlsx"
ouptFilename <- "~/Test.xlsx"

url <- paste0(protocol, "://", server, tsfrFilename)
data <- getURL(url = url, userpwd=userpwd)

I end up getting the error code

Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) : 
embedded nul in string:

Any help would be greatly appreciated as this will save us loads of time!

Thanks,

Shan


回答1:


Looks like a similar situation here: Using R to download SAS file from ftp-server

I'm no expert in r but there it looks like getBinaryUrl() worked instead of getURL() in the example given.

Hope that helps M




回答2:


I too was having problems specifying the port options when using the getURI() and getURL() functions.

In order to specify the port, you simply add the port as port = #### instead of port(####). For example:

data <- getURI(url = url,
               userpwd = userpwd,
               port = 22)

Now, like @MarkThomas pointed out, whenever you get an encodoing error, try getBinaryURL() instead of getURI(). In most cases, this will allow you to download SAS files as well as .csv files econded in UTF-8 or LATIN1!!




回答3:


Note that there are two packages, RCurl and rcurl. For RCurl, I used successfully keyfiles to connect via sftp:

opts <- list(
    ssh.public.keyfile = pubkey, # file name
    ssh.private.keyfile = privatekey, # filename
    keypasswd <- keypasswd # optional password
) 
RCurl::getURL(url=uri, .opts = opts, curl = RCurl::getCurlHandle())

For this to work, you need two create the keyfiles e.g. via putty or similar.



来源:https://stackoverflow.com/questions/47510800/attempting-to-download-files-from-sftp-using-r

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