简体   繁体   中英

Paperclip wont upload image to S3

I am having problems uploading images to Amazon S3. I have installed the paperclip gem and the gem 'aws-s3'. My Amazon S3 server is the europen: http://s3-eu-west-1.amazonaws.com

When I submit my form the form is rendered again because the photographer is not saved. And I can see that nothing is uploaded to the cloud. And I get the message "Image ok" in view.

My controller:

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     

My model:

    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'
    }

My create page:

<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 %>

My linkform:

<%=  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 console log:

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)

The AWS-S3 gem works fine with European S3 buckets.

If you add this to the enviroment.rb:

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

If you are using a newer version of paperclip you need to be using the aws-sdk gem not the aws-s3 gem. I had the same issue.

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

AWS-S3 doesn't work with European S3 buckets - you need to use a US bucket as the structures are different.

Carrierewave/Fog work fine with European buckets though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM