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

SUBDOMAIN_PATTERN =
/^[a-z0-9][a-z0-9.-]+[a-z0-9]$/
VERSION =
"1.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ S3DataStore

Returns a new instance of S3DataStore.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dragonfly/s3_data_store.rb', line 16

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.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def access_key_id
  @access_key_id
end

#bucket_nameObject

Returns the value of attribute bucket_name.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def bucket_name
  @bucket_name
end

#fog_storage_optionsObject

Returns the value of attribute fog_storage_options.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def fog_storage_options
  @fog_storage_options
end

#regionObject

Returns the value of attribute region.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def region
  @region
end

#root_pathObject

Returns the value of attribute root_path.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def root_path
  @root_path
end

#secret_access_keyObject

Returns the value of attribute secret_access_key.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def secret_access_key
  @secret_access_key
end

#storage_headersObject

Returns the value of attribute storage_headers.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def storage_headers
  @storage_headers
end

#url_hostObject

Returns the value of attribute url_host.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def url_host
  @url_host
end

#url_schemeObject

Returns the value of attribute url_scheme.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def url_scheme
  @url_scheme
end

#use_iam_profileObject

Returns the value of attribute use_iam_profile.



29
30
31
# File 'lib/dragonfly/s3_data_store.rb', line 29

def use_iam_profile
  @use_iam_profile
end

Instance Method Details

#bucket_exists?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/dragonfly/s3_data_store.rb', line 97

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

#destroy(uid) ⇒ Object



56
57
58
59
60
# File 'lib/dragonfly/s3_data_store.rb', line 56

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



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

def domain
  case region
  when 'us-east-1', nil
    's3.amazonaws.com'
  else
    "s3-#{region}.amazonaws.com"
  end
end

#read(uid) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/dragonfly/s3_data_store.rb', line 48

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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dragonfly/s3_data_store.rb', line 83

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



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

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dragonfly/s3_data_store.rb', line 31

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