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 inherited from Scope

node_parsers, #parse, register_node_parser

Constructor Details

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

Returns a new instance of S3SourceStoreBase.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/httpimagestore/configuration/s3.rb', line 321

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

  @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

  config_local :bucket, @bucket
end

Class Method Details

.parse(configuration, node) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/httpimagestore/configuration/s3.rb', line 293

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, 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?

  s3 = self.new(
    configuration.global,
    image_name,
    bucket,
    path_spec,
    public_access,
    cache_control,
    prefix,
    cache_root
  )
  s3.with_conditions(conditions)
  s3
end

Instance Method Details

#clientObject



344
345
346
# File 'lib/httpimagestore/configuration/s3.rb', line 344

def client
  @global.s3 or raise S3NotConfiguredError
end

#object(path) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/httpimagestore/configuration/s3.rb', line 356

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



348
349
350
351
352
353
354
# File 'lib/httpimagestore/configuration/s3.rb', line 348

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