lib/httpimagestore/configuration/s3.rb in httpimagestore-1.8.1 vs lib/httpimagestore/configuration/s3.rb in httpimagestore-1.9.0

- old
+ new

@@ -2,11 +2,11 @@ require 'digest/sha2' require 'msgpack' require 'addressable/uri' require 'httpimagestore/aws_sdk_regions_hack' require 'httpimagestore/configuration/path' -require 'httpimagestore/configuration/handler' +require 'httpimagestore/configuration/handler/source_store_base' require 'httpimagestore/configuration/source_failover' module Configuration class S3NotConfiguredError < ConfigurationError def initialize @@ -121,11 +121,11 @@ begin header = MessagePack.pack(@header) io.write [header.length].pack('L') # header length io.write header io.write data - rescue => error + rescue unlink # remove broken cache file raise end end end @@ -294,30 +294,35 @@ image_name = node.grab_values('image name').first node.required_attributes('bucket', 'path') node.valid_attribute_values('public_access', true, false, nil) - bucket, path_spec, public_access, cache_control, prefix, cache_root, if_image_name_on = - *node.grab_attributes('bucket', 'path', 'public', 'cache-control', 'prefix', 'cache-root', 'if-image-name-on') + bucket, path_spec, public_access, cache_control, prefix, cache_root, remaining = + *node.grab_attributes_with_remaining('bucket', 'path', 'public', 'cache-control', 'prefix', 'cache-root') + conditions, remaining = *ConditionalInclusion.grab_conditions_with_remaining(remaining) + remaining.empty? or raise UnexpectedAttributesError.new(node, remaining) + public_access = false if public_access.nil? prefix = '' if prefix.nil? - self.new( + s3 = self.new( configuration.global, image_name, - InclusionMatcher.new(image_name, if_image_name_on), bucket, path_spec, public_access, cache_control, prefix, cache_root ) + s3.with_conditions(conditions) + s3 end - def initialize(global, image_name, matcher, bucket, path_spec, public_access, cache_control, prefix, cache_root) - super global, image_name, matcher, path_spec + def initialize(global, image_name, bucket, path_spec, public_access, cache_control, prefix, cache_root) + super(global, image_name, path_spec) + @bucket = bucket @public_access = public_access @cache_control = cache_control @prefix = prefix @@ -377,10 +382,12 @@ S3SourceStoreBase.logger = Handler.logger_for(S3SourceStoreBase) CacheObject.logger = S3SourceStoreBase.logger_for(CacheObject) end class S3Source < S3SourceStoreBase + include PerfStats + def self.match(node) node.name == 'source_s3' end def self.parse(configuration, node) @@ -389,20 +396,22 @@ def realize(request_state) put_sourced_named_image(request_state) do |image_name, rendered_path| log.info "sourcing '#{image_name}' image from S3 '#{@bucket}' bucket under '#{rendered_path}' key" - object(rendered_path) do |object| - data = request_state.memory_limit.get do |limit| - object.read(limit + 1) - end - S3SourceStoreBase.stats.incr_total_s3_source - S3SourceStoreBase.stats.incr_total_s3_source_bytes(data.bytesize) + measure "sourcing image from S3", "image: '#{image_name}' bucket: '#{@bucket}'" do + object(rendered_path) do |object| + data = request_state.memory_limit.get do |limit| + object.read(limit + 1) + end + S3SourceStoreBase.stats.incr_total_s3_source + S3SourceStoreBase.stats.incr_total_s3_source_bytes(data.bytesize) - image = Image.new(data, object.content_type) - image.source_url = url(object) - image + image = Image.new(data, object.content_type) + image.source_url = url(object) + image + end end end end def to_s @@ -411,10 +420,12 @@ end Handler::register_node_parser S3Source SourceFailover::register_node_parser S3Source class S3Store < S3SourceStoreBase + include PerfStats + def self.match(node) node.name == 'store_s3' end def self.parse(configuration, node) @@ -424,24 +435,25 @@ def realize(request_state) get_named_image_for_storage(request_state) do |image_name, image, rendered_path| acl = @public_access ? :public_read : :private log.info "storing '#{image_name}' image in S3 '#{@bucket}' bucket under '#{rendered_path}' key with #{acl} access" + measure "storing image in S3", "image: '#{image_name}' bucket: '#{@bucket}'" do + object(rendered_path) do |object| + image.mime_type or log.warn "storing '#{image_name}' in S3 '#{@bucket}' bucket under '#{rendered_path}' key with unknown mime type" - object(rendered_path) do |object| - image.mime_type or log.warn "storing '#{image_name}' in S3 '#{@bucket}' bucket under '#{rendered_path}' key with unknown mime type" + options = {} + options[:single_request] = true + options[:content_type] = image.mime_type if image.mime_type + options[:acl] = acl + options[:cache_control] = @cache_control if @cache_control - options = {} - options[:single_request] = true - options[:content_type] = image.mime_type if image.mime_type - options[:acl] = acl - options[:cache_control] = @cache_control if @cache_control + object.write(image.data, options) + S3SourceStoreBase.stats.incr_total_s3_store + S3SourceStoreBase.stats.incr_total_s3_store_bytes(image.data.bytesize) - object.write(image.data, options) - S3SourceStoreBase.stats.incr_total_s3_store - S3SourceStoreBase.stats.incr_total_s3_store_bytes(image.data.bytesize) - - image.store_url = url(object) + image.store_url = url(object) + end end end end end Handler::register_node_parser S3Store