Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
Last active July 5, 2019 17:47
Show Gist options
  • Save artemklevtsov/ccc42809e534f4d8da7bfe2ba735ea75 to your computer and use it in GitHub Desktop.
Save artemklevtsov/ccc42809e534f4d8da7bfe2ba735ea75 to your computer and use it in GitHub Desktop.
R curl FTP example
# install.packages("https://github.com/jeroen/curl/archive/master.tar.gz", repos = NULL)
ftp_list_files <- function(url, user = NULL, password = NULL) {
checkmate::assert_string(url, pattern = "ftp://")
checkmate::assert_string(user, null.ok = TRUE)
checkmate::assert_string(password, null.ok = TRUE)
h <- curl::new_handle()
curl::handle_setopt(
handle = h,
verbose = TRUE,
username = user,
password = password
)
resp <- curl::curl_fetch_memory(url, h)
txt <- rawToChar(resp$content)
data.table::fread(text = txt, header = FALSE)
}
ftp_dl_file <- function(url, dest, user = NULL, password = NULL) {
checkmate::assert_string(url, pattern = "ftp://")
checkmate::assert_string(user, null.ok = TRUE)
checkmate::assert_string(password, null.ok = TRUE)
h <- curl::new_handle()
curl::handle_setopt(
handle = h,
verbose = TRUE,
username = user,
password = password
)
curl::curl_download(url, dest, handle = h)
}
ftp_ul_file <- function(url, file, user = NULL, password = NULL) {
checkmate::assert_string(url, pattern = "ftp://")
checkmate::assert_string(user, null.ok = TRUE)
checkmate::assert_string(password, null.ok = TRUE)
checkmate::assert_file_exists(file)
curl::curl_upload(
file = file,
url = url,
verbose = TRUE,
username = user,
password = password
)$url
}
ftp_rm_file <- function(url, file, user = NULL, password = NULL) {
checkmate::assert_string(url, pattern = "ftp://")
checkmate::assert_string(user, null.ok = TRUE)
checkmate::assert_string(password, null.ok = TRUE)
h <- curl::new_handle()
curl::handle_setopt(
handle = h,
quote = paste("DELE", file),
verbose = TRUE,
username = user,
password = password
)
resp <- curl::curl_fetch_memory(url, h)
txt <- rawToChar(resp$content)
data.table::fread(text = txt, header = FALSE)
}
tele2_url <- "ftp://speedtest.tele2.net/"
dlptest_url <- "ftp://ftp.dlptest.com/"
dlptest_user <- "[email protected]"
dlptest_pwd <- "fLDScD4Ynth0p4OJ6bW6qCxjh"
ftp_list_files(tele2_url)
ftp_list_files(dlptest_url, dlptest_user, dlptest_pwd)
f1 <- "1MB.zip"
tmp1 <- file.path(tempdir(), f1)
unlink(tmp1)
ftp_dl_file(paste0(tele2_url, f1), tmp1)
file.size(tmp1)
f2 <- "R-version.txt"
tmp2 <- file.path(tempdir(), f2)
writeLines(capture.output(R.version), tmp2)
ftp_ul_file(dlptest_url, tmp2, dlptest_user, dlptest_pwd)
tmp3 <- file.path(tempdir(), f2)
unlink(tmp3)
ftp_dl_file(paste0(dlptest_url, f2), tmp3, dlptest_user, dlptest_pwd)
file.size(tmp3) == file.size(tmp2)
ftp_rm_file(dlptest_url, f2, dlptest_user, dlptest_pwd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment