Reputation: 12593
I am trying to upload a file to S3 with the following simple code:
bucket.objects.create("sitemaps/event/#{file_name}", open(file))
I get the following:
Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.
What could be going wrong? Any tips will be appreciated.
Upvotes: 3
Views: 3243
Reputation: 6528
This timeout is generally happens when the content length could not be correctly determined based on the opened file. S3 is waiting for additional bytes that aren't coming. The fix is pretty simple, just open your file in binary mode.
Ruby 1.9
bucket.objects.create("sitemaps/event/#{file_name}", open(file, 'rb', :encoding => 'BINARY'))
Ruby 1.8
bucket.objects.create("sitemaps/event/#{file_name}", open(file, 'rb'))
The aws-sdk gem will handle this for you if you pass in the the path to the file:
# use a Pathname object
bucket.objects.create(key, Pathname.new(path_to_file))
# or just the path as a string
bucket.objects.create(key, :file => path_to_file)
Also, you can write to an object in s3 before it exists, so you could also do:
# my favorite syntax
obj = s3.buckets['bucket-name'].objects['object-key'].write(:file => path_to_file)
Hope this helps.
Upvotes: 7
Reputation: 2728
Try modifying the timeout parameters and see if the problem persists.
From the AWS website: http://aws.amazon.com/releasenotes/5234916478554933 (New Configuration Options)
# the new configuration options are:
AWS.config.http_open_timeout #=> new session timeout (15 seconds)
AWS.config.http_read_timeout #=> read response timeout (60 seconds)
AWS.config.http_idle_timeout #=> persistant connections idle longer are closed (60 seconds)
AWS.config.http_wire_trace #=> When true, HTTP wire traces are logged (false)
# you can update the timeouts (with seconds)
AWS.config(:http_open_timeout => 5, :http_read_timeout => 120)
# log to the rails logger
AWS.config(:http_wire_trace => true, :logger => Rails.logger)
# send wire traces to standard out
AWS.config(:http_wire_trace => true, :logger => nil)
Upvotes: 1