简体   繁体   English

Ruby aws-sdk - 超时错误

[英]Ruby aws-sdk - timeout error

I am trying to upload a file to S3 with the following simple code: 我正在尝试使用以下简单代码将文件上传到S3:

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. 任何提示将不胜感激。

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. S3正在等待未来的其他字节。 The fix is pretty simple, just open your file in binary mode. 修复非常简单,只需以二进制模式打开文件即可。

Ruby 1.9 Ruby 1.9

bucket.objects.create("sitemaps/event/#{file_name}", open(file, 'rb', :encoding => 'BINARY'))

Ruby 1.8 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: 如果您传入文件的路径,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)

Also, you can write to an object in s3 before it exists, so you could also do: 此外,您可以在s3中存在之前写入对象,因此您还可以执行以下操作:

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

Hope this helps. 希望这可以帮助。

Try modifying the timeout parameters and see if the problem persists. 尝试修改超时参数并查看问题是否仍然存在。

From the AWS website: http://aws.amazon.com/releasenotes/5234916478554933 (New Configuration Options) 从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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM