Class: Dragonfly::S3DataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/s3_data_store.rb,
lib/dragonfly/s3_data_store/version.rb

Defined Under Namespace

Classes: NotConfigured

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',
  'ap-southeast-2' => 's3-ap-southeast-2.amazonaws.com',
  'eu-west-1' => 's3-eu-west-1.amazonaws.com',
  'eu-central-1' => 's3-eu-central-1.amazonaws.com',
  'sa-east-1' => 's3-sa-east-1.amazonaws.com'
}
SUBDOMAIN_PATTERN =
/^[a-z0-9][a-z0-9.-]+[a-z0-9]$/
VERSION =
"1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ S3DataStore


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dragonfly/s3_data_store.rb', line 28

def initialize(opts={})
  @bucket_name = opts[:bucket_name]
  @access_key_id = opts[:access_key_id]
  @secret_access_key = opts[:secret_access_key]
  @region = opts[:region]
  @storage_headers = opts[:storage_headers] || {'x-amz-acl' => 'public-read'}
  @url_scheme = opts[:url_scheme] || 'http'
  @url_host = opts[:url_host]
  @use_iam_profile = opts[:use_iam_profile]
  @root_path = opts[:root_path]
  @fog_storage_options = opts[:fog_storage_options] || {}
end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def access_key_id
  @access_key_id
end

#bucket_nameObject

Returns the value of attribute bucket_name


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def bucket_name
  @bucket_name
end

#fog_storage_optionsObject

Returns the value of attribute fog_storage_options


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def fog_storage_options
  @fog_storage_options
end

#regionObject

Returns the value of attribute region


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def region
  @region
end

#root_pathObject

Returns the value of attribute root_path


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def root_path
  @root_path
end

#secret_access_keyObject

Returns the value of attribute secret_access_key


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def secret_access_key
  @secret_access_key
end

#storage_headersObject

Returns the value of attribute storage_headers


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def storage_headers
  @storage_headers
end

#url_hostObject

Returns the value of attribute url_host


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def url_host
  @url_host
end

#url_schemeObject

Returns the value of attribute url_scheme


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def url_scheme
  @url_scheme
end

#use_iam_profileObject

Returns the value of attribute use_iam_profile


41
42
43
# File 'lib/dragonfly/s3_data_store.rb', line 41

def use_iam_profile
  @use_iam_profile
end

Instance Method Details

#bucket_exists?Boolean


104
105
106
107
108
109
# File 'lib/dragonfly/s3_data_store.rb', line 104

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

#destroy(uid) ⇒ Object


68
69
70
71
72
# File 'lib/dragonfly/s3_data_store.rb', line 68

def destroy(uid)
  rescuing_socket_errors{ storage.delete_object(bucket_name, full_path(uid)) }
rescue Excon::Errors::NotFound, Excon::Errors::Conflict => e
  Dragonfly.warn("#{self.class.name} destroy error: #{e}")
end

#domainObject


86
87
88
# File 'lib/dragonfly/s3_data_store.rb', line 86

def domain
  REGIONS[get_region]
end

#read(uid) ⇒ Object


60
61
62
63
64
65
66
# File 'lib/dragonfly/s3_data_store.rb', line 60

def read(uid)
  ensure_configured
  response = rescuing_socket_errors{ storage.get_object(bucket_name, full_path(uid)) }
  [response.body, headers_to_meta(response.headers)]
rescue Excon::Errors::NotFound => e
  nil
end

#storageObject


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dragonfly/s3_data_store.rb', line 90

def storage
  @storage ||= begin
    storage = Fog::Storage.new(fog_storage_options.merge({
      :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

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


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dragonfly/s3_data_store.rb', line 74

def url_for(uid, opts={})
  if expires = opts[:expires]
    storage.get_object_https_url(bucket_name, full_path(uid), expires, {:query => opts[:query]})
  else
    scheme = opts[:scheme] || url_scheme
    host   = opts[:host]   || url_host || (
      bucket_name =~ SUBDOMAIN_PATTERN ? "#{bucket_name}.s3.amazonaws.com" : "s3.amazonaws.com/#{bucket_name}"
    )
    "#{scheme}://#{host}/#{full_path(uid)}"
  end
end

#write(content, opts = {}) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dragonfly/s3_data_store.rb', line 43

def write(content, opts={})
  ensure_configured
  ensure_bucket_initialized

  headers = {'Content-Type' => content.mime_type}
  headers.merge!(opts[:headers]) if opts[:headers]
  uid = opts[:path] || generate_uid(content.name || 'file')

  rescuing_socket_errors do
    content.file do |f|
      storage.put_object(bucket_name, full_path(uid), f, full_storage_headers(headers, content.meta))
    end
  end

  uid
end