我使用基于amazon s3的单独远程存储来存储图像。 通过activeadmin更新记录我遇到了未知错误 在图像更新期间。堆栈跟踪不是rails错误 但是在舞台环境中出现了某种超时。
配置/初始化/ carrierwave.rb
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => APP['s3']['key'],
:aws_secret_access_key => APP['s3']['secret'],
}
config.fog_directory = 'mdfile'
config.fog_public = false
config.fog_authenticated_url_expiration = 5
config.cache_dir = File.join(Rails.root, 'tmp', 's3', Rails.env)
end
应用程序/上传/ news_image_uploader.rb
class NewsImageUploader < Mdfile::BaseUploader
include CarrierWave::MiniMagick
process :resize_to_fill => [150, 112]
process convert: 'png'
def extension_white_list
APP['upload']['image_white_list']
end
def fog_public
true
end
def filename
"image.png" if original_filename
end
end
应用程序/上传/ mdfile / base_uploader.rb
module Mdfile
class BaseUploader < CarrierWave::Uploader::Base
before :store, :remember_cache_id
before :cache, :remove_old_file_before_cache
def remember_cache_id(new_file)
@cache_id_was = cache_id
end
def remove_old_file_before_cache(new_file)
remove! unless blank?
end
def store_dir
"uploads/#{Rails.env}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def filename
if original_filename
@name ||= Digest::MD5.hexdigest(File.dirname(current_path))
"#{@name}.#{file.extension}"
end
end
end
end
分配给模特(新闻,文章)的所有图像都在特殊路径上的遥控器s3上分页:uploads / stage / article / image / 691 / image.png
我已经进行了研究并使用EXCON_DEBUG = true运行rails,用于记录s3和我的主机之间的请求和响应。 如确定
创建新记录:
Excon发送一个PUT请求转储到
下面excon.request {:chunk_size=>1048576, :connect_timeout=>60, :headers=>{"User-Agent"=>"fog/1.9.0", "Content-Length"=>37593, "Content-Type"=>"image/jpeg", "x-amz-acl"=>"public-read", "Date"=>"Wed, 14 Aug 2013 08:49:52 +0000", "Authorization"=>"REDACTED", "Host"=>"mdfile.s3.amazonaws.com:443"}, :idempotent=>true, :instrumentor_name=>"excon", :nonblock=>true, :read_timeout=>60, :retry_limit=>4, :ssl_verify_peer=>true, :uri_parser=>URI, :write_timeout=>60, :host=>"mdfile.s3.amazonaws.com", :path=>"/uploads%2Fdevelopment%2Fnews%2Fimage%2F692%2Fimage.png", :port=>"443", :query=>nil, :scheme=>"https", :user=>nil, :password=>"REDACTED", :instrumentor=>Excon::StandardInstrumentor, :body=>#<File:/local/path/to/cached/file/20130814-1249-6113-6726/Reaktor.jpg>, :expects=>200, :method=>"PUT", :retries_remaining=>4 }
excon.response {:body=>"", :headers=>{ "Date"=>"Wed, 14 Aug 2013 08:49:53 GMT", "Content-Length"=>"0", "Server"=>"AmazonS3"}, :status=>200, :remote_ip=>"[XXX.XXX.XXX.XXX]"}
我剪掉了一些私人和无用的字段。
更新现有记录:
Excon发送一个DELETE请求来删除旧图像, 然后它发送路径=“/”(wtf !!)的GET请求,并进入无限循环的GET请求和成功响应。
excon.request {:chunk_size=>1048576, :connect_timeout=>60, :headers=>{"User-Agent"=>"fog/1.9.0", "Date"=>"Wed, 14 Aug 2013 08:56:47 +0000", "Authorization"=>"REDACTED", "Host"=>"mdfile.s3.amazonaws.com:443"}, :idempotent=>true, :instrumentor_name=>"excon", :mock=>false, :nonblock=>true, :read_timeout=>60, :retry_limit=>4, :ssl_verify_peer=>true, :uri_parser=>URI, :write_timeout=>60, :host=>"mdfile.s3.amazonaws.com", :path=>"/uploads%2Fdevelopment%2Fnews%2Fimage%2F692%2Fimage.png", :port=>"443", :query=>nil, :scheme=>"https", :user=>nil, :password=>"REDACTED", :family=>0, :expects=>204, :method=>"DELETE", :retries_remaining=>4, :connection=>#<Excon::Connection:b2ee900 @data={:chunk_size=>1048576, :connect_timeout=>60, :headers=>{"User-Agent"=>"fog/1.9.0"}, :idempotent=>false, :instrumentor_name=>"excon", :mock=>false, :nonblock=>true, :read_timeout=>60, :retry_limit=>4, :ssl_verify_peer=>true, :uri_parser=>URI, :write_timeout=>60, :host=>"s3.amazonaws.com", :path=>"/", :port=>"443", :query=>nil, :scheme=>"https", :user=>nil, :password=>nil, :instrumentor=>Excon::StandardInstrumentor, :family=>0} @socket_key="s3.amazonaws.com:443">, :mock=>false, :nonblock=>true, :read_timeout=>60, :retry_limit=>4, :ssl_verify_peer=>true, :uri_parser=>URI, :write_timeout=>60, :host=>"s3.amazonaws.com", :path=>"/", :port=>"443", :query=>nil, :scheme=>"https", :user=>nil, :password=>nil, :instrumentor=>Excon::StandardInstrumentor, :family=>0} @socket_key="s3.amazonaws.com:443">>>>>}
excon.response {:body=>"", :headers=>{"Date"=>"Wed, 14 Aug 2013 08:56:48 GMT", "Server"=>"AmazonS3"}, :status=>204, :remote_ip=>"[XXX.XXX.XXX.XXX]"}
excon.request {:chunk_size=>1048576, :connect_timeout=>60, :headers=>{"User-Agent"=>"fog/1.9.0", "Date"=>"Wed, 14 Aug 2013 08:56:48 +0000", "Authorization"=>"REDACTED", "Host"=>"s3.amazonaws.com:443"}, :idempotent=>true, :instrumentor_name=>"excon", :mock=>false, :nonblock=>true, :read_timeout=>60, :retry_limit=>4, :ssl_verify_peer=>true, :uri_parser=>URI, :write_timeout=>60, :host=>"s3.amazonaws.com", :path=>"/", :port=>"443", :query=>nil, :scheme=>"https", :user=>nil, :password=>"REDACTED", :instrumentor=>Excon::StandardInstrumentor, :family=>0, :expects=>200, :method=>"GET", :url=>"s3.amazonaws.com", :ssl_verify_peer=>true, :uri_parser=>URI, :write_timeout=>60, :host=>"s3.amazonaws.com", :path=>"/", :port=>"443", :query=>nil, :scheme=>"https", :user=>nil, :password=>nil, :instrumentor=>Excon::StandardInstrumentor, :family=>0} @socket_key="s3.amazonaws.com:443">}
excon.response {:body=>"", :headers=>{"Date"=>"Wed, 14 Aug 2013 08:56:50 GMT", "Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked", "Server"=>"AmazonS3"}, :status=>200, :remote_ip=>"[XXX.XXX.XXX.XXX]"}
所以我可以通过kill -9
在localhost上停止rails进程然后我澄清说,这种行为发生在调用CarrierWave逻辑的模型(新闻,文章...)的update_attribute(s)方法中。
问题:为什么carrierwave / fog会产生奇怪的GET请求并滚动到无限循环
更新: 我发现如果我在更新属性之前用nil值更新uploader的列(在我的情况下是'image'),而不是将正确的PUT请求发送到s3。因此,activeadmin控制器的错误修正代码是:
controller do
def update
resource_object = resource_class
@object = resource_object.find(params[:id])
object_sym = resource_class.to_s.downcase.to_sym
@object.update_column(:image, nil) if @object.respond_to?(:image) && params[object_sym][:image].present?
update!
end
end
但问题仍然是热门话题。