Paperclip 无法将图像上传到 S3

发布于 2024-12-06 00:12:43 字数 6193 浏览 4 评论 0原文

我在将图像上传到 Amazon S3 时遇到问题。我已经安装了回形针 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")%>

我的 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)

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"> </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"> </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"> </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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间☆小暴躁 2024-12-13 00:12:43

AWS-S3 gem 与欧洲 S3 存储桶配合良好。

如果将其添加到enviroment.rb中:

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

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"
绝對不後悔。 2024-12-13 00:12:43

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

Carrierewave/Fog 与欧洲水桶配合良好。

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.

几度春秋 2024-12-13 00:12:43

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文