s3asy 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +27 -10
- data/Rakefile +7 -7
- data/lib/s3_object_proxy.rb +28 -9
- data/s3asy.gemspec +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -4,9 +4,18 @@ Rails gem for associating an s3 object with an active record model
|
|
4
4
|
|
5
5
|
== Install
|
6
6
|
|
7
|
-
gem install robertoles-s3asy --source http://
|
7
|
+
gem install robertoles-s3asy --source http://gemcutter.org
|
8
|
+
|
9
|
+
== Config
|
10
|
+
|
11
|
+
Create a file /config/amazon_s3.yml in your rails application, with the following contents...
|
12
|
+
|
13
|
+
test:
|
14
|
+
access_key_id: your_access_key_id
|
15
|
+
secret_access_key: your_secret_access_key
|
16
|
+
bucket: your_default_bucket
|
8
17
|
|
9
|
-
==
|
18
|
+
== Associate s3 object to active record usage
|
10
19
|
|
11
20
|
You want to create an active record model 'product' which has an attribute 'image' which is an s3 object. First create a migration for your model and specify a string column for the image attribute followed by _name...
|
12
21
|
|
@@ -23,21 +32,29 @@ You want to create an active record model 'product' which has an attribute 'imag
|
|
23
32
|
drop_table :products
|
24
33
|
end
|
25
34
|
end
|
26
|
-
|
27
|
-
|
28
|
-
Now you will need to specify your s3 credentials, to do this add the file /config/amazon_s3.yml
|
29
|
-
|
30
|
-
test:
|
31
|
-
access_key_id: your_access_key_id
|
32
|
-
secret_access_key: your_secret_access_key
|
33
35
|
|
34
36
|
|
35
37
|
Finally create the association in your model, specifying the bucket on s3 the object is stored in...
|
36
38
|
|
37
39
|
class Product < ActiveRecord::Base
|
38
|
-
has_s3object :image
|
40
|
+
has_s3object :image
|
39
41
|
end
|
40
42
|
|
43
|
+
The has_s3object method will accept the follow options as a hash...
|
44
|
+
|
45
|
+
:bucket => 'bucket_name' # will look for the object in this bucket instead of the default.
|
46
|
+
|
47
|
+
|
48
|
+
== Search s3 objects in a bucket
|
49
|
+
|
50
|
+
You can search objects in an s3 bucket using the search function on the S3ObjectProxy class...
|
51
|
+
|
52
|
+
S3ObjectProxy.search('prefix_to_search_by')
|
53
|
+
# => [<AWS::S3::S3Object]
|
54
|
+
|
55
|
+
This function takes an options parameter hash where you can provide a bucket to search in (which overrides the default set in the config file)
|
56
|
+
|
57
|
+
:bucket => 'not_the_default_bucket'
|
41
58
|
|
42
59
|
== License
|
43
60
|
|
data/Rakefile
CHANGED
@@ -3,7 +3,12 @@ require 'rake'
|
|
3
3
|
require 'spec/rake/spectask'
|
4
4
|
require 'echoe'
|
5
5
|
|
6
|
-
|
6
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
7
|
+
t.spec_opts = ["-c"]
|
8
|
+
t.spec_files = FileList['spec/**/*.rb']
|
9
|
+
end
|
10
|
+
|
11
|
+
Echoe.new('s3asy', '0.1.1') do |p|
|
7
12
|
p.description = "Associate an s3 object to an active record model"
|
8
13
|
p.url = "http://github.com/robertoles/s3asy"
|
9
14
|
p.author = "Robert Oles"
|
@@ -12,9 +17,4 @@ Echoe.new('s3asy', '0.1.0') do |p|
|
|
12
17
|
p.development_dependencies = ['aws-s3']
|
13
18
|
end
|
14
19
|
|
15
|
-
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
16
|
-
|
17
|
-
Spec::Rake::SpecTask.new('spec') do |t|
|
18
|
-
t.spec_opts = ["-c"]
|
19
|
-
t.spec_files = FileList['spec/**/*.rb']
|
20
|
-
end
|
20
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/lib/s3_object_proxy.rb
CHANGED
@@ -6,20 +6,39 @@ class S3ObjectProxy
|
|
6
6
|
|
7
7
|
def initialize(options = {})
|
8
8
|
@path = options[:path]
|
9
|
-
@bucket = options[:bucket]
|
10
9
|
initialize_s3(options)
|
11
10
|
end
|
12
11
|
|
12
|
+
def url
|
13
|
+
AWS::S3::S3Object.url_for(path, bucket, :authenticated => false)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.search(prefix, options = {})
|
17
|
+
options = load_yaml().merge(options)
|
18
|
+
connect_to_s3(options)
|
19
|
+
AWS::S3::Bucket.objects(options[:bucket], :prefix => prefix)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def self.load_yaml(options = {})
|
24
|
+
s3_config_path = S3ObjectProxy.const_defined?(:CONFIG_PATH) ? S3ObjectProxy.const_get(:CONFIG_PATH) : (RAILS_ROOT + '/config/amazon_s3.yml')
|
25
|
+
YAML.load_file(s3_config_path)[RAILS_ENV].symbolize_keys
|
26
|
+
end
|
27
|
+
|
13
28
|
def initialize_s3(options)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
:access_key_id => s3_config[:access_key_id],
|
18
|
-
:secret_access_key => s3_config[:secret_access_key]
|
19
|
-
)
|
29
|
+
options = S3ObjectProxy.load_yaml(options).merge(options)
|
30
|
+
S3ObjectProxy.connect_to_s3(options)
|
31
|
+
init_bucket(options)
|
20
32
|
end
|
21
33
|
|
22
|
-
def
|
23
|
-
|
34
|
+
def init_bucket(options)
|
35
|
+
@bucket = options[:bucket]
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.connect_to_s3(options)
|
39
|
+
AWS::S3::Base.establish_connection!(
|
40
|
+
:access_key_id => options[:access_key_id],
|
41
|
+
:secret_access_key => options[:secret_access_key]
|
42
|
+
)
|
24
43
|
end
|
25
44
|
end
|
data/s3asy.gemspec
CHANGED