【问题标题】:AWS S3 Errors when trying to authenticate with rails.application.credentials尝试使用 rails.application.credentials 进行身份验证时出现 AWS S3 错误
【发布时间】:2019-12-21 10:02:33
【问题描述】:

我设置了一个 S3 存储桶,并在我的 Rails 开发环境中使用它来存储我的活动存储文件。

当我的 storage.yml 文件中有以下内容时,它可以正常工作:

aws:
  service: S3
  access_key_id: myaccesskeyid
  secret_access_key: mysecretaccesskey
  region: eu-west-2
  bucket: bucketname

但是当我尝试使用 EDITOR="mate --wait" bin/rails credentials:edit 通过我的 credentials.yml 设置 access_key_id 和 secret_access_key 时,我收到以下错误:

Aws::S3::Errors::Forbidden in Pages#home

Completed 500 Internal Server Error in 311ms (ActiveRecord: 13.6ms)

ActionView::Template::Error ():
    12:     <% @items.each do |item| %>
    13:         <%= link_to item, class: 'col-6 col-md-4 col-lg-3 home_items__item' do %>
    14:             <div class="item-preview">
    15:                 <%= image_tag(item.feature_thumb, class:"img-fluid")%>
    16:                 <div class="item-preview__title">
    17:                     <%= item.title %>
    18:                 </div>

app/models/item.rb:45:in `feature_thumb'
app/views/pages/home.html.erb:15:in `block (2 levels) in _app_views_pages_home_html_erb__4520288450801053047_70162336881260'
app/views/pages/home.html.erb:13:in `block in _app_views_pages_home_html_erb__4520288450801053047_70162336881260'
app/views/pages/home.html.erb:12:in `_app_views_pages_home_html_erb__4520288450801053047_70162336881260'
Started PUT "/__web_console/repl_sessions/bfa895913555c494e18f3b259169a041" for ::1 at 2019-08-14 20:39:17 +0100

NoMethodError: undefined method `map' for nil:NilClass

我的 credentials.yml 如下所示:

aws:
  access_key_id: myaccesskeyid
  secret_access_key: mysecretaccesskey

我修改后的 storage.yml 如下所示:

aws:
  service: S3
  access_key_id: Rails.application.credentials.aws[:access_key_id]
  secret_access_key: Rails.application.credentials.aws[:secret_access_key]
  region: eu-west-2
  bucket: bucketname

并在出错的浏览器窗口中从控制台调用这些凭据会返回正确的凭据:

> Rails.application.credentials.aws[:access_key_id]
> "myaccesskeyid"
>  Rails.application.credentials.aws[:secret_access_key]
> "mysecretaccesskey"

关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby amazon-web-services amazon-s3 yaml


    【解决方案1】:

    这看起来不像是 AWS 存储桶问题。我将粘贴您提供的堆栈跟踪并突出显示关键点:

    app/models/item.rb:45:in feature_thumb' <----------------This is the key line app/views/pages/home.html.erb:15:inblock (2 个级别) in _app_views_pages_home_html_erb__4520288450801053047_70162336881260' app/views/pages/home.html.erb:13:in block in _app_views_pages_home_html_erb__4520288450801053047_70162336881260' app/views/pages/home.html.erb:12:in_app_views_pages_home_html_erb__4520288450801053047_70162336881260' 在 2019 年 8 月 14 日 20:39:17 +0100 开始为 ::1 放置“/__web_console/repl_sessions/bfa895913555c494e18f3b259169a041”

    NoMethodError: undefined method `map' for nil:NilClass

    这意味着您在 feature_thumb 方法的某处使用了 map 方法。除了你调用它的对象是nil。您需要确保仅当对象不为 nil 时才调用 map 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 2021-09-07
      • 2019-09-30
      • 2015-12-25
      相关资源
      最近更新 更多