Class: Dragonfly::DataStorage::S3DataStore
- Inherits:
-
Object
- Object
- Dragonfly::DataStorage::S3DataStore
show all
- Includes:
- Configurable, Serializer
- Defined in:
- lib/dragonfly/data_storage/s3data_store.rb
Constant Summary
- REGIONS =
{
'us-east-1' => 's3.amazonaws.com',
'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
-
bucket_name
- default nil
-
access_key_id
- default nil
-
secret_access_key
- default nil
-
region
- default nil
-
use_iam_profile
- default nil
-
use_filesystem
- default true
-
storage_headers
- default {"x-amz-acl"=>"public-read"}
-
url_scheme
- default "http"
-
url_host
- default nil
Instance Method Summary
(collapse)
Methods included from Serializer
#b64_decode, #b64_encode, #json_decode, #json_encode, #marshal_decode, #marshal_encode
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?
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)
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
|
95
96
97
|
# File 'lib/dragonfly/data_storage/s3data_store.rb', line 95
def domain
REGIONS[get_region]
end
|
- (Object) retrieve(uid)
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,
parse_s3_metadata(response.)
]
rescue Excon::Errors::NotFound => e
raise DataNotFound, "#{e} - #{uid}"
end
|
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
= opts[:headers] || {}
mime_type = opts[:mime_type] || opts[:content_type]
['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, (, temp_object.meta))
end
else
storage.put_object(bucket_name, uid, temp_object.data, (, 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)
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
|