如何从服务器下载图像,然后使用 R 将其上传到网站? [英] How do I download images from a server and then upload it to a website using R?
问题描述
好的,所以我在服务器上存储了大约 2 GB 的文件(图像和其他内容)(我现在使用的是 Cygwin,因为我在 Windows 上),我想知道我是否能够获得所有将这些数据转换为 R,然后最终将其翻译到一个网站上,人们可以在该网站上查看/下载这些图像?
Okay, so I have approximately 2 GB worth of files (images and what not) stored on a server (I'm using Cygwin right now since I'm on Windows) and I was wondering if I was able to get all of this data into R and then eventually translate it onto a website where people can view/download those images?
我目前已经安装了 ssh 包并使用以下命令登录到我的服务器:
I currently have installed the ssh package and have logged into my server using:
ssh::ssh_connect("name_and_server_ip_here")
我已经能够成功连接,但是,我不太确定如何通过 R 定位服务器上的文件.我假设我会使用类似 scp_download 从服务器下载文件,但如前所述,我不太确定如何从服务器中找到文件,所以我无论如何都无法下载它们(目前)!
I've been able to successfully connect, however, I am not particular sure how to locate the files on the server through R. I assume I would use something like scp_download to download the files from the server, but as mentioned before, I am not particularly sure how to locate the files from the server, so I wouldn't be able to download them anyways (yet)!
任何形式的反馈和帮助将不胜感激!谢谢:)
Any sort of feedback and help would be appreciated! Thanks :)
推荐答案
您可以使用 ssh::ssh_exec_internal
和一些 shell 命令来查找"命令.
You can use ssh::ssh_exec_internal
and some shell commands to "find" commands.
sess <- ssh::ssh_connect("r2@myth", passwd="...")
out <- ssh::ssh_exec_internal(sess, command = "find /home/r2/* -maxdepth 3 -type f -iname '*.log'")
str(out)
# List of 3
# $ status: int 0
# $ stdout: raw [1:70] 2f 68 6f 6d ...
# $ stderr: raw(0)
stdout/stderr 是raw(远程命令没有产生ascii 数据是可行的),所以我们可以使用rawToChar
来转换.(如果您有非 ascii 数据,这可能不是控制台安全的,但它在这里,所以我会用它.)
The stdout/stderr are raw (it's feasible that the remote command did not produce ascii data), so we can use rawToChar
to convert. (This may not be console-safe if you have non-ascii data, but it is here, so I'll go with it.)
rawToChar(out$stdout)
# [1] "/home/r2/logs/dns.log\n/home/r2/logs/ping.log\n/home/r2/logs/status.log\n"
remote_files <- strsplit(rawToChar(out$stdout), "\n")[[1]]
remote_files
# [1] "/home/r2/logs/dns.log" "/home/r2/logs/ping.log" "/home/r2/logs/status.log"
对于下载,scp_download
没有矢量化,所以我们一次只能上传一个文件.
For downloading, scp_download
is not vectorized, so we can only upload one file at a time.
for (rf in remote_files) ssh::scp_download(sess, files = rf, to = ".")
# 4339331 C:\Users\r2\.../dns.log
# 36741490 C:\Users\r2\.../ping.log
# 17619010 C:\Users\r2\.../status.log
对于上传,scp_upload
是矢量化的,因此我们可以一次性发送所有内容.我将创建一个新目录(仅用于此示例,并且不会完全弄乱我的远程服务器 :-),然后上传它们.
For uploading, scp_upload
is vectorized, so we can send all in one shot. I'll create a new directory (just for this example, and to not completely clutter my remote server :-), and then upload them.
ssh::ssh_exec_wait(sess, "mkdir '/home/r2/newlogs'")
# [1] 0
ssh::scp_upload(sess, files = basename(remote_files), to = "/home/r2/newlogs/")
# [100%] C:\Users\r2\...\dns.log
# [100%] C:\Users\r2\...\ping.log
# [100%] C:\Users\r2\...\status.log
# [1] "/home/r2/newlogs/"
(我觉得奇怪的是 scp_upload
是矢量化的,而 scp_download
不是.如果这是在 shell/终端上,那么每次调用 scp
需要连接、验证、复制,然后断开连接,有点低效;因为我们使用的是保存的 sess
ion,我相信(未经验证)有由于没有对 R 函数进行矢量化,效率损失很小……尽管矢量化它仍然真的很容易.)
(I find it odd that scp_upload
is vectorized while scp_download
is not. If this were on a shell/terminal, then each call to scp
would need to connect, authenticate, copy, then disconnect, a bit inefficient; since we're using a saved sess
ion, I believe (unverified) that there is little efficiency lost due to not vectorizing the R function ... though it is still really easy to vectorize it.)
这篇关于如何从服务器下载图像,然后使用 R 将其上传到网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!