Class: Attached::Storage::AWS

Inherits:
Base
  • Object
show all
Defined in:
lib/attached/storage/aws.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#options, #parse

Constructor Details

#initialize(credentials) ⇒ AWS

Create a new interface supporting save and destroy operations.

Usage:

Attached::Storage::AWS.new()
Attached::Storage::AWS.new("aws.yml")


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_idObject (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

#bucketObject (readonly)

Returns the value of attribute bucket.



17
18
19
# File 'lib/attached/storage/aws.rb', line 17

def bucket
  @bucket
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



15
16
17
# File 'lib/attached/storage/aws.rb', line 15

def permissions
  @permissions
end

#secret_access_keyObject (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.permissions.merge(:key => self.bucket))

  file = directory.files.get(path)
  file.destroy if file
end

#hostObject

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.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

#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.permissions.merge(:key => self.bucket))

  directory.files.create(self.options(path).merge(self.permissions.merge(:key => path, :body => file)))

  file.close
end