Class: Configuration::S3SourceStoreBase

Inherits:
SourceStoreBase show all
Extended by:
Stats
Includes:
ClassLogging
Defined in:
lib/httpimagestore/configuration/s3.rb

Direct Known Subclasses

S3Source, S3Store

Defined Under Namespace

Classes: CacheObject, CacheRoot, S3Object

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConditionalInclusion

#excluded?, #included?, #inclusion_matcher

Constructor Details

#initialize(global, image_name, matcher, bucket, path_spec, public_access, cache_control, prefix, cache_root) ⇒ S3SourceStoreBase

Returns a new instance of S3SourceStoreBase.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/httpimagestore/configuration/s3.rb', line 308

def initialize(global, image_name, matcher, bucket, path_spec, public_access, cache_control, prefix, cache_root)
  super global, image_name, matcher
  @bucket = bucket
  @path_spec = path_spec
  @public_access = public_access
  @cache_control = cache_control
  @prefix = prefix

  @cache_root = nil
  begin
    if cache_root
      @cache_root = CacheRoot.new(cache_root)
      log.info "using S3 object cache directory '#{cache_root}' for image '#{image_name}'"
    else
      log.info "S3 object cache not configured (no cache-root) for image '#{image_name}'"
    end
  rescue CacheRoot::CacheRootNotDirError => error
    log.warn "not using S3 object cache for image '#{image_name}'", error
  end

  local :bucket, @bucket
end

Class Method Details

.parse(configuration, node) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/httpimagestore/configuration/s3.rb', line 284

def self.parse(configuration, node)
  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')
  public_access = false if public_access.nil?
  prefix = '' if prefix.nil?

  self.new(
    configuration.global,
    image_name,
    InclusionMatcher.new(image_name, if_image_name_on),
    bucket,
    path_spec,
    public_access,
    cache_control,
    prefix,
    cache_root
  )
end

Instance Method Details

#clientObject



331
332
333
# File 'lib/httpimagestore/configuration/s3.rb', line 331

def client
  @global.s3 or raise S3NotConfiguredError
end

#object(path) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/httpimagestore/configuration/s3.rb', line 343

def object(path)
  begin
    key = @prefix + path
    image = nil

    if @cache_root
      begin
        cache_file = @cache_root.cache_file(@bucket, key)
        CacheObject.new(cache_file, client, @bucket, key) do |obj|
          image = yield obj
        end
        return image
      rescue Errno::EACCES, IOError => error
        log.warn "cannot use S3 object cache for bucket: '#{@bucket}' key: '#{key}' [#{cache_file}]", error
      end
    end
    return yield S3Object.new(client, @bucket, key)
  rescue AWS::S3::Errors::AccessDenied
    raise S3AccessDenied.new(@bucket, path)
  rescue AWS::S3::Errors::NoSuchBucket
    raise S3NoSuchBucketError.new(@bucket)
  rescue AWS::S3::Errors::NoSuchKey
     raise S3NoSuchKeyError.new(@bucket, path)
  end
end

#url(object) ⇒ Object



335
336
337
338
339
340
341
# File 'lib/httpimagestore/configuration/s3.rb', line 335

def url(object)
  if @public_access
    object.public_url
  else
    object.private_url
  end
end