0

我正在使用 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 %>

我检查了钥匙以确保它们是正确的。我向我的密钥添加了管理员访问策略(根据这个问题这个问题)。我检查了权限,它们都很好。

到目前为止,睡在上面还没有奏效(根据这个问题。)

有任何想法吗?

提前致谢!!

4

2 回答 2

1

我可能会走得更远,但请确保您运行“heroku run rake db:migrate”以在生产中进行数据库迁移,然后确保运行“heroku restart”。我不确定这是否可以解决您的问题,但是在为此苦苦挣扎了很长时间之后,“heroku 重启”帮助了我。

于 2015-07-17T20:28:09.113 回答
0

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

  1. AWS 没有正确设置权限(但您已经检查过并且您能够从您的机器上传图像,所以不太可能)
  2. 您从 heroku 实例传递给 AWS 的数据是错误的。

您能否附加一个设置 S3 存储桶名称和密钥的文件?您是否尝试运行 Rails 控制台并打印出配置的值?(您可以运行heroku run bash并在连接到 heroku dyno 之后bundle exec rails console,瞧,您可以访问您的生产服务器)。

您还写道,有时上传图片会起作用,但在创建新餐厅或更新现有餐厅时却不行。那些案例是什么?看看是什么让它们如此不同(以及在 heroku 上工作)可能会很有趣?

于 2015-07-21T03:55:23.657 回答