Class: Dragonfly::DataStorage::S3DataStore

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

Constant Summary collapse

REGIONS =
{
  'us-east-1'      => 's3.amazonaws.com',  #default
  'eu-west-1'      => 's3-eu-west-1.amazonaws.com',
  'ap-southeast-1' => 's3-ap-southeast-1.amazonaws.com',
  'us-west-1'      => 's3-us-west-1.amazonaws.com'
}

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ S3DataStore

Returns a new instance of S3DataStore.



26
27
28
29
30
31
# 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]
  self.region = opts[:region]
end

Instance Method Details

#bucket_exists?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 90

def bucket_exists?
  storage.get_bucket_location(bucket_name)
  true
rescue Excon::Errors::NotFound => e
  false
end

#destroy(uid) ⇒ Object

Raises:



63
64
65
66
67
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 63

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

#domainObject



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

def domain
  REGIONS[get_region]
end

#retrieve(uid) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 52

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

#storageObject



81
82
83
84
85
86
87
88
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 81

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

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 33

def store(temp_object, opts={})
  ensure_configured
  ensure_bucket_initialized

  meta = opts[:meta] || {}
  headers = opts[:headers] || {}
  uid = opts[:path] || generate_uid(meta[:name] || temp_object.original_filename || 'file')

  if use_filesystem
    temp_object.file do |f|
      storage.put_object(bucket_name, uid, f, full_storage_headers(headers, meta))
    end
  else
    storage.put_object(bucket_name, uid, temp_object.data, full_storage_headers(headers, meta))
  end

  uid
end

#url_for(uid, opts = {}) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 69

def url_for(uid, opts={})
  if opts && opts[:expires]
    storage.get_object_url(bucket_name, uid, opts[:expires])
  else
    "http://#{bucket_name}.s3.amazonaws.com/#{uid}"
  end
end