S3,Fog,Figaro,Carrierwave:Excon :: Errors :: Forbidden(Expected(200)< => Actual(403 Forbidden)

时间:2015-05-29 23:27:09

标签: ruby-on-rails heroku amazon-s3 carrierwave figaro-ruby

我正在使用Carrierwave / Fog / S3和Figaro为yelp风格的网站上传图片。

整个网站在本地运行得很漂亮,没问题。在heroku上,它也很有效,直到我创建一个带有图像的新餐厅或尝试更新现有餐厅的图像,然后我在我的日志中得到这个:

 Excon::Errors::Forbidden (Expected(200) <=> Actual(403 Forbidden)  

这是我的image_uploader.rb:

# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  if Rails.env.production?
    storage :fog
  else
    storage :file
  end

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  process :resize_to_fit => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fit => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

  end

这是我在更新和新文件中呈现的_form.html.erb:

<%= form_for(@restaurant, :html => {multipart: true}) do |f| %>
  <% if @restaurant.errors.any? %>
    <div id="error_explanation" class= "alert alert-danger alert-dismissable">
    <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
      <h4><%= pluralize(@restaurant.errors.count, "error") %> prohibited this restaurant from being saved:</h4>

      <ul>
      <% @restaurant.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

 <div class="form-group">
    <%= f.label :name %><br>
    <%= f.text_field :name, class: "form-control", placeholder: "Panera Bread" %>
  </div>
  <div class="form-group">
    <%= f.label :address %><br>
    <%= f.text_field :address, class: "form-control", placeholder: "201 Brookline Avenue, Boston, MA 02215" %>
  </div>
  <div class="form-group">
    <%= f.label :phone %><br>
    <%= f.text_field :phone, class: "form-control", placeholder: "(617) 247-0174" %>
  </div>
  <div class="form-group">
    <%= f.label :website %><br>
    <%= f.text_field :website, class: "form-control", placeholder: "http://www.panerabread.com/" %>
  </div>
  <div class="form-group">
    <%= f.label :image %><br>
    <%= f.file_field :image, class: "form-control" %>
  </div>
  <div class="actions">
    <%= f.submit class: "btn btn-primary" %>
  </div>
<% end %>

我检查了钥匙以确保它们是正确的。 我为我的密钥添加了管理员访问权限策略(每this questionthis one)。 我已经检查了权限,但它们都很好。

到目前为止,睡在它上面并没有奏效(按this question。)

有什么想法吗?

提前致谢!!

2 个答案:

答案 0 :(得分:1)

我可能会离开,但请确保您运行“heroku run rake db:migrate”以在生产中进行数据库迁移,然后确保运行“heroku restart”。我不确定这将是你问题的答案,但是“heroku restart”在经历了长时间的挣扎之后帮助了我。

答案 1 :(得分:0)

我可以看到为什么从AWS获得403的两个潜在原因:

  1. AWS没有正确设置权限(但您已经检查过,并且您可以从您的计算机上传图片,因此不太可能)
  2. 您从heroku实例传递给AWS的数据是错误的。
  3. 您可以附加一个设置S3存储桶名称和密钥的文件吗?您是否尝试运行Rails控制台并打印出配置的值? (您可以运行heroku run bash并在连接到heroku dyno bundle exec rails console之后,瞧,您可以访问您的生产服务器。

    您还写道,有时上传图片有效,但在创建新餐厅或更新现有餐馆时却没有。这些案件是什么?看看是什么让他们变得如此不同(并且在heroku上工作)可能会很有趣?