Skip to content

Commit

Permalink
Merge 6fdc76b into 0e33595
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Apr 28, 2017
2 parents 0e33595 + 6fdc76b commit 8d81200
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
This Gem allows your rails application to access user files from cloud storage.
Currently there are drivers implemented for [Dropbox](http://www.dropbox.com),
[Skydrive](https://skydrive.live.com/), [Google Drive](http://drive.google.com),
[Box](http://www.box.com), and a server-side directory share.
[Box](http://www.box.com), [Amazon S3](https://aws.amazon.com/s3/),
and a server-side directory share.

The gem uses [OAuth](http://oauth.net/) to connect to a user's account and
generate a list of single use urls that your application can then use to
Expand Down
2 changes: 1 addition & 1 deletion lib/browse_everything/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(opts = {})
begin
driver_klass = BrowseEverything::Driver.const_get((config[:driver] || driver.to_s).camelize.to_sym)
@providers[driver] = driver_klass.new(config.merge(url_options: url_options))
rescue
rescue NameError
Rails.logger.warn "Unknown provider: #{driver}"
end
end
Expand Down
39 changes: 26 additions & 13 deletions lib/browse_everything/driver/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
module BrowseEverything
module Driver
class S3 < Base
DEFAULTS = { signed_url: true, region: 'us-east-1' }.freeze
CONFIG_KEYS = [:app_key, :app_secret, :bucket].freeze
DEFAULTS = { response_type: :signed_url }.freeze
RESPONSE_TYPES = [:signed_url, :public_url, :s3_uri].freeze
CONFIG_KEYS = [:bucket].freeze

attr_reader :entries

def initialize(config, *args)
if config.key?(:signed_url) && config.delete(:signed_url) == false
warn '[DEPRECATION] Amazon S3 driver: `:signed_url` is deprecated. Please use `:response_type` instead.'
config[:response_type] = :public_url
end
config = DEFAULTS.merge(config)
super
end
Expand All @@ -18,6 +23,12 @@ def icon
end

def validate_config
if config.values_at(:app_key, :app_secret).compact.length == 1
raise BrowseEverything::InitializationError, 'Amazon S3 driver: If either :app_key or :app_secret is provided, both must be.'
end
unless RESPONSE_TYPES.include?(config[:response_type].to_sym)
raise BrowseEverything::InitializationError, "Amazon S3 driver: Valid response types: #{RESPONSE_TYPES.join(',')}"
end
return if CONFIG_KEYS.all? { |key| config[key].present? }
raise BrowseEverything::InitializationError, "Amazon S3 driver requires #{CONFIG_KEYS.join(',')}"
end
Expand Down Expand Up @@ -63,12 +74,8 @@ def init_entries(path)
@entries = if path.empty?
[]
else
[BrowseEverything::FileEntry.new(Pathname(path).join('..'),
'',
'..',
0,
Time.current,
true)]
[BrowseEverything::FileEntry.new(Pathname(path).join('..'), '', '..',
0, Time.current, true)]
end
end

Expand All @@ -90,10 +97,10 @@ def details(path)

def link_for(path)
obj = bucket.object(path)
if config[:signed_url]
obj.presigned_url(:get, expires_in: 14400)
else
obj.public_url
case config[:response_type].to_sym
when :signed_url then obj.presigned_url(:get, expires_in: 14400)
when :public_url then obj.public_url
when :s3_uri then "s3://#{obj.bucket_name}/#{obj.key}"
end
end

Expand All @@ -106,7 +113,13 @@ def bucket
end

def client
@client ||= Aws::S3::Client.new(credentials: Aws::Credentials.new(config[:app_key], config[:app_secret]), region: config[:region])
if @client.nil?
aws_config = {}
aws_config[:credentials] = Aws::Credentials.new(config[:app_key], config[:app_secret]) if config[:app_key].present?
aws_config[:region] = config[:region] if config.key?(:region)
@client = Aws::S3::Client.new(aws_config)
end
@client
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
# :client_id: YOUR_GOOGLE_API_CLIENT_ID
# :client_secret: YOUR_GOOGLE_API_CLIENT_SECRET
# s3:
# :app_key: YOUR_AWS_S3_KEY
# :app_secret: YOUR_AWS_S3_SECRET
# :bucket: YOUR_AWS_S3_BUCKET
# :region: YOUR_AWS_S3_REGION # default: us-east-1
# :signed_url: true # set to false for public urls
# :response_type: :signed_url # set to :public_url for public urls or :s3_uri for an s3://BUCKET/KEY uri
# :app_key: YOUR_AWS_S3_KEY # :app_key, :app_secret, and :region can be specified
# :app_secret: YOUR_AWS_S3_SECRET # explicitly here, or left out to use system-configured
# :region: YOUR_AWS_S3_REGION # defaults.
# See https://aws.amazon.com/blogs/security/a-new-and-standardized-way-to-manage-credentials-in-the-aws-sdks/
# sky_drive:
# :client_id: YOUR_MS_LIVE_CONNECT_CLIENT_ID
# :client_secret: YOUR_MS_LIVE_CONNECT_CLIENT_SECRET

0 comments on commit 8d81200

Please sign in to comment.