Class: Dragonfly::DataStorage::S3DataStore

Inherits:
Object
  • Object
show all
Includes:
Configurable, Serializer
Defined in:
lib/dragonfly/data_storage/s3data_store.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Serializer

#b64_decode, #b64_encode, #json_decode, #json_encode, #marshal_decode, #marshal_encode

Methods included from Configurable

included

Constructor Details

#initialize(opts = {}) ⇒ S3DataStore

Returns a new instance of S3DataStore.


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

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)

110
111
112
113
114
115
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 110

def bucket_exists?
  rescuing_socket_errors{ storage.get_bucket(bucket_name) }
  true
rescue Excon::Errors::NotFound => e
  false
end

#destroy(uid) ⇒ Object


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

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

#domainObject


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

def domain
  REGIONS[get_region]
end

#retrieve(uid) ⇒ Object


60
61
62
63
64
65
66
67
68
69
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 60

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

#storageObject


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 97

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

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


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 38

def store(temp_object, opts={})
  ensure_configured
  ensure_bucket_initialized
  
  headers = opts[:headers] || {}
  mime_type = opts[:mime_type] || opts[:content_type]
  headers['Content-Type'] = mime_type if mime_type
  uid = opts[:path] || generate_uid(temp_object.name || 'file')
  
  rescuing_socket_errors do
    if use_filesystem
      temp_object.file do |f|
        storage.put_object(bucket_name, uid, f, full_storage_headers(headers, temp_object.meta))
      end
    else
      storage.put_object(bucket_name, uid, temp_object.data, full_storage_headers(headers, temp_object.meta))
    end
  end
  
  uid
end

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


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

def url_for(uid, opts={})
  if opts && opts[:expires]
    if storage.respond_to?(:get_object_https_url) # fog's get_object_url is deprecated (aug 2011)
      storage.get_object_https_url(bucket_name, uid, opts[:expires])
    else
      storage.get_object_url(bucket_name, uid, opts[:expires])
    end
  else
    scheme = opts[:scheme] || url_scheme
    host   = opts[:host]   || url_host || "#{bucket_name}.s3.amazonaws.com"
    "#{scheme}://#{host}/#{uid}"
  end
end