2014-08-28 47 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