Paperclip不会将图像上传到S3

时间:2011-09-21 21:30:15

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

我在向Amazon S3上传图片时遇到问题。我已经安装了paperclip gem和gem'aws-s3'。我的Amazon S3服务器是欧洲:http://s3-eu-west-1.amazonaws.com

当我提交表格时,表格会再次呈现,因为摄影师没有保存。我可以看到没有任何内容上传到云端。我在视图中收到了“图片确定”的消息。

我的控制器:

def savenew
    @photographer = Photographer.create(params[:photographers]) 
    @photographer.sort_order = Photographer.count + 1

    if @photographer.save   
      redirect_to :action => 'list', :id => params[:id] 
    else    
      render 'create', :id => params[:id]             
    end                                 
end     

我的模特:

    attr_accessor :flv_file_size, :flv_updated_at, :flv_content_type, :image_file_size, :image_updated_at, :image_content_type,
    :quicktime_file_size, :quicktime_updated_at, :quicktime_content_type

  has_attached_file :image,
    :storage => :s3,
    :bucket => 'mybucket',
    :path => "/photographer/image/:id/:filename",
    :s3_credentials => {
      :access_key_id => 'mykey',
      :secret_access_key => 'mykey'
    }

  has_attached_file :flv,
    :storage => :s3,
    :bucket => 'mybucket',
    :path => "/photographer/flv/:id/:filename",
    :s3_credentials => {
      :access_key_id => 'mykey',
      :secret_access_key => 'mykey'
    }

      has_attached_file :quicktime,
    :storage => :s3,
    :bucket => 'mybucket',
    :path => "/photographer/quicktime/:id/:filename",
    :s3_credentials => {
      :access_key_id => 'mykey',
      :secret_access_key => 'mykey'
    }

我的创建页面:

<h2>Movie</h2>

<h3>Create new movie</h3>

<%= link_to 'back to movie list', {:action => 'list', :id => params[:id]}, {:class => "margin-left"} %>

<div class="spacer">&nbsp;</div>
<%#= start_form_tag(:action => "savenew", :id => params[:id]) %>
<%= form_tag( {:action => "savenew", :id  => params[:id] },  :multipart => true ) do |f|%>
<%= render(:partial => "linkform", :object => @photographer) %>      



<p>
   <label for="submit">&nbsp;</label>
<%= submit_tag("create new") %>
</p>
<% end %>

我的链接形式:

<%=  javascript_include_tag "tiny_mce/tiny_mce" %>
<%=  javascript_include_tag "init_my_tiny_mce" %>
<p>
<label for="hyperlink:">name</label>
    <%= text_field("photographer", "name") %>
</p>

<p>
<label for="hyperlink:">text </label>
    <%= text_area("photographer", "text", :rows => 12) %>
</p>
<p>
<label for="hyperlink:">Upload/update image</label>
    <%= file_field("photographer", "image") %>
</p>

<% if @photographer %>
    <% if @photographer.image %>
    <p class="ok">
    <label for="dd">image ok</label>    
    <%= image_tag("/photographer/image/#{@photographer.id}/#{@photographer["image"]}", :size => "39x22" ) %>        
    </p>        
    <p>
        <label for="sdd">&nbsp;</label>
        <%= link_to("remove", {:action => "remove_image", :id => @photographer.id }, {:confirm => "Are your sure?"}) %>
    </p>
    <% else %> 
    <p  class="current">
    <label for="dd"></label>
    No image uploaded for movie
    </p>
    <% end %>
      <br />
 <% end %>     

<br /> 
<p>
<label for="hyperlink:">Upload/update flv</label>
    <%= file_field("photographer", "flv") %>
</p>
<br />
<% if @photographer %>
    <% if @photographer.flv %>
    <p class="ok">
    <label for="dd">flv: <%= @photographer["flv"]%></label>
    <%= link_to("remove", {:action => "remove_flv", :id => @photographer.id }, {:confirm => "Are your sure?"}) %>
    </p>        
    <% else %> 
    <p  class="current">    
    No flv uploaded 
    <br /><br />
    </p>
    <% end %>
<% end %>
<br /> 
<p>
<label for="hyperlink:">Upload/update quicktime</label>
    <%= file_field("photographer", "quicktime") %>                 
</p>
<br />
<% if @photographer %>
    <% if @photographer.quicktime %>    
    <p class="ok">
    <label for="dd">quicktime:  <%= @photographer["quicktime"]%></label>
    <%= link_to("remove", {:action => "remove_quicktime", :id => @photographer.id }, {:confirm => "Are your sure?"}) %>
    </p>
    <% else %> 
    <p  class="current">
    <label for="dd"></label>
    No quicktime uploaded 
    <br />
    </p>
    <% end %>
<% end %>
<%= error_messages_for("photographer")%>

My Rails控制台日志:

Started POST "/admin/photographers/savenew" for 127.0.0.1 at 2011-09-21 23:32:40
 +0200
  Processing by Admin::PhotographersController#savenew as HTML
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"LDd/jde1bzb7NqaNfzDKHjMc4j4
PHdjflgSYVGL3z+k=", "photographer"=>{"name"=>"sadasdasd", "text"=>"asd", "image"
=>#<ActionDispatch::Http::UploadedFile:0x59ad680 @original_filename="IT_T_rgb.jp
g", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\
"photographer[image]\"; filename=\"IT_T_rgb.jpg\"\r\nContent-Type: image/jpeg\r\
n", @tempfile=#<File:C:/Users/Iceberg/AppData/Local/Temp/RackMultipart20110921-6
200-o7ckpi>>}, "commit"=>"create new"}
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mBEGIN←[0m
  ←[1m←[35mSQL (1.0ms)←[0m  ROLLBACK
  ←[1m←[36mSQL (1.0ms)←[0m  ←[1mSELECT COUNT(*) FROM `photographers`←[0m
  ←[1m←[35mSQL (0.0ms)←[0m  BEGIN
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mROLLBACK←[0m
Rendered admin/photographers/_linkform.rhtml (5.0ms)
  ←[1m←[35mTextpage Load (1.0ms)←[0m  SELECT `textpages`.* FROM `textpages` ORDE
R BY name ASC
Rendered admin/photographers/create.rhtml within layouts/admin (105.0ms)
Completed 200 OK in 190ms (Views: 119.0ms | ActiveRecord: 3.0ms)

3 个答案:

答案 0 :(得分:1)

AWS-S3 gem适用于欧洲S3存储桶。

如果将其添加到enviroment.rb:

AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com"

答案 1 :(得分:0)

AWS-S3不适用于欧洲S3存储桶 - 您需要使用美国存储桶,因为结构不同。

Carrierewave / Fog与欧洲水桶合作很好。

答案 2 :(得分:0)

如果您使用的是较新版本的回形针,则需要使用aws-sdk gem而不是aws-s3 gem。我有同样的问题。

https://github.com/thoughtbot/paperclip#storage