lib/attached/storage/aws.rb in attached-0.5.9 vs lib/attached/storage/aws.rb in attached-0.6.0

- old
+ new

@@ -1,21 +1,14 @@ -require 'attached/storage/base' +require 'attached/storage/fog' +require 'fog' -begin - require 'fog' -rescue LoadError - raise "installation of 'fog' is required before using 'aws' for storage" -end - module Attached module Storage - class AWS < Base + class AWS < Fog - attr_reader :permissions - attr_reader :bucket attr_reader :access_key_id attr_reader :secret_access_key @@ -25,14 +18,14 @@ # # Attached::Storage::AWS.new() # Attached::Storage::AWS.new("aws.yml") def initialize(credentials) + super + credentials = parse(credentials) - @permissions = { :public => true } - @bucket = credentials[:bucket] || credentials['bucket'] @access_key_id = credentials[:access_key_id] || credentials['access_key_id'] @secret_access_key = credentials[:secret_access_key] || credentials['secret_access_key'] raise "'bucket' must be specified if using 'aws' for storage" unless @bucket @@ -48,80 +41,25 @@ def host() "https://#{self.bucket}.s3.amazonaws.com/" end - # Save a file to a given path. - # - # Parameters: - # - # * file - The file to save. - # * path - The path to save. + private - def save(file, path) - file = File.open(file.path) - directory = connection.directories.get(self.bucket) - directory ||= connection.directories.create(self.permissions.merge(:key => self.bucket)) + def directory() + connection.directories.get(self.bucket) || connection.directories.create(self.defaults.merge(:key => self.bucket)) + end - directory.files.create(self.options(path).merge(self.permissions.merge(:key => path, :body => file))) - file.close - end - - - # Retrieve a file from a given path. - # - # Parameters: - # - # * path - The path to retrieve. - - def retrieve(path) - directory = connection.directories.get(self.bucket) - directory ||= connection.directories.create(self.permissions.merge(:key => self.bucket)) - - file = directory.files.get(path) - - body = file.body - - extname = File.extname(path) - basename = File.basename(path, extname) - - file = Tempfile.new([basename, extname]) - file.binmode - file.write(body) - file.rewind - - file - end - - - # Destroy a file at a given path. - # - # Parameters: - # - # * path - The path to destroy. - - def destroy(path) - directory = connection.directories.get(self.bucket) - directory ||= connection.directories.create(self.permissions.merge(:key => self.bucket)) - - file = directory.files.get(path) - file.destroy if file - end - - - private - - def connection - @connection ||= Fog::Storage.new( + @connection ||= ::Fog::Storage.new( :aws_secret_access_key => self.secret_access_key, :aws_access_key_id => self.access_key_id, :provider => 'AWS' ) end end end -end \ No newline at end of file +end