使用 R 检查 FTP 文件是否存在

问题描述 投票:0回答:1

我正在尝试检查我的 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)
但这会下载文件

r curl sftp
1个回答
0
投票

我不相信您可以传递 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

© www.soinside.com 2019 - 2024. All rights reserved.