Class: Dragonfly::DataStorage::S3DataStore

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

Constant Summary

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'
}

Configuration Summary

Configurable attributes

Configurable attributes for an object in general object can be configured either by using something like
  object.configure do |c|
    c.some_configurable_attribute = "some value"
    c.some_other_configurable_attribute = 42
    ...
  end
or
  object.some_configurable_attribute = "some value"

Configurable attributes for instances of Dragonfly::DataStorage::S3DataStore

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

- (S3DataStore) initialize(opts = {})

Returns a new instance of S3DataStore



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

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]
  self.use_iam_profile = opts[:use_iam_profile]
end

Instance Method Details

- (Boolean) bucket_exists?

Returns:

  • (Boolean)


113
114
115
116
117
118
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 113

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

- (Object) destroy(uid)

Raises:



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

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

- (Object) domain



95
96
97
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 95

def domain
  REGIONS[get_region]
end

- (Object) retrieve(uid)

Raises:



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

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

- (Object) storage



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

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,
      :use_iam_profile => use_iam_profile
    }.reject {|name, option| option.nil?})
    storage.sync_clock
    storage
  end
end

- (Object) store(temp_object, opts = {})



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

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

- (Object) url_for(uid, opts = {})



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

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