paperclip-aws 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ gem 'paperclip', '>=2.3.0'
3
+ gem 'aws-sdk', '>=1.0.3'
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "shoulda", ">= 0"
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.6.4"
11
+ gem "rcov", ">= 0"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.9)
5
+ activesupport (= 3.0.9)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activerecord (3.0.9)
9
+ activemodel (= 3.0.9)
10
+ activesupport (= 3.0.9)
11
+ arel (~> 2.0.10)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.9)
14
+ arel (2.0.10)
15
+ aws-sdk (1.0.3)
16
+ httparty (~> 0.7)
17
+ json (~> 1.4)
18
+ nokogiri (>= 1.4.4)
19
+ uuidtools (~> 2.1)
20
+ builder (2.1.2)
21
+ cocaine (0.1.0)
22
+ crack (0.1.8)
23
+ git (1.2.5)
24
+ httparty (0.7.8)
25
+ crack (= 0.1.8)
26
+ i18n (0.5.0)
27
+ jeweler (1.6.4)
28
+ bundler (~> 1.0)
29
+ git (>= 1.2.5)
30
+ rake
31
+ json (1.5.3)
32
+ mime-types (1.16)
33
+ nokogiri (1.5.0)
34
+ paperclip (2.3.16)
35
+ activerecord (>= 2.3.0)
36
+ activesupport (>= 2.3.2)
37
+ cocaine (>= 0.0.2)
38
+ mime-types
39
+ rake (0.9.2)
40
+ rcov (0.9.9)
41
+ shoulda (2.11.3)
42
+ tzinfo (0.3.29)
43
+ uuidtools (2.1.2)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ aws-sdk (>= 1.0.3)
50
+ bundler (~> 1.0.0)
51
+ jeweler (~> 1.6.4)
52
+ paperclip (>= 2.3.0)
53
+ rcov
54
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Igor Alexandrov
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.md ADDED
@@ -0,0 +1,100 @@
1
+ # Storage module to official 'aws-sdk' gem for Amazon S3 #
2
+
3
+ 'paperclip-aws' is a full featured storage module that supports all S3 locations (US, European and Tokio) without any additional hacking.
4
+
5
+ Just install in and your model is ready to be stored all over ther world.
6
+
7
+ ## Requirements ##
8
+
9
+ * [paperclip][0] ~> 2.3
10
+ * [aws-sdk][1] >= 1.0.3
11
+
12
+ ## Installation ##
13
+
14
+ gem install papepclip-aws
15
+
16
+ After this add 'papepclip-aws' to your `Gemfile` or `environment.rb`
17
+
18
+ ## Using Storage ##
19
+
20
+ class SomeS3Attachment < ActiveRecord::Base
21
+ def self.s3_config
22
+ @@s3_config ||= YAML.load(ERB.new(File.read("#{Rails.root}/config/s3.yml")).result)[Rails.env]
23
+ end
24
+
25
+ has_attached_file :data,
26
+ :styles => {
27
+ :thumb => [">75x"],
28
+ :medium => [">600x"]
29
+ },
30
+ :storage => :aws,
31
+ :s3_credentials => {
32
+ :access_key_id => self.s3_config['access_key_id'],
33
+ :secret_access_key => self.s3_config['secret_access_key'],
34
+ :endpoint => self.s3_config['endpoint']
35
+ },
36
+ :s3_bucket => self.s3_config['bucket'],
37
+ :s3_host_alias => self.s3_config['s3_host_alias'],
38
+ :s3_acl => :public_read,
39
+ :s3_protocol => 'http',
40
+ :path => "company_documents/:id/:style/:data_file_name"
41
+ end
42
+
43
+ ## Possible options ##
44
+
45
+ ### :endpoint ###
46
+ Endpoint where your bucket is located. Default is `'s3.amazonaws.com'` which is for 'US Standard' region.
47
+
48
+ You can find full list of endpoints and regions [here](http://aws.amazon.com/articles/3912#s3)
49
+
50
+ ### :s3_acl ###
51
+ Sets permissions to your objects. Values are:
52
+
53
+ :private
54
+ :public_read
55
+ :public_read_write
56
+ :authenticated_read
57
+ :bucket_owner_read
58
+ :bucket_owner_full_control
59
+
60
+ ### :s3_protocol ###
61
+ Default protocol to use: `'http'` or `'https'`.
62
+
63
+ ## Get your data
64
+
65
+ 'paperclip-aws' redefines Paperclip `url` method to get object URL.
66
+
67
+ def url(style=default_style, options={})
68
+ end
69
+
70
+ Supported options are
71
+
72
+ * `:protocol` — `'http'` or `'https'`
73
+
74
+ Use this options to redefine default protocol, configured in model.
75
+
76
+ * `:expires`
77
+
78
+ Sets the expiration time of the URL; after this time S3 will return an error if the URL is used. This can be an integer (to specify the number of seconds after the current time), a string (which is parsed as a date using Time#parse), a Time, or a DateTime object. This option defaults to one hour after the current time.
79
+
80
+ Default is set to 3600 seconds.
81
+
82
+ * `:action`
83
+
84
+ Method, the HTTP verb or object method for which the returned URL will be valid. Valid values:
85
+
86
+ * `:get` or `:read`
87
+ * `:put` or `:write`
88
+ * `:delete`
89
+
90
+ Default is set to `:read`, which is the most common used.
91
+
92
+ ## Examples
93
+
94
+ Create link for file that will expire in 10 seconds after it was created. Useful when redirecting user to file.
95
+
96
+ file.data.url(:original, { :expires => Time.now + 10.seconds, :protocol => 'https' })
97
+
98
+
99
+ [0]: https://github.com/thoughtbot/paperclip
100
+ [1]: https://github.com/amazonwebservices/aws-sdk-for-ruby
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "paperclip-aws"
18
+ gem.homepage = "http://github.com/igor-alexandrov/paperclip-aws"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Storage module to official 'aws-sdk' gem for Amazon S3}
21
+ gem.description = %Q{'paperclip-aws' is a full featured storage module that supports all S3 locations (US, European and Tokio) without any additional hacking.}
22
+ gem.email = "igor.alexandrov@gmail.com"
23
+ gem.authors = ["Igor Alexandrov"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "paperclip-aws #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.0
@@ -0,0 +1,149 @@
1
+ # coding: UTF-8
2
+
3
+ require 'paperclip'
4
+
5
+ module Paperclip
6
+ module Storage
7
+ module Aws
8
+ def self.extended base
9
+ begin
10
+ require 'aws-sdk'
11
+ rescue LoadError => e
12
+ e.message << " (You may need to install the aws-sdk gem)"
13
+ raise e
14
+ end unless defined?(AWS)
15
+
16
+ base.instance_eval do
17
+ @s3_credentials = parse_credentials(@options[:s3_credentials])
18
+
19
+ # setup bucket
20
+ @s3_bucket = @options[:s3_bucket] || @s3_credentials[:bucket]
21
+ @s3_bucket = @s3_bucket.call(self) if @s3_bucket.is_a?(Proc)
22
+
23
+ # setup permissions
24
+ @s3_acl = @options[:s3_acl] || :public_read
25
+
26
+ # choose what storage class we use, 'standard' or 'reduced_redundancy'
27
+ @s3_storage_class = @options[:s3_storage_class] || :standard
28
+
29
+ @s3_protocol = @options[:s3_protocol] || 'http'
30
+ @s3_headers = @options[:s3_headers] || {}
31
+ @s3_host_alias = @options[:s3_host_alias]
32
+ @s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.is_a?(Proc)
33
+
34
+ @s3_endpoint = @s3_credentials[:endpoint] || 's3.amazonaws.com'
35
+
36
+ @s3 = AWS::S3.new(
37
+ :access_key_id => @s3_credentials[:access_key_id],
38
+ :secret_access_key => @s3_credentials[:secret_access_key],
39
+ :s3_endpoint => @s3_endpoint
40
+ )
41
+ end
42
+ end
43
+
44
+ def url(style=default_style, options={})
45
+ if options[:expires].present? || options[:action].present?
46
+ options.reverse_merge!({
47
+ :expires => 60*60,
48
+ :action => :read
49
+ })
50
+ secure = ( self.choose_protocol(options) == 'https' )
51
+ @s3.buckets[@s3_bucket].objects[path(style)].url_for(options[:action], { :secure => secure, :expires => options[:expires] }).to_s
52
+ else
53
+ if @s3_host_alias.present?
54
+ "#{choose_protocol(options)}://#{@s3_host_alias}/#{path(style).gsub(%r{^/}, "")}"
55
+ else
56
+ "#{choose_protocol(options)}://#{@s3_endpoint}/#{@s3_bucket}/#{path(style).gsub(%r{^/}, "")}"
57
+ end
58
+ end
59
+ end
60
+
61
+ def bucket_name
62
+ @s3_bucket
63
+ end
64
+
65
+ def parse_credentials(creds)
66
+ creds = find_credentials(creds).stringify_keys
67
+ env = Object.const_defined?(:Rails) ? Rails.env : nil
68
+ (creds[env] || creds).symbolize_keys
69
+ end
70
+
71
+ def exists?(style = default_style)
72
+ @s3.buckets[@s3_bucket].objects[path(style)].exists?
73
+ end
74
+
75
+ def choose_protocol(options={})
76
+ if options[:protocol].present?
77
+ return options[:protocol].to_s
78
+ else
79
+ return @s3_protocol
80
+ end
81
+ end
82
+
83
+ # Returns representation of the data of the file assigned to the given
84
+ # style, in the format most representative of the current storage.
85
+ def to_file style = default_style
86
+ return @queued_for_write[style] if @queued_for_write[style]
87
+ filename = path(style)
88
+ extname = File.extname(filename)
89
+ basename = File.basename(filename, extname)
90
+ file = Tempfile.new([basename, extname])
91
+ file.binmode
92
+ file.write(@s3.buckets[@s3_bucket].objects[path(style)].read)
93
+ file.rewind
94
+ return file
95
+ end
96
+
97
+ def create_bucket
98
+ @s3.buckets.create(@s3_bucket)
99
+ end
100
+
101
+ def flush_writes #:nodoc:
102
+ @queued_for_write.each do |style, file|
103
+ begin
104
+ log("saving #{path(style)}")
105
+
106
+ @s3.buckets[@s3_bucket].objects[path(style)].write(
107
+ file,
108
+ :acl => @s3_acl,
109
+ :storage_class => @s3_storage_class
110
+ )
111
+ rescue AWS::S3::Errors::NoSuchBucket => e
112
+ create_bucket
113
+ retry
114
+ rescue AWS::S3::Errors::Base => e
115
+ raise
116
+ end
117
+ end
118
+ @queued_for_write = {}
119
+ end
120
+
121
+ def flush_deletes #:nodoc:
122
+ @queued_for_delete.each do |path|
123
+ begin
124
+ log("deleting #{path}")
125
+ @s3.buckets[@s3_bucket].objects[path].delete
126
+ rescue AWS::S3::Base => e
127
+ raise
128
+ end
129
+ end
130
+ @queued_for_delete = []
131
+ end
132
+
133
+ def find_credentials creds
134
+ case creds
135
+ when File
136
+ YAML::load(ERB.new(File.read(creds.path)).result)
137
+ when String, Pathname
138
+ YAML::load(ERB.new(File.read(creds)).result)
139
+ when Hash
140
+ creds
141
+ else
142
+ raise ArgumentError, "Credentials are not a path, file, or hash."
143
+ end
144
+ end
145
+ private :find_credentials
146
+
147
+ end
148
+ end
149
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'papepclip-aws'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestPapepclipAws < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-aws
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Igor Alexandrov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: paperclip
16
+ requirement: &70127719039620 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70127719039620
25
+ - !ruby/object:Gem::Dependency
26
+ name: aws-sdk
27
+ requirement: &70127719038940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.3
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70127719038940
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ requirement: &70127719038340 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70127719038340
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &70127719037660 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70127719037660
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &70127719036700 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.6.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70127719036700
69
+ - !ruby/object:Gem::Dependency
70
+ name: rcov
71
+ requirement: &70127719035940 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70127719035940
80
+ description: ! '''paperclip-aws'' is a full featured storage module that supports
81
+ all S3 locations (US, European and Tokio) without any additional hacking.'
82
+ email: igor.alexandrov@gmail.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files:
86
+ - LICENSE.txt
87
+ - README.md
88
+ files:
89
+ - .document
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - VERSION
96
+ - lib/papepclip-aws.rb
97
+ - test/helper.rb
98
+ - test/test_papepclip-aws.rb
99
+ homepage: http://github.com/igor-alexandrov/paperclip-aws
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ segments:
113
+ - 0
114
+ hash: 488569729423285379
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.7.2
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Storage module to official 'aws-sdk' gem for Amazon S3
127
+ test_files: []