我正在尝试检查我的 ftp 上是否存在文件
我尝试使用软件包
{httr}
和 {httr2}
但失败了。以下是我的尝试
{curl}
包进行操作my_file_url <- "sftp://my_server/my_file.txt"
httr
httr::HEAD(my_file_url, httr::authenticate(my_username, my_password))
失败并显示以下消息
Error in x$headers$`content-type` :
$ operator is invalid for atomic vectors
httr2
request(my_file_url) %>%
req_method("HEAD") %>%
req_auth_basic( my_username, my_password) %>%
req_perform()
奇怪地失败(我检查了我的凭据)并显示以下消息
Error:
! Authentication failure
附注我尝试了
curl::curl_fetch_memory(my_file_url, handle = my_curl_handle)
但这会下载文件
我不相信您可以传递 HTTP 方法 (
HEAD
) 或验证 sftp 服务的标头,但如果您的权限允许您获取目录列表,这可能会起作用:
library(curl)
#> Using libcurl 8.3.0 with Schannel
# https://test.rebex.net/ demo server
SFTP_DEMO <- "sftp://demo:[email protected]:22/"
file_list <-
curl(url = SFTP_DEMO, handle = new_handle(dirlistonly = TRUE)) |>
readLines()
file_list
#> [1] "." ".." "pub" "readme.txt"
"readme.txt" %in% file_list
#> [1] TRUE
"im-not-here" %in% file_list
#> [1] FALSE
创建于 2023 年 11 月 29 日,使用 reprex v2.0.2