Paperclip 无法将图像上传到 S3
我在将图像上传到 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"> </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 %>
我的链接表单:
<%= 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")%>
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AWS-S3 gem 与欧洲 S3 存储桶配合良好。
如果将其添加到enviroment.rb中:
The AWS-S3 gem works fine with European S3 buckets.
If you add this to the enviroment.rb:
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.
如果您使用的是较新版本的回形针,则需要使用 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