【问题标题】:how to apply sync_clock option on S3 upload with CarrierWave in Ruby On Rails如何在 Ruby On Rails 中使用 CarrierWave 对 S3 上传应用 sync_clock 选项
【发布时间】:2013-10-24 07:06:44
【问题描述】:

由于请求时间与当前时间的差异太大,我每次都收到此错误。我发现我们需要应用 sync_clock 选项但无法配置地点。请参阅我的配置,请帮助我们配置同步时钟

错误:

  Expected(200) <=> Actual(403 Forbidden)
  request => {:headers=>{"Content-Length"=>54911, "Content-Type"=>"image/jpeg", "x-amz-acl"=>"public-read", "Cache-Control"=>"max-age=315576000", "Date"=>"Thu, 24 Oct 2013 01:14:14 +0000", "Authorization"=>"changed", "Host"=>"changed"}, :host=>"changed", :mock=>nil, :path=>"/uploads%2Fproject%2Fimage_1%2F697%2FHamburg-Speicher-im-Bau-090825.jpg", :port=>"443", :query=>nil, :scheme=>"https", :body=>#<File:/app/tmp/carrierwave/20131024-0114-2-7499/Hamburg-Speicher-im-Bau-090825.jpg>, :expects=>200, :idempotent=>true, :method=>"PUT"}
  response => #<Excon::Response:0x0000000b72f0a0 @body="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>RequestTimeTooSkewed</Code><Message>The difference between the request time and the current time is too large.</Message><MaxAllowedSkewMilliseconds>900000</MaxAllowedSkewMilliseconds><RequestId>EA8E8FF76B54D7F3</RequestId><HostId>/RiS+pn3JcKzOoArMhFRYmSBRYwRAptugp8W32XAT4vupukmxMCtHRKIHy7wy9BL</HostId><RequestTime>Thu, 24 Oct 2013 01:14:14 +0000</RequestTime><ServerTime>2013-10-24T01:29:49Z</ServerTime></Error>", @headers={"x-amz-request-id"=>"EA8E8FF76B54D7F3", "x-amz-id-2"=>"/RiS+pn3JcKzOoArMhFRYmSBRYwRAptugp8W32XAT4vupukmxMCtHRKIHy7wy9BL", "Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked", "Date"=>"Thu, 24 Oct 2013 01:29:47 GMT", "Connection"=>"close", "Server"=>"AmazonS3"}, @status=403>
  vendor/bundle/ruby/1.9.1/gems/excon-0.6.6/lib/excon/connection.rb:190:in `request' 

初始化器

  CarrierWave.configure do |config|
  if Rails.env.production?
    config.fog_directory  = 'ese-prod'
    config.fog_host       = 'https://s3.amazonaws.com/ese-prod'
  else
    config.fog_directory  = 'ese-dev'
    config.fog_host       = 'https://s3.amazonaws.com/ese-dev'
  end
  if Rails.env.production? || Rails.env.development?

    config.fog_credentials = {
       :provider               => 'AWS',
       :aws_access_key_id      => 'AAAAAAAAAAA',
       :aws_secret_access_key  => 'BBBBBBBBBBBB',
       :region                 => 'us-east-1'
     }
    config.fog_public     = true
    config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}

    config.root = Rails.root.join('tmp') # adding these...
    config.cache_dir = 'carrierwave' # ...two lines

  # elsif Rails.env.development?
  #  config.storage = :file
  else
    config.storage = :file
  end
end

上传者

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  if Rails.env.production? || Rails.env.development?
    storage :fog
  else
    storage :file
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process :resize_to_limit => [50, 50]
  end

  version :partner do
    process :resize_to_limit => [150, 150]
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

【问题讨论】:

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


    【解决方案1】:

    不幸的是,这是发生的事情。值得庆幸的是有一个修复。

    在初始化程序中,您应该能够执行以下操作:

    Fog::Storage.new(fog_credentials).sync_clock

    您应该能够在此处为雾凭据的初始化程序中使用传递给配置的相同值。 sync_clock 向 S3 发出一个简单的请求并存储偏移量(然后通过偏移量修改它发送的时间戳)。所以这应该确保你不会再看到这个错误(尽管它不应该经常出现,即如果你重新部署到heroku,新的测功机可能不会仍然有偏差)。希望可以解决问题,但如果需要,我们很乐意提供更多帮助。

    【讨论】:

    • 感谢您的重播,因为我们将我们的应用程序迁移到carrierwave-aws,所以它肯定会在功能上帮助我们
    • 谢谢,我花了两天多的时间来修复请求!
    • 救命答案
    【解决方案2】:

    S3 的响应意味着您请求中的“日期”标头不正确超过 15 分钟。你应该检查:

    1. 您的系统时间设置正确
    2. 您的时区设置正确

    另见:

    【讨论】:

    • 如何检查它的时区问题以及需要设置什么时区以及如何在 heroku 设置时区?
    • 时区是一个应用程序设置,在 config/application.rb 中。见here。如果您的测功机时间出现偏差,您别无选择,只能重新部署并希望它不会再次发生,或者按照 geemus 的建议重新同步时钟。对于其他试图赶上的人,请参阅groups.google.com/forum/#!topic/ruby-fog/kaehIbyr3kk
    • 是的,正如@Taavo 所指出的,我认为获得倾斜的测功机是不寻常的,因此重新部署也可能会修复它。 sync_clock 确保它,但可能不是大多数时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2019-05-09
    • 1970-01-01
    • 2017-09-14
    • 2012-01-08
    • 2018-04-18
    相关资源
    最近更新 更多