dm-s3 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +77 -0
- data/Rakefile +58 -0
- data/VERSION.yml +4 -0
- data/dm-s3.gemspec +52 -0
- data/lib/dm-s3.rb +116 -0
- data/test/dm-s3_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +85 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Roberto Thais
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
= dm-s3
|
2
|
+
|
3
|
+
Very often we want to store a file in s3 and keep a record of it in our database.
|
4
|
+
|
5
|
+
== Setting
|
6
|
+
|
7
|
+
If you have a model class that will have s3 objects living in different buckets do
|
8
|
+
|
9
|
+
property :s3_key, String
|
10
|
+
property :s3_bucket, String
|
11
|
+
|
12
|
+
has_s3_file
|
13
|
+
|
14
|
+
and define a method to save your resource and your s3 object
|
15
|
+
|
16
|
+
def self.create!(key, bucket, file, options)
|
17
|
+
object = self.new :s3_key => key, :s3_bucket => bucket
|
18
|
+
object.save if object.store_with(file, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
If all the s3 objects will live in the same bucket then do
|
22
|
+
|
23
|
+
property :s3_key, String
|
24
|
+
|
25
|
+
has_s3_file_at 'my-bucket-name'
|
26
|
+
|
27
|
+
The save method will reflect the difference
|
28
|
+
|
29
|
+
def self.create!(key, file, options)
|
30
|
+
object = self.new :s3_key => key
|
31
|
+
object.save if object.store_with(file, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
You can directly access the bucket object associated with this model too
|
35
|
+
|
36
|
+
MyModel.bucket
|
37
|
+
|
38
|
+
== Getting
|
39
|
+
|
40
|
+
To retrieve data from s3 simply pull the datamapper object, e.g.
|
41
|
+
|
42
|
+
object = MyModel.first
|
43
|
+
|
44
|
+
Doing
|
45
|
+
|
46
|
+
object.s3
|
47
|
+
|
48
|
+
Will give you the s3 object. So you can do things like
|
49
|
+
|
50
|
+
object.s3.value
|
51
|
+
object.s3.url
|
52
|
+
object.s3.about
|
53
|
+
|
54
|
+
etc.
|
55
|
+
|
56
|
+
For convenience, the methods 'value', 'metadata', 'about' and 'url' get bound to the datamapper object directly so the following is valid
|
57
|
+
|
58
|
+
object.value
|
59
|
+
object.metadata
|
60
|
+
object.about
|
61
|
+
object.url
|
62
|
+
|
63
|
+
Make sure these don't conflict with your properties.
|
64
|
+
|
65
|
+
== Gotchas
|
66
|
+
|
67
|
+
* The gem assumes that you will call
|
68
|
+
|
69
|
+
AWS::S3::Base.establish_connection!
|
70
|
+
|
71
|
+
with the appropriate credentials some time before you create or access your s3 enabled models
|
72
|
+
|
73
|
+
* The gem won't create buckets for you. Make sure the buckets exist before attempting to use them.
|
74
|
+
|
75
|
+
== Copyright
|
76
|
+
|
77
|
+
Copyright (c) 2009 Roberto Thais. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "dm-s3"
|
8
|
+
gem.summary = "A simple gem that makes it easy to associate datamapper objects with s3 objects"
|
9
|
+
gem.email = "roberto.n.thais@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/rpbertp13/dm-s3"
|
11
|
+
gem.authors = ["Roberto Thais"]
|
12
|
+
gem.add_dependency('aws-s3', '>= 0.6.2')
|
13
|
+
gem.add_dependency('dm-core', '>= 0.9.11')
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
if File.exist?('VERSION.yml')
|
47
|
+
config = YAML.load(File.read('VERSION.yml'))
|
48
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
49
|
+
else
|
50
|
+
version = ""
|
51
|
+
end
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "dm-s3 #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
58
|
+
|
data/VERSION.yml
ADDED
data/dm-s3.gemspec
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{dm-s3}
|
5
|
+
s.version = "0.1.5"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Roberto Thais"]
|
9
|
+
s.date = %q{2009-07-10}
|
10
|
+
s.email = %q{roberto.n.thais@gmail.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION.yml",
|
22
|
+
"dm-s3.gemspec",
|
23
|
+
"lib/dm-s3.rb",
|
24
|
+
"test/dm-s3_test.rb",
|
25
|
+
"test/test_helper.rb"
|
26
|
+
]
|
27
|
+
s.homepage = %q{http://github.com/rpbertp13/dm-s3}
|
28
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.rubygems_version = %q{1.3.4}
|
31
|
+
s.summary = %q{A simple gem that makes it easy to associate datamapper objects with s3 objects}
|
32
|
+
s.test_files = [
|
33
|
+
"test/dm-s3_test.rb",
|
34
|
+
"test/test_helper.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_runtime_dependency(%q<aws-s3>, [">= 0.6.2"])
|
43
|
+
s.add_runtime_dependency(%q<dm-core>, [">= 0.9.11"])
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<aws-s3>, [">= 0.6.2"])
|
46
|
+
s.add_dependency(%q<dm-core>, [">= 0.9.11"])
|
47
|
+
end
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<aws-s3>, [">= 0.6.2"])
|
50
|
+
s.add_dependency(%q<dm-core>, [">= 0.9.11"])
|
51
|
+
end
|
52
|
+
end
|
data/lib/dm-s3.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
require 'aws/s3'
|
3
|
+
|
4
|
+
module DataMapper
|
5
|
+
module S3
|
6
|
+
|
7
|
+
class BaseBucketConnector
|
8
|
+
def initialize
|
9
|
+
@s3 = AWS::S3::S3Object
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_s3_object(instance)
|
13
|
+
@s3.find(instance.s3_key)
|
14
|
+
end
|
15
|
+
|
16
|
+
def store(instance, file, options)
|
17
|
+
@s3.store(instance.s3_key, file, options)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class InstanceBucketConnector < BaseBucketConnector
|
22
|
+
def get_s3_object(instance)
|
23
|
+
s3(instance)
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
def store(instance, file, options)
|
28
|
+
s3(instance)
|
29
|
+
super
|
30
|
+
end
|
31
|
+
|
32
|
+
def s3(instance)
|
33
|
+
@s3.set_current_bucket_to instance.s3_bucket
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class ClassBucketConnector < BaseBucketConnector
|
38
|
+
def initialize(klass, bucket_name)
|
39
|
+
@class = klass
|
40
|
+
@bucket_name = bucket_name
|
41
|
+
super()
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_s3_object(instance)
|
45
|
+
self.s3
|
46
|
+
super
|
47
|
+
end
|
48
|
+
|
49
|
+
def store(instance, file, options)
|
50
|
+
s3
|
51
|
+
super
|
52
|
+
end
|
53
|
+
|
54
|
+
def bucket
|
55
|
+
AWS::S3::Bucket.find @bucket_name
|
56
|
+
end
|
57
|
+
|
58
|
+
def s3
|
59
|
+
@s3.set_current_bucket_to @bucket_name
|
60
|
+
@s3
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
module InstanceMethods
|
65
|
+
def s3
|
66
|
+
self.class.connector.get_s3_object(self)
|
67
|
+
end
|
68
|
+
|
69
|
+
def store_with(file, options = nil)
|
70
|
+
self.class.connector.store(self, file, options)
|
71
|
+
end
|
72
|
+
|
73
|
+
#Shortcut method for urls - defaults to unauthenticated
|
74
|
+
def url(options = {:authenticated => false})
|
75
|
+
s3.url(options)
|
76
|
+
end
|
77
|
+
|
78
|
+
[:value, :metadata, :about].each do |m|
|
79
|
+
send :define_method, m do
|
80
|
+
s3.send(m)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
module ClassMethods
|
86
|
+
#Auto-add the properties in future version
|
87
|
+
|
88
|
+
def has_s3_file_at(bucket_name)
|
89
|
+
@connector = ClassBucketConnector.new(self, bucket_name)
|
90
|
+
|
91
|
+
class << self
|
92
|
+
attr_reader :connector
|
93
|
+
|
94
|
+
send :define_method, :s3 do
|
95
|
+
self.connector.s3
|
96
|
+
end
|
97
|
+
|
98
|
+
send :define_method, :bucket do
|
99
|
+
self.connector.bucket
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
send :after_class_method, :inherited do |retval, subclass|
|
104
|
+
subclass.instance_variable_set "@connector", @connector
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def has_s3_file
|
109
|
+
@connector = InstanceBucketConnector.new
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
114
|
+
Resource.append_inclusions(S3::InstanceMethods)
|
115
|
+
Model.append_extensions(S3::ClassMethods)
|
116
|
+
end
|
data/test/dm-s3_test.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-s3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roberto Thais
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-10 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: aws-s3
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.6.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: dm-core
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.11
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: roberto.n.thais@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION.yml
|
51
|
+
- dm-s3.gemspec
|
52
|
+
- lib/dm-s3.rb
|
53
|
+
- test/dm-s3_test.rb
|
54
|
+
- test/test_helper.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/rpbertp13/dm-s3
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.3.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: A simple gem that makes it easy to associate datamapper objects with s3 objects
|
83
|
+
test_files:
|
84
|
+
- test/dm-s3_test.rb
|
85
|
+
- test/test_helper.rb
|