Ruby aws-sdk - 超时错误

时间:2012-10-30 23:48:26

标签: ruby-on-rails ruby ruby-on-rails-3 amazon-s3

我正在尝试使用以下简单代码将文件上传到S3:

bucket.objects.create("sitemaps/event/#{file_name}", open(file))

我得到以下内容:

在超时期限内未读取或写入与服务器的套接字连接。空闲连接将被关闭。

可能出现什么问题?任何提示将不胜感激。

2 个答案:

答案 0 :(得分:7)

当基于打开的文件无法正确确定内容长度时,通常会发生此超时。 S3正在等待未来的其他字节。修复非常简单,只需以二进制模式打开文件即可。

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'))

如果您传入文件的路径,aws-sdk gem将为您处理:

# 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)

此外,您可以在s3存在之前写入对象,因此您也可以这样做:

# my favorite syntax
obj = s3.buckets['bucket-name'].objects['object-key'].write(:file => path_to_file)

希望这有帮助。

答案 1 :(得分:1)

尝试修改超时参数,看看问题是否仍然存在。

来自AWS网站:http://aws.amazon.com/releasenotes/5234916478554933(新配置选项)

# 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)