使用 R 使用 sftp 协议访问 Azure Blob

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

我需要从启用了 sftp 支持的 Azure blob 存储下载文件。我已获得用户名、密码和端点,这些在使用 filezilla 访问 blob 时有效。

问题是我无法使用 ssh、sftp 或 RCurl 库下载文件。

编辑:我让它可以与 Python 一起使用,但我的产品环境仅限于 R。

Rcurl代码:

# Define connection parameters
protocol <- "sftp"
server <- "endpoint.blob.core.windows.net"
user <- "username"
password <- "password"
file <- "/home/folder/file.csv"
url <- paste0(protocol, "://", server, file)

# Download the file    
data <- getURL(url = url, userpwd = paste0(user, ":", password), verbose = TRUE)

错误:

Host endpoint.blob.core.windows.net:22 was resolved.
IPv6: (none)
IPv4: 52.xxx.xxx.xx
Trying 52.xxx.xxx.xxx:22...
Connected to endpoint.blob.core.windows.net (52.xxx.xxx.xxx) port 22
Failure establishing ssh session: -5, Unable to exchange encryption keys
Closing connection
Error in function (type, msg, asError = TRUE)  :
Failure establishing ssh session: -5, Unable to exchange encryption keys

ssh 代码:

# Define connection parameters
server <- "endpoint.blob.core.windows.net"
port <- 22
user <- "username"
password <- "password"
file_path <- "/home/folder/file.csv"
local_path <- "."
    
# Establish SFTP connection
sesh <- ssh_connect(paste0(user, "@", server), passwd = password)
    
# Download the file
scp_download(session=sesh, files=file_path, to=local_path, verbose = TRUE)

sesh 变量:

<ssh session>
[email protected]:22 (connected)
server: 07:af:36:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

错误:

Error: SCP failure: Failed executing command: scp -f -r '/home/folder/file.csv'

sftp 代码:

# Define connection parameters
urlRemote <- 'endpoint.blob.core.windows.net'
username <- "username"
password <- "password"
    
# Establish SFTP connection
sftp_con <- sftp_connect(server = urlRemote, 
                         folder = "/home/folder", 
                         username = username, 
                         password = password,
                         port=22)
# list files in blob folder    
clientFiles <- sftp_list(sftp_connection = sftp_con,
                         type = "f", 
                         verbose = T)

sftp_con 变量:

$protocol
[1] "sftp://"

$server
[1] "endpoint.blob.core.windows.net"

$port
[1] 22

$folder
[1] "home/folder"

$username
[1] "username"

$password
[1] "password"

$userpass
[1] "username:password"

$url
[1] "sftp://endpoint.blob.core.windows.net/home/folder/"

$url_port
[1] "sftp://endpoint.blob.core.windows.net:22/home/folder/"

$login_url
[1] "sftp://username:[email protected]/home/folder/"

$login_url_port
[1] "sftp://username:[email protected]:22/home/folder/"

$timeout
[1] 30

错误:

SFTP url: sftp://endpoint.blob.core.windows.net/home/folder/
Error in function (type, msg, asError = TRUE)  :
Failure establishing ssh session: -5, Unable to exchange encryption keys

我通常使用此 git 存储库中的“sftp”库 https://github.com/stenevang/sftp 来完成此类工作,但它似乎不适用于启用了 sftp 协议的 Azure blob。

r ssh azure-blob-storage sftp rcurl
1个回答
0
投票

使用 R 使用 sftp 协议访问 Azure Blob

这是使用 R 语言通过 SFTP 协议从 azure blob 存储下载文件的代码。

代码:

server <- 'venkat8912.blob.core.windows.net'
user <- 'venkat8912.test.venkat'
remote_file <- "data/invoice.pdf"
local_path <- "./prod.pdf"

# Construct the sftp command
sftp_cmd <- paste("sftp", "-o", "StrictHostKeyChecking=no", 
                  paste0(user, "@", server, ":", remote_file), local_path)

# Execute the command in the system shell
system(sftp_cmd, intern = TRUE)

输出:

source("c:\\Users\\xxx\\xxx\\data.R", encoding = "UTF-8")
[email protected]'s password: 
Connected to venkat8912.blob.core.windows.net.

上面的代码没有使用任何R库,文件被下载到我的本地环境中。

enter image description here

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