Class: Attached::Storage::AWS
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
readonly
Returns the value of attribute access_key_id.
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#permissions ⇒ Object
readonly
Returns the value of attribute permissions.
-
#secret_access_key ⇒ Object
readonly
Returns the value of attribute secret_access_key.
Instance Method Summary collapse
-
#destroy(path) ⇒ Object
Destroy a file at a given path.
-
#host ⇒ Object
Access the host (e.g. bucket.s3.amazonaws.com) for a storage service.
-
#initialize(credentials) ⇒ AWS
constructor
Create a new interface supporting save and destroy operations.
-
#retrieve(path) ⇒ Object
Retrieve a file from a given path.
-
#save(file, path) ⇒ Object
Save a file to a given path.
Methods inherited from Base
Constructor Details
#initialize(credentials) ⇒ AWS
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/attached/storage/aws.rb', line 29 def initialize(credentials) 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 end |
Instance Attribute Details
#access_key_id ⇒ Object (readonly)
Returns the value of attribute access_key_id.
18 19 20 |
# File 'lib/attached/storage/aws.rb', line 18 def access_key_id @access_key_id end |
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
17 18 19 |
# File 'lib/attached/storage/aws.rb', line 17 def bucket @bucket end |
#permissions ⇒ Object (readonly)
Returns the value of attribute permissions.
15 16 17 |
# File 'lib/attached/storage/aws.rb', line 15 def @permissions end |
#secret_access_key ⇒ Object (readonly)
Returns the value of attribute secret_access_key.
19 20 21 |
# File 'lib/attached/storage/aws.rb', line 19 def secret_access_key @secret_access_key end |
Instance Method Details
#destroy(path) ⇒ Object
Destroy a file at a given path.
Parameters:
-
path - The path to destroy.
104 105 106 107 108 109 110 |
# File 'lib/attached/storage/aws.rb', line 104 def destroy(path) directory = connection.directories.get(self.bucket) directory ||= connection.directories.create(self..merge(:key => self.bucket)) file = directory.files.get(path) file.destroy if file end |
#host ⇒ Object
Access the host (e.g. bucket.s3.amazonaws.com) for a storage service.
Usage:
storage.host
48 49 50 |
# File 'lib/attached/storage/aws.rb', line 48 def host() "https://#{self.bucket}.s3.amazonaws.com/" end |
#retrieve(path) ⇒ Object
Retrieve a file from a given path.
Parameters:
-
path - The path to retrieve.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/attached/storage/aws.rb', line 78 def retrieve(path) directory = connection.directories.get(self.bucket) directory ||= connection.directories.create(self..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 |
#save(file, path) ⇒ Object
Save a file to a given path.
Parameters:
-
file - The file to save.
-
path - The path to save.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/attached/storage/aws.rb', line 60 def save(file, path) file = File.open(file.path) directory = connection.directories.get(self.bucket) directory ||= connection.directories.create(self..merge(:key => self.bucket)) directory.files.create(self.(path).merge(self..merge(:key => path, :body => file))) file.close end |