Heroku 中的多行配置变量
我有一个 Rails 应用程序,可以在使用 Paypal 进行交易之前加载许多 RSA 证书。在我的开发机器上,这些证书是从文件系统中的文件读取的,但由于 Heroku(我用于部署)基本上是只读的,我无法上传这些文件,所以我猜我必须从配置变量中读取证书(请参阅 Heroku 配置变量)。
因为证书由多行数据组成,所以我不确定如何将它们设置为变量,或者即使这是可能的。有谁知道我该如何做到这一点或能够提出替代方法?
非常感谢, 艾迪
I've have a Rails app that loads a number of RSA certificates before a transaction is made with Paypal. On my development machine, these certificates are read from files in the file system but because Heroku (which I'm using for delpoyment) is largely read-only, I can't upload these files so I'm guessing I'll have to read the certificates from config variables (see Heroku Config Vars).
Because the certificates consist of multiple lines of data, I'm not sure how to set them as variables or even if this is possible. Does anyone know how I could do this or be able to suggest an alternative approach?
Many thanks,
Eddie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我发现添加多行配置的一个简单方法是双引号它们,然后从我的本地环境中回显它们
I found that a easy way to add multi-line configs is to double quote them and then echo them from my local environment
如果您想从文件内容设置 Heroku 配置值,您可以使用以下 shell 技巧:
可以通过在值周围加上引号来直接设置多行值:
如果您使用 Foreman 在本地运行(现在
heroku local
),它不支持多行变量。您必须首先使用某些东西将它们注入到环境中,例如 envdir:If you want to set Heroku config values from your file contents, you can use the following shell trick:
Multi-line values can be set directly by putting quotes around the value:
If you're using Foreman to run locally (now
heroku local
), it doesn't support multi-line variables. You must use something to inject them into the environment first, such as envdir:或者,您可以转到 Heroku 仪表板的“设置”选项卡,打开“配置变量”并将其粘贴进去。
简单。
Or you can just go to the Settings tab of the Heroku dashboard, open Config Vars and paste it in.
Easy peasy.
我们需要做同样的事情。
您可以将变量值括在双引号中:
如果您使用 Foreman 进行本地主机开发,则 .env 文件不支持多行变量,因此您需要在启动 Foreman 之前将其导出到 shell
We needed to do the same thing.
You can wrap the variable value in double quotes:
If you're using Foreman for localhost development, the .env file doesn't support multi-line variables so you'll need to export it to the shell before launching Foreman
我的回答有点晚了,但我最近在多行环境中遇到了同样的问题。 Heroku 上的变量。
我的解决方案是使用
strict_encode64
:添加密钥:
在代码中,然后使用
Base64.strict_decode64(ENV['SECRET_KEY'])
对其进行解码My answer comes a bit late but I had the same issue recently with multi-line env. variables on Heroku.
My solution was to use
strict_encode64
:add the key:
In the code, you then decode it using
Base64.strict_decode64(ENV['SECRET_KEY'])
如何使用 NodeJS 处理此问题的示例。通过用
\n
替换\\n
字符来清理该值:取自:
使用 Firebase privateKey 作为 Heroku 配置变量来转义问题
An example of how to deal with this problem using NodeJS. Sanitize the value by replacing
\\n
characters with\n
:Taken from:
Escaping issue with firebase privateKey as a Heroku config variable
我知道这是一种非常手动的方法,但对我有用的是将私钥粘贴到文本编辑器中,找到并用实际的换行符替换
\n
并将其粘贴为Heroku 中环境变量的值。I know this is a very manual way of doing it, but what worked for me is to paste the private key into a text editor, find and replace
\n
with an actual line break and paste that as the value for the env var in Heroku.