carrierwave-aws 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ .config
4
+ Gemfile.lock
5
+ bin
6
+ vendor
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-aws-sdk.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Parker Selbert
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # Carrierwave AWS Storage
2
+
3
+ Use the officially supported AWS-SDK library for S3 storage rather than relying
4
+ on fog. There are several things going for it:
5
+
6
+ * Full featured, it supports more of the API than Fog
7
+ * Significantly smaller footprint
8
+ * Fewer dependencies
9
+ * Clear documentation
10
+
11
+ Here is a simple comparison table [02/04/2013]
12
+
13
+ | Library | Disk Space | Lines of Code | Boot Time | Runtime Deps | Develop Deps |
14
+ | ------- | ---------- | ------------- | --------- | ------------ | ------------ |
15
+ | fog | 28.0M | 133469 | 0.693 | 9 | 11 |
16
+ | aws-sdk | 4.4M | 80017 | 0.098 | 3 | 8 |
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ gem 'carrierwave-aws'
23
+
24
+ ## Usage
25
+
26
+ Configure and use it just like you would Fog. The only notable difference is
27
+ the use of `aws_bucket` instead of `fog_directory`, and `aws_acl` instead of
28
+ `fog_public`.
29
+
30
+ ```ruby
31
+ CarrierWave.configure do |config|
32
+ config.storage = :aws
33
+ config.aws_bucket = ENV['S3_BUCKET_NAME']
34
+ config.aws_acl = :public_read
35
+ config.aws_authenticated_url_expiration = 60 * 60 * 24 * 365
36
+
37
+ config.aws_credentials = {
38
+ access_key_id: ENV['AWS_ACCESS_KEY_ID'],
39
+ secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
40
+ }
41
+ end
42
+ ```
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave/aws/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'carrierwave-aws'
8
+ gem.version = Carrierwave::AWS::VERSION
9
+ gem.authors = ['Parker Selbert']
10
+ gem.email = ['parker@sorentwo.com']
11
+ gem.description = %q{Use aws-sdk for S3 support in CarrierWave}
12
+ gem.summary = %q{A slimmer alternative to using Fog for S3 support in CarrierWave}
13
+ gem.homepage = "https://github.com/sorentwo/carrierwave-aws-sdk"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(spec)/})
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_dependency 'carrierwave', '>= 0.7.0'
20
+ gem.add_dependency 'aws-sdk', '~> 1.8.1.2'
21
+
22
+ gem.add_development_dependency 'rspec', '~> 2.12.0'
23
+ end
@@ -0,0 +1,15 @@
1
+ require 'carrierwave'
2
+ require 'carrierwave/aws/version'
3
+ require 'carrierwave/storage/aws'
4
+
5
+ class CarrierWave::Uploader::Base
6
+ add_config :aws_attributes
7
+ add_config :aws_authenticated_url_expiration
8
+ add_config :aws_credentials
9
+ add_config :aws_bucket
10
+ add_config :aws_acl
11
+
12
+ configure do |config|
13
+ config.storage_engines[:aws] = 'CarrierWave::Storage::AWS'
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Carrierwave
2
+ module AWS
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,117 @@
1
+ require 'aws/s3'
2
+
3
+ module CarrierWave
4
+ module Storage
5
+ class AWS < Abstract
6
+ def self.connection_cache
7
+ @connection_cache ||= {}
8
+ end
9
+
10
+ def store!(file)
11
+ File.new(uploader, self, uploader.store_path).tap do |aws_file|
12
+ aws_file.store(file)
13
+ end
14
+ end
15
+
16
+ def retrieve!(identifier)
17
+ File.new(uploader, self, uploader.store_path(identifier))
18
+ end
19
+
20
+ def connection
21
+ @connection ||= begin
22
+ credentials = uploader.aws_credentials
23
+ self.class.connection_cache[credentials] ||= ::AWS::S3.new(credentials)
24
+ end
25
+ end
26
+
27
+ class File
28
+ attr_reader :uploader, :base, :path
29
+
30
+ def initialize(uploader, base, path)
31
+ @uploader, @base, @path = uploader, base, path
32
+ end
33
+
34
+ def attributes
35
+ file.attributes
36
+ end
37
+
38
+ def content_type
39
+ @content_type || file.content_type
40
+ end
41
+
42
+ def content_type=(new_content_type)
43
+ @content_type = new_content_type
44
+ end
45
+
46
+ def delete
47
+ file.delete
48
+ end
49
+
50
+ def extension
51
+ path.split('.').last
52
+ end
53
+
54
+ def read
55
+ file.read
56
+ end
57
+
58
+ def size
59
+ file.content_length
60
+ end
61
+
62
+ def exists?
63
+ !!file
64
+ end
65
+
66
+ def store(new_file)
67
+ aws_file = new_file.to_file
68
+
69
+ @file = bucket.objects[path].write(aws_file, {
70
+ acl: uploader.aws_acl,
71
+ content_type: new_file.content_type
72
+ }.merge(uploader.aws_attributes))
73
+
74
+ aws_file.close unless aws_file.closed?
75
+
76
+ true
77
+ end
78
+
79
+ def authenticated_url
80
+ file.url_for(:read, expires: uploader.aws_authenticated_url_expiration).to_s
81
+ end
82
+
83
+ def public_url
84
+ file.public_url.to_s
85
+ end
86
+
87
+ def url(options = {})
88
+ if uploader.aws_acl != :public_read
89
+ authenticated_url
90
+ else
91
+ public_url
92
+ end
93
+ end
94
+
95
+ def filename(options = {})
96
+ if file_url = url(options)
97
+ file_url.gsub(/.*\/(.*?$)/, '\1')
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def connection
104
+ base.connection
105
+ end
106
+
107
+ def bucket
108
+ @bucket ||= connection.buckets[uploader.aws_bucket]
109
+ end
110
+
111
+ def file
112
+ @file ||= bucket.objects[path]
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::Uploader::Base do
4
+ it 'defines aws specific storage options' do
5
+ described_class.should respond_to(:aws_attributes)
6
+ end
7
+
8
+ it 'inserts aws as a known storage engine' do
9
+ described_class.configure do |config|
10
+ config.storage_engines.should have_key(:aws)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+ require 'carrierwave'
3
+ require 'carrierwave-aws'
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::Storage::AWS do
4
+ let(:credentials) { { access_key_id: 'abc', secret_access_key: '123' } }
5
+ let(:uploader) { mock(:uploader, aws_credentials: credentials) }
6
+ let(:storage) { described_class.new(uploader) }
7
+
8
+ describe '#connection' do
9
+ it 'instantiates a new connection' do
10
+ AWS::S3.should_receive(:new).with(credentials)
11
+
12
+ storage.connection
13
+ end
14
+
15
+ it 'caches connections by credentials' do
16
+ AWS::S3.should_receive(:new).with(credentials).and_return(mock)
17
+
18
+ storage.connection.should === storage.connection
19
+ end
20
+ end
21
+ end
22
+
23
+ describe CarrierWave::Storage::AWS::File do
24
+ let(:file) { mock(:file, read: '0101010') }
25
+ let(:objects) { { 'files/1/file.txt' => file } }
26
+ let(:bucket) { mock(:bucket, objects: objects) }
27
+ let(:connection) { mock(:connection, buckets: { 'example-com' => bucket }) }
28
+ let(:uploader) { mock(:uploader, aws_bucket: 'example-com') }
29
+ let(:storage) { mock(:storage, connection: connection) }
30
+ let(:path) { 'files/1/file.txt' }
31
+
32
+ subject(:aws_file) { described_class.new(uploader, storage, path) }
33
+
34
+ describe '#read' do
35
+ it 'reads from the remote file object' do
36
+ aws_file.read.should == '0101010'
37
+ end
38
+ end
39
+
40
+ describe '#authenticated_url' do
41
+ it 'requests a url for reading with the configured expiration' do
42
+ uploader.stub(aws_authenticated_url_expiration: 60)
43
+
44
+ file.should_receive(:url_for).with(:read, expires: 60)
45
+
46
+ aws_file.authenticated_url
47
+ end
48
+ end
49
+
50
+ describe '#url' do
51
+ it 'requests a public url if acl is public readable' do
52
+ uploader.stub(aws_acl: :public_read)
53
+ file.should_receive(:public_url)
54
+
55
+ aws_file.url
56
+ end
57
+
58
+ it 'requests an authenticated url if acl is not public readable' do
59
+ uploader.stub(aws_acl: :private, aws_authenticated_url_expiration: 60)
60
+
61
+ file.should_receive(:url_for)
62
+
63
+ aws_file.url
64
+ end
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-aws
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Parker Selbert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: carrierwave
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.7.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.7.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: aws-sdk
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.1.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.1.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.12.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.12.0
62
+ description: Use aws-sdk for S3 support in CarrierWave
63
+ email:
64
+ - parker@sorentwo.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - carrierwave-aws.gemspec
75
+ - lib/carrierwave-aws.rb
76
+ - lib/carrierwave/aws/version.rb
77
+ - lib/carrierwave/storage/aws.rb
78
+ - spec/carrierwave-aws_spec.rb
79
+ - spec/spec_helper.rb
80
+ - spec/storage/aws_spec.rb
81
+ homepage: https://github.com/sorentwo/carrierwave-aws-sdk
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.23
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: A slimmer alternative to using Fog for S3 support in CarrierWave
105
+ test_files:
106
+ - spec/carrierwave-aws_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/storage/aws_spec.rb