carrierwave-aliyun 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelogs.md +5 -0
- data/carrierwave-aliyun.gemspec +1 -0
- data/lib/carrierwave/aliyun/version.rb +1 -1
- data/lib/carrierwave/storage/aliyun.rb +46 -21
- data/spec/aliyun_spec.rb +4 -4
- data/spec/spec_helper.rb +3 -3
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26578c252fe9854ba9c649f43f34085a9e08d8a9
|
4
|
+
data.tar.gz: 4aa49c1451a5552aa9918bc5482bbfbc143e329f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0ed5151b787618e4496807b065ec493c3a60e141ac7286d9eab5acdbd8c1fa74197b328867243daa62979defd237642a518fd97b9fa2f06eb2aa9aab3dcdb4a
|
7
|
+
data.tar.gz: 6a4a619a5e131a94bc2a4b3e1c8e12eeb26143a074227b3d9ff59f8425a71f00a3e6e602cfa5e14a0159b1dbb7c53e223a06d1b1ca21d75e02faf7450403d97c
|
data/Changelogs.md
CHANGED
data/carrierwave-aliyun.gemspec
CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
17
|
s.require_paths = ["lib"]
|
18
|
+
s.license = 'MIT'
|
18
19
|
|
19
20
|
s.add_dependency "carrierwave", [">= 0.5.7"]
|
20
21
|
s.add_dependency "rest-client", [">= 1.6.7"]
|
@@ -8,7 +8,7 @@ require 'uri'
|
|
8
8
|
module CarrierWave
|
9
9
|
module Storage
|
10
10
|
class Aliyun < Abstract
|
11
|
-
|
11
|
+
|
12
12
|
class Connection
|
13
13
|
def initialize(options={})
|
14
14
|
@aliyun_access_id = options[:aliyun_access_id]
|
@@ -23,26 +23,47 @@ module CarrierWave
|
|
23
23
|
@aliyun_host = options[:aliyun_host] || "#{@aliyun_bucket}.oss.aliyuncs.com"
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
=begin rdoc
|
27
|
+
上传文件
|
28
|
+
|
29
|
+
== 参数:
|
30
|
+
- path - remote 存储路径
|
31
|
+
- file - 需要上传文件的 File 对象
|
32
|
+
- options:
|
33
|
+
- content_type - 上传文件的 MimeType,默认 `image/jpg`
|
34
|
+
|
35
|
+
== 返回值:
|
36
|
+
图片的下载地址
|
37
|
+
=end
|
38
|
+
def put(path, file, options={})
|
27
39
|
path = format_path(path)
|
28
40
|
bucket_path = get_bucket_path(path)
|
29
|
-
content_md5 = Digest::MD5.
|
41
|
+
content_md5 = Digest::MD5.file(file)
|
30
42
|
content_type = options[:content_type] || "image/jpg"
|
31
43
|
date = gmtdate
|
32
44
|
url = path_to_url(path)
|
33
45
|
auth_sign = sign("PUT", bucket_path, content_md5, content_type,date)
|
34
46
|
headers = {
|
35
|
-
"Authorization" => auth_sign,
|
47
|
+
"Authorization" => auth_sign,
|
36
48
|
"Content-Type" => content_type,
|
37
|
-
"Content-Length" =>
|
49
|
+
"Content-Length" => file.size,
|
38
50
|
"Date" => date,
|
39
51
|
"Host" => @aliyun_upload_host,
|
40
52
|
"Expect" => "100-Continue"
|
41
53
|
}
|
42
|
-
RestClient.put(URI.encode(url),
|
54
|
+
RestClient.put(URI.encode(url), file, headers)
|
43
55
|
return path_to_url(path, :get => true)
|
44
56
|
end
|
45
|
-
|
57
|
+
|
58
|
+
=begin rdoc
|
59
|
+
删除 Remote 的文件
|
60
|
+
|
61
|
+
== 参数:
|
62
|
+
- path - remote 存储路径
|
63
|
+
|
64
|
+
== 返回值:
|
65
|
+
图片的下载地址
|
66
|
+
=end
|
46
67
|
def delete(path)
|
47
68
|
path = format_path(path)
|
48
69
|
bucket_path = get_bucket_path(path)
|
@@ -56,22 +77,26 @@ module CarrierWave
|
|
56
77
|
RestClient.delete(URI.encode(url), headers)
|
57
78
|
return path_to_url(path, :get => true)
|
58
79
|
end
|
59
|
-
|
80
|
+
|
81
|
+
##
|
82
|
+
# 阿里云需要的 GMT 时间格式
|
60
83
|
def gmtdate
|
61
84
|
Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
62
85
|
end
|
63
|
-
|
86
|
+
|
64
87
|
def format_path(path)
|
65
88
|
return "" if path.blank?
|
66
89
|
path.gsub!(/^\//,"")
|
67
|
-
|
90
|
+
|
68
91
|
path
|
69
92
|
end
|
70
93
|
|
71
94
|
def get_bucket_path(path)
|
72
95
|
[@aliyun_bucket,path].join("/")
|
73
96
|
end
|
74
|
-
|
97
|
+
|
98
|
+
##
|
99
|
+
# 根据配置返回完整的上传文件的访问地址
|
75
100
|
def path_to_url(path, opts = {})
|
76
101
|
if opts[:get]
|
77
102
|
"http://#{@aliyun_host}/#{path}"
|
@@ -80,10 +105,10 @@ module CarrierWave
|
|
80
105
|
end
|
81
106
|
end
|
82
107
|
|
83
|
-
private
|
108
|
+
private
|
84
109
|
def sign(verb, path, content_md5 = '', content_type = '', date)
|
85
110
|
canonicalized_oss_headers = ''
|
86
|
-
canonicalized_resource = "/#{path}"
|
111
|
+
canonicalized_resource = "/#{path}"
|
87
112
|
string_to_sign = "#{verb}\n\n#{content_type}\n#{date}\n#{canonicalized_oss_headers}#{canonicalized_resource}"
|
88
113
|
digest = OpenSSL::Digest::Digest.new('sha1')
|
89
114
|
h = OpenSSL::HMAC.digest(digest, @aliyun_access_key, string_to_sign)
|
@@ -140,8 +165,8 @@ module CarrierWave
|
|
140
165
|
oss_connection.path_to_url(@path, :get => true)
|
141
166
|
end
|
142
167
|
|
143
|
-
def store(
|
144
|
-
oss_connection.put(@path,
|
168
|
+
def store(file, opts = {})
|
169
|
+
oss_connection.put(@path, file, opts)
|
145
170
|
end
|
146
171
|
|
147
172
|
private
|
@@ -156,22 +181,22 @@ module CarrierWave
|
|
156
181
|
|
157
182
|
def oss_connection
|
158
183
|
return @oss_connection if @oss_connection
|
159
|
-
|
184
|
+
|
160
185
|
config = {
|
161
|
-
:aliyun_access_id => @uploader.aliyun_access_id,
|
162
|
-
:aliyun_access_key => @uploader.aliyun_access_key,
|
186
|
+
:aliyun_access_id => @uploader.aliyun_access_id,
|
187
|
+
:aliyun_access_key => @uploader.aliyun_access_key,
|
163
188
|
:aliyun_bucket => @uploader.aliyun_bucket,
|
164
189
|
:aliyun_internal => @uploader.aliyun_internal,
|
165
190
|
:aliyun_host => @uploader.aliyun_host
|
166
191
|
}
|
167
192
|
@oss_connection ||= CarrierWave::Storage::Aliyun::Connection.new(config)
|
168
193
|
end
|
169
|
-
|
194
|
+
|
170
195
|
end
|
171
|
-
|
196
|
+
|
172
197
|
def store!(file)
|
173
198
|
f = CarrierWave::Storage::Aliyun::File.new(uploader, self, uploader.store_path)
|
174
|
-
f.store(file.
|
199
|
+
f.store(::File.open(file.file), :content_type => file.content_type)
|
175
200
|
f
|
176
201
|
end
|
177
202
|
|
data/spec/aliyun_spec.rb
CHANGED
@@ -13,12 +13,12 @@ describe "Aliyun" do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should put" do
|
16
|
-
url = @connection.put("a/a.jpg",load_file("foo.jpg")
|
16
|
+
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
17
17
|
Net::HTTP.get_response(URI.parse(url)).code.should == "200"
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should put with / prefix" do
|
21
|
-
url = @connection.put("/a/a.jpg",load_file("foo.jpg")
|
21
|
+
url = @connection.put("/a/a.jpg",load_file("foo.jpg"))
|
22
22
|
Net::HTTP.get_response(URI.parse(url)).code.should == "200"
|
23
23
|
end
|
24
24
|
|
@@ -28,14 +28,14 @@ describe "Aliyun" do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should use default domain" do
|
31
|
-
url = @connection.put("a/a.jpg",load_file("foo.jpg")
|
31
|
+
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
32
32
|
url.should == "http://#{ALIYUN_BUCKET}.oss.aliyuncs.com/a/a.jpg"
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should support custom domain" do
|
36
36
|
@opts[:aliyun_host] = "foo.bar.com"
|
37
37
|
@connection = CarrierWave::Storage::Aliyun::Connection.new(@opts)
|
38
|
-
url = @connection.put("a/a.jpg",load_file("foo.jpg")
|
38
|
+
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
39
39
|
url.should == "http://foo.bar.com/a/a.jpg"
|
40
40
|
end
|
41
41
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -24,9 +24,9 @@ end
|
|
24
24
|
ActiveRecord::Migration.verbose = false
|
25
25
|
|
26
26
|
# 测试的时候需要修改这个地方
|
27
|
-
ALIYUN_ACCESS_ID = "
|
28
|
-
ALIYUN_ACCESS_KEY = '
|
29
|
-
ALIYUN_BUCKET = "
|
27
|
+
ALIYUN_ACCESS_ID = "osOXJ5VeQVgDLMs3"
|
28
|
+
ALIYUN_ACCESS_KEY = 'm2LSEotGzDFrtHLeG1e6OATUTQ5ypR'
|
29
|
+
ALIYUN_BUCKET = "bison-dev"
|
30
30
|
|
31
31
|
CarrierWave.configure do |config|
|
32
32
|
config.storage = :aliyun
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave-aliyun
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -62,7 +62,8 @@ files:
|
|
62
62
|
- spec/spec_helper.rb
|
63
63
|
- spec/upload_spec.rb
|
64
64
|
homepage: https://github.com/huacnlee/carrierwave-aliyun
|
65
|
-
licenses:
|
65
|
+
licenses:
|
66
|
+
- MIT
|
66
67
|
metadata: {}
|
67
68
|
post_install_message:
|
68
69
|
rdoc_options: []
|
@@ -91,3 +92,4 @@ test_files:
|
|
91
92
|
- spec/foo.zip
|
92
93
|
- spec/spec_helper.rb
|
93
94
|
- spec/upload_spec.rb
|
95
|
+
has_rdoc:
|