Ruby on Rails:能否将 Ruby 代码放入 YAML 配置文件中?

发布于 2024-09-10 02:19:25 字数 189 浏览 6 评论 0原文

我想在我的 amazon_s3.yml 配置文件中执行类似的操作:

access_key_id: ENV['S3_KEY']
secret_access_key: ENV['S3_SECRET']

...但我知道这不起作用。不确定是否可行,但是您可以将 Ruby 代码放入 YAML 文件中吗?

I would like to do something like this in my amazon_s3.yml config file:

access_key_id: ENV['S3_KEY']
secret_access_key: ENV['S3_SECRET']

...but I know this isn't working. Not sure if it is even possible, but can you put Ruby code in a YAML file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

通常不/直接。我这样说是因为为了使用 ruby​​ 结果,您需要在加载文件之前首先使用 ERB 之类的东西。就代码而言,您需要从以下内容开始:

loaded_data = YAML.load_file("my-file.yml")

甚至

loaded_data = YAML.load(File.read("my-file.yml"))

到:

loaded_data = YAML.load(ERB.new(File.read("my-file.yml")).result)

在这种特定情况下,您必须查看加载文件的内容 - 在某些情况下,
它可能已经设计为直接从环境中加载它,或者您可能需要:

  1. Monkey 修补代码
  2. Fork + 使用您的自定义版本。

由于有一些插件使用 amazon_s3.yml,因此可能值得发布您正在使用的哪个库使用它 - 或者,我相信通过快速谷歌,AWS 库允许您将 AMAZON_ACCESS_KEY_ID 和 AMAZON_SECRET_ACCESS_KEY 定义为环境变量,并且它将从盒子里取出它们。

Not normally / directly. I say this because in order to use ruby results you need to use something like ERB first before loading the file. In terms of code, you need to go from something like:

loaded_data = YAML.load_file("my-file.yml")

Or even

loaded_data = YAML.load(File.read("my-file.yml"))

To:

loaded_data = YAML.load(ERB.new(File.read("my-file.yml")).result)

In this specific case, you would have to look at what is loading the file - in some cases,
it may already be designed to load it straight out of the environment or you may need to either:

  1. Monkey Patch the code
  2. Fork + Use your custom version.

Since there are a few plugins that use amazon_s3.yml, It might be worth posting which library you are using that uses it - alternatively, I believe from a quick google that the AWS library lets you define AMAZON_ACCESS_KEY_ID and AMAZON_SECRET_ACCESS_KEY as env vars and it will pick them up out of the box.

审判长 2024-09-17 02:19:25

如果它是通过 ERB 解释的,则可以,在这种情况下,它的作用类似于 ERB 视图,并且 Ruby 代码位于 <%%> 之间。

尝试:

access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>

... 和看看是否有效

You can if it is interpreted through ERB, in which case it acts like an ERB view and Ruby code goes between <% and %>

Try:

access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>

... and see if it works

挽清梦 2024-09-17 02:19:25

使用 fd. 的示例,如果您的应用程序配置为使用 HAML,请尝试使用字符串插值替换 ERB 语法。例如,:

access_key_id: #{ENV['S3_KEY']}
secret_access_key: #{ENV['S3_SECRET']}

而不是:

access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>

Using fd.'s example, try swapping out the ERB syntax with string interpolation if your application is configure to use HAML. E.g.,:

access_key_id: #{ENV['S3_KEY']}
secret_access_key: #{ENV['S3_SECRET']}

instead of:

access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>
浅紫色的梦幻 2024-09-17 02:19:25

对我来说,无需任何额外代码即可使用(Rails 4.2):

default: &default
  adapter: <%= 'mysql2' %>

Works like a charm for me without any additional code (Rails 4.2):

default: &default
  adapter: <%= 'mysql2' %>
清引 2024-09-17 02:19:25

在 Rails 4.2 中,使用 ERB 语法将评估代码并返回字符串。

# environment variables
S3_KEY=01234
S3_SECRET=56789

# yaml file
access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>

# then you can do
ENV.fetch('access_key_id')
 => "01234"
ENV.fetch('secret_access_key')
 => "56789"

您还可以在 YAML 文件的字符串中编写 ruby​​ 代码,然后对其进行评估

# yaml file
retry_interval: '5.minues'

# then you can do
eval(ENV.fetch('retry_interval'))
 => 300 seconds

注意:使用 eval 时要非常小心,因为它可能会带来严重的安全风险

In rails 4.2 using ERB syntax will evaluate the code and return strings.

# environment variables
S3_KEY=01234
S3_SECRET=56789

# yaml file
access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>

# then you can do
ENV.fetch('access_key_id')
 => "01234"
ENV.fetch('secret_access_key')
 => "56789"

You can also write ruby code in a string in your YAML file and then evaluate it later

# yaml file
retry_interval: '5.minues'

# then you can do
eval(ENV.fetch('retry_interval'))
 => 300 seconds

CAUTION: be very careful when using eval as it can post a serious security risk

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文