1

我试图找出为什么我的照片没有出现在 S3 上。当我检查(不存在的)图片时,它显示了正确的亚马逊 s3 路径,但图像从未上传到 s3。当我在控制台中输入 heroku logs --tail 时,没有明显的错误。我也已经在 heroku 上设置了 s3 变量。heroku 日志看起来与我上传音乐文件的其他网站相同,但这些文件从未出现在 S3 上...

这是github页面,如果您喜欢在那里阅读的话。谢谢!

上传后的 Heroku 日志。我从我的另一个应用程序中看到的唯一明显区别是这个应用程序首先重定向,然后回形针保存附件。

Started POST "/restaurants/3/menuitems" for 68.38.119.33 at 2013-01-25 16:36:07 +0000
2013-01-25T16:36:07+00:00 app[web.1]: Processing by MenuitemsController#create as HTML
2013-01-25T16:36:07+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"I8iVlAdNa/ui96aflFAlPxfAIgmvOB4k/Y7Fhf5ElMI=", "menuitem"=>{"name"=>"Penne alla Vodkas", "description"=>"Fresh oven baked bread with a kiss of mother nature cut into fresh little delicious strips.", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000005ff55f0 @original_filename="penneallavodka.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"menuitem[image]\"; filename=\"penneallavodka.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20130125-2-1xa5s3v>>, "price"=>"10.95"}, "commit"=>"Add Item", "restaurant_id"=>"3"}
2013-01-25T16:36:11+00:00 app[web.1]: Redirected to http://sugarcider.com/restaurants/3
2013-01-25T16:36:11+00:00 app[web.1]: Completed 302 Found in 4129ms (ActiveRecord: 18.1ms)
2013-01-25T16:36:11+00:00 app[web.1]: [paperclip] Saving attachments.
2013-01-25T16:36:11+00:00 app[web.1]: [paperclip] saving penneallavodka.jpeg
2013-01-25T16:36:11+00:00 app[web.1]: [AWS S3 200 4.101325 0 retries] put_object(:acl=>:public_read,:bucket_name=>"restaurantimages",:content_length=>818276,:content_type=>"image/jpeg",:data=>#<Paperclip::UploadedFileAdapter:0x00000006322908 @target=#<ActionDispatch::Http::UploadedFile:0x00000005ff55f0 @original_filename="penneallavodka.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"menuitem[image]\"; filename=\"penneallavodka.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20130125-2-1xa5s3v>>, @tempfile=#<File:/tmp/penneallavodka20130125-2-mfgg7i.jpeg>>,:key=>"penneallavodka.jpeg")  

菜单项模型

class Menuitem < ActiveRecord::Base
  attr_accessible :name, :description, :price, :image

  belongs_to :restaurant

  has_attached_file :image,
    :styles => {
      :thumbnail => '100x101>',
      :regular => '560x568>'
    },
    :storage => :s3,
    :s3_credentials => {
      :bucket => ENV['AWS_BUCKET'],
      :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
      :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
    },
    :path => ":filename.:extension"
end

新的.html.erb

<%= form_for [@restaurant, @restaurant.menuitems.new], :html => { :multipart => true } do |f| %>
  <%= f.label :name %><br/>
  <%= f.text_field :name %><br/>
  <%= f.label :description %><br/>
  <%= f.text_field :description %><br/>
  <%= f.label :image %><br/>
  <%= f.file_field :image %><br/>
  <%= f.label :price %><br/>
  <%= f.text_field :price %><br/>
  <%= f.submit 'Add Item', :class => 'btn btn-large btn-success' %>
<% end %>

Menuitems_controller.rb

  def new
    @restaurant = Restaurant.find(params[:restaurant_id])
    @menuitem = @restaurant.menuitems.build
  end

  def create
    @restaurant = Restaurant.find(params[:restaurant_id])
    @menuitem = @restaurant.menuitems.build(params[:menuitem])
    respond_to do |format|
      if @menuitem.save
        format.html { redirect_to restaurant_path(@restaurant) }
      else
        format.html { redirect_to restaurant_path(@restaurant), notice: 'One or more fields are not correctly formatted.' }
      end
    end
  end

  def index
    @restaurant = Restaurant.find(params[:restaurant_id])
    @menuitems = @restaurant.menuitems.build
  end

我在production.rb中有这个

  config.paperclip_defaults = {
    :storage => :s3,
    :s3_protocol => 'http',
    :s3_credentials => {
      :bucket => ENV['AWS_BUCKET'],
      :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
      :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
    }
  }

餐厅的 index.html

  <% @menuitems.each do |f| %>
    <tr>
      <td><%= f.name %></td>
      <td><%= f.description %></td>
      <td>$<%= f.price %></td>
      <td><%= image_tag f.image.url(:regular) %></td>
      <td><%= link_to 'Destroy', restaurant_menuitem_path(f), :method => :delete, :class => 'btn btn-danger' %>
      <%= link_to 'Edit', edit_restaurant_menuitem_path(f), :class => 'btn btn-success'%></td>
    </tr>
  <% end %>
4

1 回答 1

0

好的,我解决了我自己的问题。

我必须删除我的原始存储桶并使用新名称创建一个新存储桶,然后我必须在 heroku 配置上重置我的 aws_bucket 变量

heroku config:add AWS_BUCKET=newbucketname

现在它完美地工作了!

于 2013-01-25T16:51:47.337 回答