如何在本地定义环境变量而不更改它们的定义/将它们推送到heroku?

发布于 2025-01-08 06:20:29 字数 635 浏览 1 评论 0原文

为了将我的应用程序连接到 amazon s3 和其他服务,我必须处理许多凭据。

我用 s3 启动并运行了我的 heroku 应用程序,它运行得很好。我按照以下示例定义了 s3 访问凭据: http://devcenter.heroku.com/articles/config -vars

但是,我现在希望能够从本地开发环境访问 s3。显然,我在 heroku 上定义的配置变量在我的本地主机上不可用。如何在本地定义这些键?另外,我正在特别寻找一种安全的解决方案(例如,如果我在初始化程序或其他内容中以纯文本方式定义密钥,我不希望将该文件推送到 heroku 上)。

作为背景,这是我添加到模型中以使回形针与 s3 一起运行的内容

 has_attached_file :photo,
:storage => :s3,
:bucket => 'bucket_name',
:s3_credentials => {
  :access_key_id => ENV['S3_KEY'],
  :secret_access_key => ENV['S3_SECRET']
}

I have many credentials that I have to handle in order to hook my app up to amazon s3 and other services.

I got my heroku app up and running with s3, and it works great. I defined my s3 access credentials following this example: http://devcenter.heroku.com/articles/config-vars

However, I want now to be able to have access to s3 from my local development environment. Obviously, the config vars that I defined on heroku aren't available on my localhost. How can I define these keys locally? Also, I'm looking in particular for a solution that is secure (for example if I define my keys in plain text in an intializer or something, I don't want that file to be pushed on heroku).

For background, here is what I add to my model to get paperclip running with s3

 has_attached_file :photo,
:storage => :s3,
:bucket => 'bucket_name',
:s3_credentials => {
  :access_key_id => ENV['S3_KEY'],
  :secret_access_key => ENV['S3_SECRET']
}

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

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

发布评论

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

评论(3

ら栖息 2025-01-15 06:20:29

如果您不希望共享此类内容,定义此类内容的最佳位置可能是初始化程序。

# config/initializers/s3_constants.rb


if Rails.env.development?
  S3_KEY = "mys3key"
  S3_SECRET = "mys3secret"
end

确保将此文件添加到 .gitignore,这样它就不会与存储库的其余部分一起推送。

实际上,每个环境不同的常量确实应该位于该环境的文件中(这里说development.rb)...但是这些文件也应该真正添加到您的版本控制系统中,如果您确定,肯定希望这些数据从 git 中排除,那么您不提交的单独文件可能是您最好的选择。

The best place to define stuff like this, if you don't want it shared, is probably an initializer.

# config/initializers/s3_constants.rb


if Rails.env.development?
  S3_KEY = "mys3key"
  S3_SECRET = "mys3secret"
end

Ensure this file is added to .gitignore so it won't be pushed along with the rest of your repository.

Realistically speaking, constants that differ on a per-environment basis should really be located in the file for that environment (say development.rb here)... but those files should also really be added to your version control system, and if you definitely, definitely want this data excluded from git, then a separate file that you do not commit is probably your best bet.

葬﹪忆之殇 2025-01-15 06:20:29

只需像任何其他环境变量一样在 .bash_profile 文件中定义环境变量即可。也许留下评论来将该部分划分为特定于 Rails 的环境变量。

#~/.bash_profile
# Rails constants
S3_KEY="blady"
S3_SECRET="bladybloo123"

另外,也许您想将名称更改为更具体的名称,以便可以定义多个 s3 连接...

Just define the environment variables in your .bash_profile file like any other environment variable. Maybe leave a comment to demarcate the section as Rails-specific environment variables.

#~/.bash_profile
# Rails constants
S3_KEY="blady"
S3_SECRET="bladybloo123"

Also, maybe you want to change the name to something more specific so that you can have more than one s3 connection defined...

空宴 2025-01-15 06:20:29

heroku 提供 heroku config:add 并且您提供 KEY=value。请参阅配置变量文档

heroku provides heroku config:add and you provide KEY=value. see config vars documentation

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