【问题标题】:Ruby aws-sdk - timeout errorRuby aws-sdk - 超时错误
【发布时间】:2012-11-01 02:40:59
【问题描述】:

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

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

我得到以下信息:

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

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

【问题讨论】:

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


    【解决方案1】:

    此超时通常发生在无法根据打开的文件正确确定内容长度时。 S3 正在等待未到来的额外字节。修复很简单,只需以二进制模式打开文件即可。

    红宝石 1.9

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

    红宝石 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)
    

    希望这会有所帮助。

    【讨论】:

    • ruby 1.9 的修复与 2.0 相同吗?我在 2.0 上遇到超时错误问题。只有open(file) 在 1.9.3 上运行良好。这里提到的同样的问题github.com/aws/aws-sdk-ruby/issues/241
    【解决方案2】:

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

    来自 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2020-09-22
      • 2014-01-07
      相关资源
      最近更新 更多