`fetch':找不到密钥:“S3_BUCKET_NAME” (KeyError)与paperclip / aws s3 / rails [英] `fetch': key not found: "S3_BUCKET_NAME" (KeyError) with paperclip/aws s3/rails

查看:549
本文介绍了`fetch':找不到密钥:“S3_BUCKET_NAME” (KeyError)与paperclip / aws s3 / rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在rails(4.2.3)中使用'aws-sdk',''找到'fetch':key not found:S3_BUCKET_NAME(KeyError) 〜> 2.3'和paperclip,〜> 5.0.0

我通过终端在我的环境中设置了密钥并运行 heroku config 显示它们列出。



在我的config / environments / development.rb以及在我的config production.rb中,我都包含了:

  config.paperclip_defaults = {
存储:: s3,
s3_credentials:{
存储桶:ENV.fetch('S3_BUCKET_NAME'),
access_key_id:ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key:ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region:ENV.fetch('AWS_REGION'),
}

我也在上面的代码中加入了我的user.rb模型,但为了

  has_attached_file:头像,
样式:{medium:300x300# ,thumb:100x100#},
:convert_options => {
:thumb => -quality 75 -strip},
:storage => :s3,
:s3_credentials => {
:bucket => ENV ['S3_BUCKET_NAME'],
:access_key_id => ENV ['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV ['AWS_SECRET_ACCESS_KEY'],
:region => ENV ['AWS_REGION']
},
:path => :filename。:extension,
#:path => :rails_root / public / system /:attachment /:id /:style /:filename,
:default_url => default_img.png

validates_attachment_content_type:avatar,content_type:/\Aimage\/.*\/

我也将env vars包含在我的secrets.yml中:

 开发: 
secret_key_base:817c07d41b8524495628fbe91fb1f0535ade65aa96a3fee379a8d16c29cc1f7b167f537442e547422ab17ee9700028a95896eb1c0717de06dfe7895d15ddb5ce
SECRET_KEY:sk_test_xxx
publishable_key:pk_test_xxx
access_key_id:XXX
secret_access_key:XXX
s3_bucket_name: '斗名'

test:
secret_key_base:a38e71848a4d9bc63fa8dce4522add10a4931b10e6786f0cab6a9eb1643e271b992f52fa6eff672b0d03687003099c0632477dd26b246ac4e637c52c69ec4ab0

#不要在存储库中保留生产机密,
#而是从环境中读取值。
产量:
secret_key_base:<%= ENV [SECRET_KEY_BASE]%>
secret_key:<%= ENV [SECRET_KEY]%>
publishable_key:<%= ENV [PUBLISHABLE_KEY]%>
access_key_id:<%= ENV ['AWS_ACCESS_KEY_ID']%>
secret_access_key:<%= ENV ['AWS_SECRET_ACCESS_KEY']%>
s3_bucket_name:<%= ENV [S3_BUCKET_NAME]%>

虽然这可能不是必需的。在搜索结果页面上搜索与此错误相关的其他几篇文章,这些内容让我无法继续前进 - 是否有人对此问题有任何想法?

解决方案

'fetch':找不到密钥:S3_BUCKET_NAME环境变量S3_BUCKET_NAME没有值。

在你的情况下,你正在使用Heroku。请按照以下链接中的说明操作。



对于Heroku: https://devcenter.heroku.com/articles/config-vars



如果您使用ENV ['variables'],您需要让他们在每一个环境中都能成立。测试,生产和发展。如果您正在使用PC,MAC或Linux进行开发,则需要确保设置了环境变量。



对于linux:
https:// www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps



对于OSX:
我这样做和linux一样,我只是通过添加一行来将它们设置在我的〜/ .zshrc 中。如果你不使用zshell,那么在你的〜/ .bashrc 〜/ .bash_profile 中添加一行。 / p>

  export ENV_VARIABLE_NAME =value

对于Windows:
我不知道如何,但我相信谷歌。

I am getting a 'fetch': key not found: "S3_BUCKET_NAME" (KeyError) error in rails (4.2.3) using 'aws-sdk', '~> 2.3' and "paperclip", "~> 5.0.0"

I have set the Keys in my environment via terminal and running heroku config shows them listed.

In both my config/environments/development.rb as well as in my config production.rb I have included:

config.paperclip_defaults = {
  storage: :s3,
  s3_credentials: {
    bucket: ENV.fetch('S3_BUCKET_NAME'),
    access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
    secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
    s3_region: ENV.fetch('AWS_REGION'),
  }
}

I have also included the above code in my user.rb model, but for the sake of reference it looks like this in the model:

has_attached_file :avatar, 
    styles: { medium: "300x300#", thumb: "100x100#" },
    :convert_options => {
    :thumb => "-quality 75 -strip" },
    :storage => :s3,
    :s3_credentials => {
      :bucket => ENV['S3_BUCKET_NAME'],
      :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
      :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
      :region => ENV['AWS_REGION']
    },
    :path => ":filename.:extension",
    # :path => ":rails_root/public/system/:attachment/:id/:style/:filename", 
    :default_url => "default_img.png"

    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

I have also included the env vars in my secrets.yml:

development:
  secret_key_base: 817c07d41b8524495628fbe91fb1f0535ade65aa96a3fee379a8d16c29cc1f7b167f537442e547422ab17ee9700028a95896eb1c0717de06dfe7895d15ddb5ce
  secret_key: sk_test_xxx
  publishable_key: pk_test_xxx
  access_key_id: xxx
  secret_access_key: xxx
  s3_bucket_name: 'bucket-name'

test:
  secret_key_base: a38e71848a4d9bc63fa8dce4522add10a4931b10e6786f0cab6a9eb1643e271b992f52fa6eff672b0d03687003099c0632477dd26b246ac4e637c52c69ec4ab0

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  secret_key: <%= ENV["SECRET_KEY"] %>
  publishable_key: <%= ENV["PUBLISHABLE_KEY"] %>
  access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  s3_bucket_name: <%= ENV["S3_BUCKET_NAME"]%>

although that may not have been required. Googling around and going through the few other posts on SO related to this error have given me little to go on - does anybody have any ideas on what the issue may be?

解决方案

'fetch': key not found: "S3_BUCKET_NAME" means that the environment variables S3_BUCKET_NAME does not have a value.

In your case you are using Heroku. Follow the instructions in the link below.

For Heroku: https://devcenter.heroku.com/articles/config-vars.

If you are using ENV['variables'] you need to have them set in every environment. test, production and development. If you are developing on a PC, MAC, or Linux you need to make sure the environment variables are set.

For linux: https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps

For OSX: I do it the same was as linux, I just set them in my ~/.zshrc by adding a line like this. If you aren't using zshell then add a line to your ~/.bashrc or ~/.bash_profile.

export ENV_VARIABLE_NAME="value"

For Windows: I don't know how, but I am sure google does.

这篇关于`fetch':找不到密钥:“S3_BUCKET_NAME” (KeyError)与paperclip / aws s3 / rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆