carrierwave-s3cn 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9d526651a0a8f75f77e18149613e5b2ef34ea3e
4
+ data.tar.gz: 7fc4f426e22ea6081314f8eee4876be51ba87448
5
+ SHA512:
6
+ metadata.gz: f3ad1b451d0b6c3295e29d2ae1243e0117dc2fbe9a6fcaa82193a01941f1b90b960b17bf826ea98db29ee3ac6aa8fd659828a3ed8b366e28f01781b717161f57
7
+ data.tar.gz: aedebe5d62cf54e579f96fed62510165e3044ef2ca62f304842f9b104f228d4645a4e9bf78f20804992917a08d05061ed82c565b6e1662d7085b9298b9b5ca96
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-s3cn.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 xudeke
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,70 @@
1
+ # CarrierWave::S3cn
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'carrierwave-s3cn'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install carrierwave-s3cn
20
+
21
+ ## Usage
22
+
23
+ You'll need to configure it in config/initializes/carrierwave.rb
24
+
25
+ ```ruby
26
+ ::CarrierWave.configure do |config|
27
+ config.storage = :s3cn
28
+ config.s3cn_region = "cn-north-1"
29
+ config.s3cn_endpoint = '' #optional
30
+ config.s3cn_bucket = "s3 bucket name"
31
+ config.s3cn_access_key_id = "中国 s3 access_key_id"
32
+ config.s3cn_secret_access_key = "中国 s3 secret_access_key"
33
+ config.s3cn_bucket_private = true #default true
34
+ config.s3cn_protocol = "https" #default https
35
+ end
36
+ ```
37
+
38
+ For more information on qiniu, please read http://docs.aws.amazon.com/general/latest/gr/isolated_regions.html
39
+
40
+ And then in your uploader, set the storage to `:qiniu`:
41
+
42
+ ```ruby
43
+ class AvatarUploader < CarrierWave::Uploader::Base
44
+ storage :s3cn
45
+ end
46
+ ```
47
+
48
+ You can override configuration item in individual uploader like this:
49
+
50
+ ```ruby
51
+ class AvatarUploader < CarrierWave::Uploader::Base
52
+ storage :s3cn
53
+
54
+ self.s3cn_bucket = "avatars"
55
+ self.s3cn_access_key_id = ""
56
+ self.s3cn_secret_access_key = ''
57
+ self.qiniu_bucket_private= true #default is false
58
+
59
+ end
60
+ ```
61
+ You can see a example project on: https://github.com/dekexu/carrierwave-s3cn
62
+ or see the spec test on https://github.com/dekexu/carrierwave-s3cn
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/dekexu/carrierwave-s3cn/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require "bundler/gem_tasks"
3
+ desc "Open an irb session preloaded with this library"
4
+ task :console do
5
+ exec "irb -rubygems -I ./lib -r ./lib/carrierwave/s3cn.rb"
6
+ end
7
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave/s3cn/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carrierwave-s3cn"
8
+ spec.version = CarrierWave::S3cn::VERSION
9
+ spec.authors = ["xudeke"]
10
+ spec.email = ["xudeke@gmail.com"]
11
+ spec.summary = %q{S3 storage, in china, plugin for CarrierWave }
12
+ spec.description = %q{S3 storage, in china, plugin for CarrierWave}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "carrierwave"
22
+ spec.add_dependency "aws-sdk-v1"
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,10 @@
1
+ require "carrierwave/s3cn/version"
2
+ require "carrierwave/s3cn/storage"
3
+ require "carrierwave/s3cn/configuration"
4
+
5
+ ::CarrierWave.configure do |config|
6
+ config.storage_engines.merge!({:s3cn => "::CarrierWave::Storage::S3cn"})
7
+ end
8
+
9
+ ::CarrierWave::Uploader::Base.send(:include, ::CarrierWave::S3cn::Configuration)
10
+
@@ -0,0 +1,36 @@
1
+ #encoding: utf-8
2
+ module CarrierWave
3
+ module S3cn
4
+ module Configuration
5
+ extend ActiveSupport::Concern
6
+ included do
7
+ add_config :s3cn_region
8
+ add_config :s3cn_endpoint
9
+ add_config :s3cn_bucket
10
+ add_config :s3cn_access_key_id
11
+ add_config :s3cn_secret_access_key
12
+ add_config :s3cn_bucket_private
13
+ add_config :s3cn_protocol
14
+ alias_config :s3cn_protocal, :s3cn_protocol
15
+ end
16
+
17
+ module ClassMethods
18
+ def alias_config(new_name, old_name)
19
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
20
+ def self.#{new_name}(value=nil)
21
+ self.#{old_name}(value)
22
+ end
23
+
24
+ def self.#{new_name}=(value)
25
+ self.#{old_name}=(value)
26
+ end
27
+
28
+ def #{new_name}
29
+ #{old_name}
30
+ end
31
+ RUBY
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,134 @@
1
+ # encoding: utf-8
2
+ require 'carrierwave'
3
+ require 'aws-sdk-v1'
4
+
5
+ module CarrierWave
6
+ module Storage
7
+ class S3cn < Abstract
8
+
9
+ class Connection
10
+ def initialize(options={})
11
+ @s3cn_region = options[:s3cn_region] || 'cn-north-1'
12
+ @s3cn_endpoint = options[:s3cn_endpoint]
13
+ @s3cn_bucket = options[:s3cn_bucket]
14
+ @s3cn_access_key_id = options[:s3cn_access_key_id]
15
+ @s3cn_secret_access_key = options[:s3cn_secret_access_key]
16
+ @s3cn_protocol = options[:s3cn_protocol]
17
+ @s3cn_bucket_private = options[:s3cn_bucket_private] || true
18
+ init
19
+ end
20
+
21
+ def store(file, key)
22
+ stored_file = file.to_file
23
+ @s3cn_bucket_obj.objects.create(key, stored_file)
24
+ stored_file.close if stored_file && !stored_file.closed?
25
+ end
26
+
27
+ def delete(key)
28
+ begin
29
+ @s3cn_bucket_obj.objects[key].delete
30
+ rescue Exception
31
+ nil
32
+ end
33
+ end
34
+
35
+ def info(key)
36
+ @s3cn_bucket_obj.objects[key].head
37
+ end
38
+
39
+ def download_url(path)
40
+ if @s3cn_bucket_private
41
+ temp_url = @s3cn_bucket_obj.objects[path].url_for(:get, :signature_version => :v4).to_s
42
+ else
43
+ temp_url = @s3cn_bucket_obj.objects[path].public_url.to_s
44
+ end
45
+ temp_url
46
+ end
47
+
48
+ private
49
+
50
+ def init
51
+ init_s3cn_connection
52
+ end
53
+
54
+ def init_s3cn_connection
55
+ return if !@s3cn.nil?
56
+ temp_config = {:access_key_id => @s3cn_access_key_id, :secret_access_key => @s3cn_secret_access_key, :region => @s3cn_region}
57
+ @s3cn = AWS::S3.new(temp_config)
58
+ @s3cn_bucket_obj = @s3cn.buckets[@s3cn_bucket]
59
+ end
60
+ end
61
+
62
+ class File
63
+ def initialize(uploader, path)
64
+ @uploader, @path = uploader, path
65
+ end
66
+
67
+ def path
68
+ @path
69
+ end
70
+
71
+ def url
72
+ s3cn_connection.download_url(@path)
73
+ end
74
+
75
+ def store(file)
76
+ s3cn_connection.store(file, @path)
77
+ end
78
+
79
+ def delete
80
+ s3cn_connection.delete(@path)
81
+ end
82
+
83
+ def content_type
84
+ file_info[:content_type] || 'application/octet-stream'
85
+ end
86
+
87
+ def size
88
+ file_info[:content_length] || 0
89
+ end
90
+
91
+ def etag
92
+ file_info[:etag]
93
+ end
94
+
95
+ def meta
96
+ file_info[:meta]
97
+ end
98
+
99
+ private
100
+
101
+ def s3cn_connection
102
+ if @s3cn_connection
103
+ @s3cn_connection
104
+ else
105
+ config = {
106
+ :s3cn_region => @uploader.s3cn_region,
107
+ :s3cn_endpoint => @uploader.s3cn_endpoint,
108
+ :s3cn_bucket => @uploader.s3cn_bucket,
109
+ :s3cn_access_key_id => @uploader.s3cn_access_key_id,
110
+ :s3cn_secret_access_key => @uploader.s3cn_secret_access_key,
111
+ :s3cn_bucket_private => @uploader.s3cn_bucket_private,
112
+ :s3cn_protocol => @uploader.s3cn_protocol
113
+ }
114
+ @s3cn_connection ||= Connection.new config
115
+ end
116
+ end
117
+
118
+ def file_info
119
+ @file_info ||= s3cn_connection.info(@path)
120
+ end
121
+ end
122
+
123
+ def store!(file)
124
+ f = ::CarrierWave::Storage::S3cn::File.new(uploader, uploader.store_path(uploader.filename))
125
+ f.store(file)
126
+ f
127
+ end
128
+
129
+ def retrieve!(identifier)
130
+ ::CarrierWave::Storage::S3cn::File.new(uploader, uploader.store_path(identifier))
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,5 @@
1
+ module CarrierWave
2
+ module S3cn
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ require "rubygems"
2
+ require "rspec"
3
+ require "rspec/autorun"
4
+ require "padrino"
5
+ require "active_record"
6
+ require "carrierwave"
7
+ require "carrierwave/orm/activerecord"
8
+
9
+ class ImageUploader < CarrierWave::Uploader::Base
10
+ storage :s3cn
11
+ self.s3cn_region = "cn-north-1"
12
+ # self.s3cn_endpoint = '' #optional
13
+ self.s3cn_bucket = "my-bucket"
14
+ self.s3cn_access_key_id = "AKIAOYZP5FWNNNW4RFZA"
15
+ self.s3cn_secret_access_key = "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"
16
+ self.s3cn_bucket_private = true #default true
17
+ self.s3cn_protocol = "https" #default https
18
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-s3cn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - xudeke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carrierwave
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-v1
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: S3 storage, in china, plugin for CarrierWave
70
+ email:
71
+ - xudeke@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - carrierwave-s3cn.gemspec
82
+ - lib/carrierwave/s3cn.rb
83
+ - lib/carrierwave/s3cn/configuration.rb
84
+ - lib/carrierwave/s3cn/storage.rb
85
+ - lib/carrierwave/s3cn/version.rb
86
+ - spec/upload.rb
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: S3 storage, in china, plugin for CarrierWave
111
+ test_files:
112
+ - spec/upload.rb