lib/attached/storage/aws.rb in attached-0.2.1 vs lib/attached/storage/aws.rb in attached-0.2.2
- old
+ new
@@ -1,36 +1,38 @@
require 'attached/storage/base'
begin
- require 'aws/s3'
+ require 'fog'
rescue LoadError
- raise "installation of 'aws/s3' is required before using 'aws' for storage"
+ raise "installation of 'fog' is required before using 'aws' for storage"
end
module Attached
module Storage
class AWS < Base
- attr_reader :access
+ attr_reader :permissions
+
attr_reader :bucket
attr_reader :access_key_id
attr_reader :secret_access_key
# Create a new AWS interface supporting save and destroy operations.
#
# Usage:
#
- # Attached::Storage::S3.new()
- # Attached::Storage::S3.new("s3.yml")
+ # Attached::Storage::AWS.new()
+ # Attached::Storage::AWS.new("aws.yml")
def initialize(credentials)
credentials = parse(credentials)
- @access = :public_read
+ @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']
end
@@ -40,11 +42,11 @@
# Usage:
#
# storage.host
def host()
- "https://#{self.bucket}.s3.amazonaws.com"
+ "https://#{self.bucket}.s3.amazonaws.com/"
end
# Save a file to a given path on AWS S3.
#
@@ -52,42 +54,41 @@
#
# * file - The file to save.
# * path - The path to save.
def save(file, path)
- connect()
- begin
- ::AWS::S3::S3Object.store(path, file, bucket, :access => access)
- rescue ::AWS::S3::NoSuchBucket => e
- ::AWS::S3::Bucket.create(bucket)
- retry
- end
+ file = File.open(file.path)
+
+ directory = connection.directories.get(self.bucket)
+ directory ||= connection.directories.create(self.permissions.merge(:key => self.bucket))
+
+ directory.files.create(self.permissions.merge(:body => file, :key => path))
end
# Destroy a file at a given path on AWS S3.
#
# Parameters:
#
# * path - The path to destroy.
def destroy(path)
- connect()
- begin
- ::AWS::S3::S3Object.delete(path, bucket)
- rescue ::AWS::S3::NoSuchBucket => e
- end
+ 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
+
-
- # Connect to an AWS S3 server.
-
- def connect()
- @connection ||= ::AWS::S3::Base.establish_connection!(
- :access_key_id => access_key_id, :secret_access_key => secret_access_key
+ def connection
+ @connection ||= Fog::Storage.new(
+ :aws_secret_access_key => self.secret_access_key,
+ :aws_access_key_id => self.access_key_id,
+ :provider => 'AWS'
)
end
end
\ No newline at end of file