2014-08-28 45 views
1

文件上傳這是問題我最近的延續 - Stack Level Too Deep error - produced with strong parameters I thinkExconn ::錯誤:: SocketError在通過Carrierwave和霧

每當我做了Post創作,其中涉及文件上傳,我得到這個錯誤:

Started POST "/posts" for 127.0.0.1 at 2014-08-28 08:47:09 -0500 
Processing by PostsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"daUAMfiQZ/uiD/0ADg=", "post"=>{"status"=>"confirmed", "title"=>"Ashlee lost 10 pounds in 5 weeks", "photo"=>#<ActionDispatch::Http::UploadedFile:0x000001038f04b8 @tempfile=#<Tempfile:/var/folders/0f/hgplttnd7dg6q9m62qtbnpn00000gn/T/RackMultipart20140828-89271-qwxck1>, @original_filename="Ashlee-Testimonial.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[photo]\"; filename=\"Ashlee-Testimonial.png\"\r\nContent-Type: image/png\r\n">, "body"=>"She lost 10 pounds in 5 weeks doing 10PP."}, "commit"=>"Submit"} 
    User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 
    (0.4ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] 
    (0.2ms) BEGIN 
    SQL (2.2ms) INSERT INTO "posts" ("body", "created_at", "photo", "status", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["body", "She lost 10 pounds in 5 weeks doing 10PP."], ["created_at", "2014-08-28 13:47:09.320765"], ["photo", "Ashlee-Testimonial.png"], ["status", "confirmed"], ["title", "Ashlee lost 10 pounds in 5 weeks"], ["updated_at", "2014-08-28 13:47:09.320765"], ["user_id", 1]] 
Digest::Digest is deprecated; use Digest 
    (0.3ms) ROLLBACK 
Completed 500 Internal Server Error in 10904ms 

Excon::Errors::SocketError - Broken pipe: 

Post模式是這樣的:

# == Schema Information 
# 
# Table name: posts 
# 
# id   :integer   not null, primary key 
# status  :string(255) 
# title  :string(255) 
# date  :datetime 
# photo  :string(255) 
# body  :text 
# created_at :datetime 
# updated_at :datetime 
# user_id :integer 
# ancestry :string(255) 
# file  :string(255) 
# 

class Post < ActiveRecord::Base 
    has_ancestry 
    belongs_to :user 
    resourcify 

    mount_uploader :photo, ImageUploader 
    mount_uploader :file, FileUploader 
end 

我的控制器看起來是這樣的:

def create 
    @post = current_user.posts.new(post_params) 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
     format.json { render :show, status: :created, location: @post } 
     else 
     format.html { render :new } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    private 
    # Never trust parameters from the scary internet, only allow the white list through. 
    def post_params 
     params.require(:post).permit(:status, :title, :date, :photo, :body, :parent_id) 
    end 

這是我image_uploader.rb

# encoding: utf-8 

class ImageUploader < CarrierWave::Uploader::Base 
    include CarrierWave::RMagick 
    storage :fog 

    include CarrierWave::MimeTypes 
    process :set_content_type 

    def store_dir 
    "images/#{model.id}-#{model.created_at}" 
    end 

    version :thumb do 
    process :resize_to_fit => [80, 80] 
    end 

    version :large do 
    process :resize_to_limit => [400, 400] 
    end 

    def extension_white_list 
    %w(jpg jpeg gif png) 
    end 

end 

這是我的Gemfile:

source 'https://rubygems.org' 

gem 'rails', '4.1.1' 

group :assets do 
    gem 'sass-rails', '~> 4.0.3' 
    gem 'uglifier', '>= 1.3.0' 
    gem 'coffee-rails', '~> 4.0.0' 
    gem "font-awesome-rails" 
    gem 'bootstrap-sass', '~> 3.2.0' 
    gem 'autoprefixer-rails' 
end 

group :development do 
    gem 'annotate', github: 'ctran/annotate_models' 
    gem 'sextant' 
    gem "quiet_assets", ">= 1.0.2" 
    gem 'better_errors', '~> 1.1.0' 
    gem 'binding_of_caller', '~> 0.7.2' 
    gem 'meta_request' 
    gem 'execjs' 
    gem 'therubyracer' 
    gem "letter_opener" 
    gem 'bullet' 
    gem 'rack-mini-profiler'  
    gem 'guard-rails' 
    gem 'rb-fchange', :require=>false 
    gem 'rb-fsevent', :require=>false 
    gem 'rb-inotify', :require=>false 
    gem 'guard-livereload', '~> 2.3.0', :require=>false 
    gem 'rack-livereload', '~> 0.3.15' 
end 

group :production do 
    gem 'rails_12factor' 
end 

gem 'jquery-rails' 
gem 'turbolinks' 
gem 'jbuilder', '~> 2.0' 
gem 'sdoc', '~> 0.4.0',   group: :doc 
gem 'spring',  group: :development 
gem 'devise', '~> 3.2.4' 
gem 'thin' 
gem 'pg' 
gem 'cancancan', '~> 1.8.2' 
gem 'rolify' 
gem 'rmagick', :require => 'RMagick' 
gem "mini_magick" 
gem 'carrierwave', '~> 0.10.0' 
gem "fog", "~> 1.3.1" 
gem 'figaro', '~> 0.7.0' 
gem 'geocoder', '~> 1.2.2' 
gem 'social-share-button', '~> 0.1.6' 
gem 'ancestry', '~> 2.1.0' 
gem "simple_form" 

這是我上傳的形式分:

<%= simple_form_for @post do |f| %> 
    <%= f.error_notification %> 

    <%= f.input :parent_id, as: :hidden %> 

    <% if can? :manage, @post %> 
     <%= f.input :status, collection: Status.all %> 
    <% end %>  

    <%= f.input :title %><br /> 
    <%= f.input :date %><br />  
    <%= f.input :photo %><br /> 
    <%= f.input :body %><br />  

    <%= f.button :submit %> 

<% end %> 

這是我config/carrierwave.rb文件:

CarrierWave.configure do |config| 
    config.fog_credentials = { 
    provider:    'AWS', 
    region:     'us-east-1',       
    aws_access_key_id:  ENV["AWS_ACCESS_KEY"],   
    aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],   
    } 
    config.fog_directory = ENV["AWS_MY_BUCKET"]  
end 

任何想法可能導致這種情況?

更新1

下面是一些更多的調試信息。一旦我刪除了圖像的大小調整,該Socket錯誤消息就消失了。

但是,這是新的錯誤,我得到:

Digest::Digest is deprecated; use Digest 
    (0.2ms) ROLLBACK 
Completed 500 Internal Server Error in 7372ms 

Excon::Errors::Forbidden - Expected(200) <=> Actual(403 Forbidden) 
    request => {:connect_timeout=>60, :headers=>{"Content-Length"=>225260, "Content-Type"=>"image/png", "x-amz-acl"=>"public-read", "Date"=>"Thu, 28 Aug 2014 16:34:11 +0000", "Authorization"=>"AWS PUBLIC_KEY:SECRET_KEY=", "Host"=>"my_site.s3.amazonaws.com:443"}, :instrumentor_name=>"excon", :mock=>false, :read_timeout=>60, :retry_limit=>4, :ssl_ca_file=>"/.rvm/gems/[email protected]_site/gems/excon-0.13.4/data/cacert.pem", :ssl_verify_peer=>true, :write_timeout=>60, :host=>"my_site.s3.amazonaws.com", :path=>"/images%2F7-2014-08-28+16%3A34%3A11+UTC%2F10pp-main-banner.png", :port=>"443", :query=>nil, :scheme=>"https", :body=>#<File:/myapp/public/uploads/tmp/1409243651-91536-3147/10pp-main-banner.png>, :expects=>200, :idempotent=>true, :method=>"PUT"} 
    response => #<Excon::Response:0x000001035f0f88 @body="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><RequestId>2FDE1E67B32981B7</RequestId><HostId>itxnvlzNTkJ29N3MtyYmL8EP29zrW6s9Hr6Xc2P7QzTElkB56OU26WdzsE/6vSz1</HostId><AWSAccessKeyId>AKIAISIFEOAKO3CNPZTA</AWSAccessKeyId></Error>", @headers={"x-amz-request-id"=>"2FDE1E67B32981B7", "x-amz-id-2"=>"itxnvlzNTkJ29N3MtyYmL8EP29zrW6s9Hr6Xc2P7QzTElkB56OU26WdzsE/6vSz1", "Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked", "Date"=>"Thu, 28 Aug 2014 16:34:16 GMT", "Connection"=>"close", "Server"=>"AmazonS3"}, @status=403>: 
+0

也許這樣? http://stackoverflow.com/a/18754787/2128691 – dax 2014-08-28 14:17:36

+0

@dax我試過......這就是爲什麼'我東1'在我的'Carrierwave.rb'文件中指定。另外....'我 - 東1'是美國標準......據我所知。所以總之......你......我已經這麼做了。 – marcamillion 2014-08-28 14:19:53

+0

啊好吧對不起,沒有看到。 – dax 2014-08-28 14:26:23

回答

1

我想通了。發生了什麼後,我在這個應用程序的AWS IAM中創建了一個新用戶,並且我沒有爲該用戶提供適當的憑據。即我從未創建安全策略或將其分配給組。所以它產生了一個403錯誤。

現在我已經修好了,一切正常。

我希望這可以幫助別人。

2

我認爲這可能與語言環境後做所有。

下面是一個使用s3的人的例子,但它是一樣的想法。 they說:

I was recently working on my website and ran into a problem uploading larger files (like 1.8M) to S3 via Carrierwave/Fog. I was getting the following error: Excon::Errors::SocketError (Broken pipe (Errno::EPIPE)):

What I found out was that the region code needed to be set in my carrierwave.rb initializer file in order to get the larger files uploaded. Apparently it defaults to some region code other than the one I was using and that for some reason leads to an error when uploading larger files.

To find the region code, navigate through S3 to an actual file that has been stored and click on it and click 「Properties」 on the upper right of the screen. You will see something like the following as the link:

https://s3-us-west-2.amazonaws.com/ginger2/uploads/web_developer_project_image/image/1/thumb_Screen_Shot_2013-09-05_at_2.10.45_PM.png

In this case, the following is the region code you should set in your config: us-west-2

+0

我傾向於同意你的看法。問題是我無法弄清楚,我嘗試了'us-west-2',但這不起作用。當我登錄我的AWS賬戶並查看存儲桶時,我看到了'Endpoint:my_site.s3-website-us-east-1.amazonaws.com',這是我原來的,它給了我同樣的錯誤。 – marcamillion 2014-08-28 16:17:03

+0

感謝您的幫助。我想到了。發生了什麼後,我在這個應用程序的AWS IAM中創建了一個新用戶,並且我沒有爲該用戶提供適當的憑據。即我從未創建安全策略或將其分配給組。所以它產生了一個403錯誤。現在我已經解決了這個問題,一切順利。如果您將此添加爲答案或修改您的答案,我會接受它。謝謝你的一切! – marcamillion 2014-08-28 23:24:31

相關問題