使用回形针上传到s3后图像未显示

时间:2013-01-25 02:53:14

标签: ruby-on-rails amazon-s3 paperclip

我试图找出为什么我的照片没有出现在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")  

Menuitem模型

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

New.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']
    }
  }

restaurant's 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 %>

1 个答案:

答案 0 :(得分:0)

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

我必须删除原始存储桶并使用新名称创建一个新存储桶,然后我必须使用

重置myoku配置中的aws_bucket变量
heroku config:add AWS_BUCKET=newbucketname

现在它完美无缺!