class BrowseEverything::Driver::S3
Constants
- CONFIG_KEYS
- DEFAULTS
- RESPONSE_TYPES
Attributes
authentication_klass[RW]
entries[R]
Public Class Methods
default_authentication_klass()
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 16 def default_authentication_klass Aws::S3::Client end
new(config, *args)
click to toggle source
Calls superclass method
BrowseEverything::Driver::Base::new
# 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
Public Instance Methods
bucket()
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 76 def bucket @bucket ||= Aws::S3::Bucket.new(config[:bucket], client: client) end
contents(path = '')
click to toggle source
Retrieve the entries from the S3
Bucket @return [Array<BrowseEverything::FileEntry>]
# 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
icon()
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 34 def icon 'amazon' end
link_for(path)
click to toggle source
# 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_config()
click to toggle source
# 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
Private Instance Methods
add_directories(listing)
click to toggle source
Populate the entries with FileEntry
objects from an S3
listing @param listing [Seahorse::Client::Response]
# File lib/browse_everything/driver/s3.rb, line 128 def add_directories(listing) listing.common_prefixes.each do |prefix| new_entry = entry_for(from_base(prefix.prefix), 0, Time.current, true) @entries << new_entry unless new_entry.nil? end end
add_files(listing, path)
click to toggle source
Given a listing and a S3
listing and path, populate the entries @param listing [Seahorse::Client::Response] @param path [String]
# File lib/browse_everything/driver/s3.rb, line 138 def add_files(listing, path) listing.contents.each do |entry| key = from_base(entry.key) new_entry = entry_for(key, entry.size, entry.last_modified, false) @entries << new_entry unless strip(key) == strip(path) || new_entry.nil? end end
authenticate()
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 108 def authenticate session.authenticate end
aws_config()
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 94 def aws_config result = {} result[:credentials] = Aws::Credentials.new(config[:app_key], config[:app_secret]) if config[:app_key].present? result[:region] = config[:region] if config.key?(:region) result end
client()
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 112 def client @client ||= authenticate end
entry_for(name, size, date, dir)
click to toggle source
Construct a BrowseEverything::FileEntry
object @param name [String] @param size [String] @param date [DateTime] @param dir [String] @return [BrowseEverything::FileEntry]
# File lib/browse_everything/driver/s3.rb, line 122 def entry_for(name, size, date, dir) BrowseEverything::FileEntry.new(name, [key, name].join(':'), File.basename(name), size, date, dir) end
from_base(key)
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 86 def from_base(key) Pathname.new(key).relative_path_from(Pathname.new(config[:base].to_s)).to_s end
full_path(path)
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 90 def full_path(path) config[:base].present? ? File.join(config[:base], path) : path end
generate_listing(path)
click to toggle source
For a given path to a S3
resource, retrieve the listing object and construct the file entries @param path [String]
# File lib/browse_everything/driver/s3.rb, line 149 def generate_listing(path) client listing = client.list_objects(bucket: config[:bucket], delimiter: '/', prefix: full_path(path)) add_directories(listing) add_files(listing, path) end
session()
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 101 def session AuthenticationFactory.new( self.class.authentication_klass, aws_config ) end
strip(path)
click to toggle source
# File lib/browse_everything/driver/s3.rb, line 82 def strip(path) path.sub %r{^/?(.+?)/?$}, '\1' end