Class: Dragonfly::DataStorage::S3DataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/data_storage/s3data_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ S3DataStore

Returns a new instance of S3DataStore.



26
27
28
29
30
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 26

def initialize(opts={})
  self.bucket_name = opts[:bucket_name]
  self.access_key_id = opts[:access_key_id]
  self.secret_access_key = opts[:secret_access_key]
end

Instance Method Details

#connectionObject



32
33
34
35
36
37
38
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 32

def connection
  @connection ||= Fog::Storage.new(:provider => 'AWS',
    :aws_access_key_id => access_key_id,
    :aws_secret_access_key => secret_access_key,
    :region => region
  )
end

#create_bucket!Object



40
41
42
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 40

def create_bucket!
  connection.put_bucket(bucket_name) unless bucket_names.include?(bucket_name)
end

#destroy(uid) ⇒ Object

Raises:



75
76
77
78
79
80
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 75

def destroy(uid)
  ensure_initialized
  connection.delete_object(bucket_name, uid)
rescue Excon::Errors::NotFound => e
  raise DataNotFound, "#{e} - #{uid}"
end

#retrieve(uid) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
73
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 64

def retrieve(uid)
  ensure_initialized
  s3_object = connection.get_object(bucket_name, uid)
  [
    s3_object.body,
    (s3_object.headers)
  ]
rescue Excon::Errors::NotFound => e
  raise DataNotFound, "#{e} - #{uid}"
end

#store(temp_object, opts = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 44

def store(temp_object, opts={})
  uid = opts[:path] || generate_uid(temp_object.name || 'file')
  ensure_initialized
  extra_data = temp_object.attributes
  if use_filesystem
    temp_object.file do |f|
      connection.put_object(bucket_name,
                            uid,
                            f.read,
                            (extra_data))
    end
  else
    connection.put_object(bucket_name,
                          uid,
                          temp_object.data,
                          (extra_data))
  end
  uid
end