Class: BrowseEverything::Driver::S3

Inherits:
Base
  • Object
show all
Defined in:
lib/browse_everything/driver/s3.rb

Constant Summary collapse

DEFAULTS =
{ response_type: :signed_url, expires_in: 14400 }.freeze
RESPONSE_TYPES =
i[signed_url public_url s3_uri].freeze
CONFIG_KEYS =
i[bucket region].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Base

#code, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#auth_link, #config, #connect, default_sorter, inherited, #key, #name

Constructor Details

#initialize(config, *args) ⇒ S3

Returns a new instance of S3.



23
24
25
26
27
28
29
30
31
32
# File 'lib/browse_everything/driver/s3.rb', line 23

def initialize(config, *args)
  if config.key?(:signed_url)
    warn '[DEPRECATION] Amazon S3 driver: `:signed_url` is deprecated.  Please use `response_type :signed_url` instead.'
    response_type = config.delete(:signed_url) ? :signed_url : :public_url
    config[:response_type] = response_type
  end
  merged_config = DEFAULTS.merge(config)
  self.class.authentication_klass ||= self.class.default_authentication_klass
  super(merged_config, *args)
end

Class Attribute Details

.authentication_klassObject

Returns the value of attribute authentication_klass.



14
15
16
# File 'lib/browse_everything/driver/s3.rb', line 14

def authentication_klass
  @authentication_klass
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



21
22
23
# File 'lib/browse_everything/driver/s3.rb', line 21

def entries
  @entries
end

Class Method Details

.default_authentication_klassObject



16
17
18
# File 'lib/browse_everything/driver/s3.rb', line 16

def default_authentication_klass
  Aws::S3::Client
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/browse_everything/driver/s3.rb', line 72

def authorized?
  true
end

#bucketObject



76
77
78
# File 'lib/browse_everything/driver/s3.rb', line 76

def bucket
  @bucket ||= Aws::S3::Bucket.new(config[:bucket], client: client)
end

#contents(path = '') ⇒ Array<BrowseEverything::FileEntry>

Retrieve the entries from the S3 Bucket

Returns:



47
48
49
50
51
52
53
# File 'lib/browse_everything/driver/s3.rb', line 47

def contents(path = '')
  path = File.join(path, '') unless path.empty?
  @entries = []

  generate_listing(path)
  @sorter.call(@entries)
end

#iconObject



34
35
36
# File 'lib/browse_everything/driver/s3.rb', line 34

def icon
  'amazon'
end


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/browse_everything/driver/s3.rb', line 55

def link_for(path)
  obj = bucket.object(full_path(path))

  extras = {
    file_name: File.basename(path),
    expires: (config[:expires_in] if config[:response_type] == :signed_url)
  }.compact

  url = case config[:response_type].to_sym
        when :signed_url then obj.presigned_url(:get, expires_in: config[:expires_in])
        when :public_url then obj.public_url
        when :s3_uri     then "s3://#{obj.bucket_name}/#{obj.key}"
        end

  [url, extras]
end

#validate_configObject



38
39
40
41
42
43
# File 'lib/browse_everything/driver/s3.rb', line 38

def validate_config
  raise InitializationError, 'Amazon S3 driver: If either :app_key or :app_secret is provided, both must be.' if config.values_at(:app_key, :app_secret).compact.length == 1
  raise InitializationError, "Amazon S3 driver: Valid response types: #{RESPONSE_TYPES.join(',')}" unless RESPONSE_TYPES.include?(config[:response_type].to_sym)
  return if CONFIG_KEYS.all? { |key| config[key].present? }
  raise InitializationError, "Amazon S3 driver requires #{CONFIG_KEYS.join(',')}"
end