简体   繁体   English

在R中的getURL函数中隐藏密码

[英]Hide password in getURL function in R

I have been using R function getURL() to load data on RStudio from a remote FTP server. 我一直在使用R函数getURL()从远程FTP服务器加载RStudio上的数据。 However, this requires having my username and password visible in the script. 但是,这需要在脚本中显示我的用户名和密码。

require("RCurl")
getURL("ftp://directory/filename.txt", userpwd="user:pwd")

Is there a way to hide this information? 有没有办法隐藏这些信息?

This is something of a guess, as I'm not familiar with R, but the normal way to do this (in any language) is to pass the username and password via environment variables that are set from an external source, such as a .env file that is not checked into your source repo, or are passed in from settings handled by your VM's hypervisor (if you have one). 这是一个猜测,因为我不熟悉R,但通常的方法(使用任何语言)是通过从外部源设置的环境变量传递用户名和密码,例如.env未检入源代码库的.env文件,或者从VM的虚拟机管理程序处理的设置中传入的文件(如果有的话)。 That way your credentials never hit your repo and do not appear directly in the source. 这样,您的凭据永远不会到达您的仓库,也不会直接出现在源代码中。 It's also convenient if you want to run the code in different contexts, such as local, test, stage, production, etc. 如果您想在不同的上下文中运行代码也很方便,例如本地,测试,舞台,制作等。

This answer looks like a reasonable description of how to do this in R. 这个答案看起来像是在R中如何做到这一点的合理描述。

You could use the Keyring package. 您可以使用密钥环包。

library(keyring)
key_set(service = "curl_page", 
                 username = "joe")

Then enter your password when requested. 然后在请求时输入您的密码。 Then you can retrieve it using: 然后你可以使用以下方法检索它

require("RCurl")
getURL("ftp://directory/filename.txt", userpwd=key_get("curl_page",username = "joe"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM