carrierwave-aliyun 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelogs.md +4 -0
- data/README.md +6 -6
- data/lib/carrierwave/aliyun/version.rb +1 -1
- data/lib/carrierwave/storage/aliyun.rb +70 -68
- data/spec/aliyun_spec.rb +9 -7
- data/spec/spec_helper.rb +2 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25e4c65f600597e1c2cc8ec0e9f4cf06044b0e60
|
4
|
+
data.tar.gz: 364f0df6e6f470ec901df437f0831a2f1c473040
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60925c4e88c63a2bc82caf7856c7703cf55b9cd634f855b931d2aee9a2150b915627c1e341cbb9ace24f7d9d303bae67871e118132f51976e81a26c0364ed0c2
|
7
|
+
data.tar.gz: 4e82ed9ba1c8c212bfc22f9299c778e1ee16b741a271a31408df58ed5230c23abe308a25a75bc34549e3dcceb9c945625acf79c47b8e6a2124b28e384644506b
|
data/Changelogs.md
CHANGED
data/README.md
CHANGED
@@ -23,18 +23,18 @@ gem 'carrierwave-aliyun'
|
|
23
23
|
|
24
24
|
```ruby
|
25
25
|
CarrierWave.configure do |config|
|
26
|
-
config.storage
|
27
|
-
config.aliyun_access_id
|
26
|
+
config.storage = :aliyun
|
27
|
+
config.aliyun_access_id = "xxxxxx"
|
28
28
|
config.aliyun_access_key = 'xxxxxx'
|
29
29
|
# 你需要在 Aliyum OSS 上面提前创建一个 Bucket
|
30
|
-
config.aliyun_bucket
|
30
|
+
config.aliyun_bucket = "simple"
|
31
31
|
# 是否使用内部连接,true - 使用 Aliyun 局域网的方式访问 false - 外部网络访问
|
32
|
-
config.aliyun_internal
|
32
|
+
config.aliyun_internal = true
|
33
33
|
# 配置存储的地区数据中心,默认: cn-hangzhou
|
34
|
-
# config.aliyun_area
|
34
|
+
# config.aliyun_area = "cn-hangzhou"
|
35
35
|
# 使用自定义域名,设定此项,carrierwave 返回的 URL 将会用自定义域名
|
36
36
|
# 自定于域名请 CNAME 到 you_bucket_name.oss.aliyuncs.com (you_bucket_name 是你的 bucket 的名称)
|
37
|
-
config.aliyun_host
|
37
|
+
config.aliyun_host = "http://foo.bar.com"
|
38
38
|
end
|
39
39
|
```
|
40
40
|
|
@@ -11,91 +11,93 @@ module CarrierWave
|
|
11
11
|
|
12
12
|
class Connection
|
13
13
|
def initialize(options={})
|
14
|
-
@aliyun_access_id
|
14
|
+
@aliyun_access_id = options[:aliyun_access_id]
|
15
15
|
@aliyun_access_key = options[:aliyun_access_key]
|
16
|
-
@aliyun_bucket
|
17
|
-
@aliyun_area
|
16
|
+
@aliyun_bucket = options[:aliyun_bucket]
|
17
|
+
@aliyun_area = options[:aliyun_area] || 'cn-hangzhou'
|
18
|
+
|
18
19
|
# Host for upload
|
19
|
-
@aliyun_upload_host = "#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
|
20
20
|
if options[:aliyun_internal] == true
|
21
|
-
@aliyun_upload_host = "
|
21
|
+
@aliyun_upload_host = "http://#{@aliyun_bucket}.oss-#{@aliyun_area}-internal.aliyuncs.com"
|
22
|
+
else
|
23
|
+
@aliyun_upload_host = "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
|
22
24
|
end
|
25
|
+
|
23
26
|
# Host for get request
|
24
|
-
@aliyun_host = options[:aliyun_host] || "
|
25
|
-
end
|
27
|
+
@aliyun_host = options[:aliyun_host] || "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
- path - remote 存储路径
|
32
|
-
- file - 需要上传文件的 File 对象
|
33
|
-
- options:
|
34
|
-
- content_type - 上传文件的 MimeType,默认 `image/jpg`
|
29
|
+
if not @aliyun_host.include?("http")
|
30
|
+
raise "config.aliyun_host requirement include http:// or https://, but you give: #{@aliyun_host}"
|
31
|
+
end
|
32
|
+
end
|
35
33
|
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
# 上传文件
|
35
|
+
# params:
|
36
|
+
# - path - remote 存储路径
|
37
|
+
# - file - 需要上传文件的 File 对象
|
38
|
+
# - options:
|
39
|
+
# - content_type - 上传文件的 MimeType,默认 `image/jpg`
|
40
|
+
# returns:
|
41
|
+
# 图片的下载地址
|
39
42
|
def put(path, file, options={})
|
40
|
-
path
|
41
|
-
bucket_path
|
42
|
-
content_md5
|
43
|
+
path = format_path(path)
|
44
|
+
bucket_path = get_bucket_path(path)
|
45
|
+
content_md5 = Digest::MD5.file(file)
|
43
46
|
content_type = options[:content_type] || "image/jpg"
|
44
|
-
date
|
45
|
-
url
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
date = gmtdate
|
48
|
+
url = path_to_url(path)
|
49
|
+
|
50
|
+
host = URI.parse(url).host
|
51
|
+
|
52
|
+
auth_sign = sign("PUT", bucket_path, content_md5, content_type,date)
|
53
|
+
headers = {
|
54
|
+
"Authorization" => auth_sign,
|
55
|
+
"Content-Type" => content_type,
|
50
56
|
"Content-Length" => file.size,
|
51
|
-
"Date"
|
52
|
-
"Host"
|
53
|
-
"Expect"
|
57
|
+
"Date" => date,
|
58
|
+
"Host" => host,
|
59
|
+
"Expect" => "100-Continue"
|
54
60
|
}
|
61
|
+
|
55
62
|
RestClient.put(URI.encode(url).gsub("+", "%2B"), file, headers)
|
56
63
|
return path_to_url(path, :get => true)
|
57
64
|
end
|
58
65
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
- path - remote 存储路径
|
65
|
-
|
66
|
-
== 返回值:
|
67
|
-
图片文件
|
68
|
-
=end
|
66
|
+
# 读取文件
|
67
|
+
# params:
|
68
|
+
# - path - remote 存储路径
|
69
|
+
# returns:
|
70
|
+
# file data
|
69
71
|
def get(path)
|
70
72
|
path = format_path(path)
|
71
|
-
url
|
73
|
+
url = path_to_url(path)
|
72
74
|
RestClient.get(URI.encode(url))
|
73
75
|
end
|
74
76
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
图片的下载地址
|
83
|
-
=end
|
77
|
+
# 删除 Remote 的文件
|
78
|
+
#
|
79
|
+
# params:
|
80
|
+
# - path - remote 存储路径
|
81
|
+
#
|
82
|
+
# returns:
|
83
|
+
# 图片的下载地址
|
84
84
|
def delete(path)
|
85
|
-
path
|
85
|
+
path = format_path(path)
|
86
86
|
bucket_path = get_bucket_path(path)
|
87
|
-
date
|
88
|
-
|
89
|
-
|
90
|
-
|
87
|
+
date = gmtdate
|
88
|
+
url = path_to_url(path)
|
89
|
+
host = URI.parse(url).host
|
90
|
+
headers = {
|
91
|
+
"Host" => host,
|
92
|
+
"Date" => date,
|
91
93
|
"Authorization" => sign("DELETE", bucket_path, "", "" ,date)
|
92
94
|
}
|
93
|
-
|
95
|
+
|
94
96
|
RestClient.delete(URI.encode(url).gsub("+", "%2B"), headers)
|
95
97
|
return path_to_url(path, :get => true)
|
96
98
|
end
|
97
99
|
|
98
|
-
|
100
|
+
#
|
99
101
|
# 阿里云需要的 GMT 时间格式
|
100
102
|
def gmtdate
|
101
103
|
Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
@@ -116,18 +118,18 @@ module CarrierWave
|
|
116
118
|
# 根据配置返回完整的上传文件的访问地址
|
117
119
|
def path_to_url(path, opts = {})
|
118
120
|
if opts[:get]
|
119
|
-
"
|
121
|
+
"#{@aliyun_host}/#{path}"
|
120
122
|
else
|
121
|
-
"
|
123
|
+
"#{@aliyun_upload_host}/#{path}"
|
122
124
|
end
|
123
125
|
end
|
124
126
|
|
125
|
-
|
127
|
+
private
|
126
128
|
def sign(verb, path, content_md5 = '', content_type = '', date)
|
127
129
|
canonicalized_oss_headers = ''
|
128
130
|
canonicalized_resource = "/#{path}"
|
129
131
|
string_to_sign = "#{verb}\n\n#{content_type}\n#{date}\n#{canonicalized_oss_headers}#{canonicalized_resource}"
|
130
|
-
digest = OpenSSL::Digest
|
132
|
+
digest = OpenSSL::Digest.new('sha1')
|
131
133
|
h = OpenSSL::HMAC.digest(digest, @aliyun_access_key, string_to_sign)
|
132
134
|
h = Base64.encode64(h)
|
133
135
|
"OSS #{@aliyun_access_id}:#{h}"
|
@@ -137,8 +139,8 @@ module CarrierWave
|
|
137
139
|
class File
|
138
140
|
def initialize(uploader, base, path)
|
139
141
|
@uploader = uploader
|
140
|
-
@path
|
141
|
-
@base
|
142
|
+
@path = path
|
143
|
+
@base = base
|
142
144
|
end
|
143
145
|
|
144
146
|
##
|
@@ -209,12 +211,12 @@ module CarrierWave
|
|
209
211
|
return @oss_connection if @oss_connection
|
210
212
|
|
211
213
|
config = {
|
212
|
-
:aliyun_access_id
|
214
|
+
:aliyun_access_id => @uploader.aliyun_access_id,
|
213
215
|
:aliyun_access_key => @uploader.aliyun_access_key,
|
214
|
-
:aliyun_area
|
215
|
-
:aliyun_bucket
|
216
|
-
:aliyun_internal
|
217
|
-
:aliyun_host
|
216
|
+
:aliyun_area => @uploader.aliyun_area,
|
217
|
+
:aliyun_bucket => @uploader.aliyun_bucket,
|
218
|
+
:aliyun_internal => @uploader.aliyun_internal,
|
219
|
+
:aliyun_host => @uploader.aliyun_host
|
218
220
|
}
|
219
221
|
@oss_connection ||= CarrierWave::Storage::Aliyun::Connection.new(config)
|
220
222
|
end
|
data/spec/aliyun_spec.rb
CHANGED
@@ -7,7 +7,10 @@ describe "Aliyun" do
|
|
7
7
|
@opts = {
|
8
8
|
:aliyun_access_id => ALIYUN_ACCESS_ID,
|
9
9
|
:aliyun_access_key => ALIYUN_ACCESS_KEY,
|
10
|
-
:aliyun_bucket => ALIYUN_BUCKET
|
10
|
+
:aliyun_bucket => ALIYUN_BUCKET,
|
11
|
+
:aliyun_area => "cn-hangzhou",
|
12
|
+
:aliyun_internal => true,
|
13
|
+
:aliyun_host => "http://bison-dev.cn-hangzhou.oss.aliyun-inc.com"
|
11
14
|
}
|
12
15
|
@connection = CarrierWave::Storage::Aliyun::Connection.new(@opts)
|
13
16
|
end
|
@@ -27,13 +30,12 @@ describe "Aliyun" do
|
|
27
30
|
Net::HTTP.get_response(URI.parse(url)).code.should == "404"
|
28
31
|
end
|
29
32
|
|
30
|
-
it "should use default domain" do
|
31
|
-
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
32
|
-
url.should == "http://#{ALIYUN_BUCKET}.oss-cn-hangzhou.aliyuncs.com/a/a.jpg"
|
33
|
-
end
|
34
|
-
|
35
33
|
it "should support custom domain" do
|
36
|
-
@opts[:aliyun_host] = "foo.bar.com"
|
34
|
+
@opts[:aliyun_host] = "https://foo.bar.com"
|
35
|
+
@connection = CarrierWave::Storage::Aliyun::Connection.new(@opts)
|
36
|
+
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
37
|
+
url.should == "https://foo.bar.com/a/a.jpg"
|
38
|
+
@opts[:aliyun_host] = "http://foo.bar.com"
|
37
39
|
@connection = CarrierWave::Storage::Aliyun::Connection.new(@opts)
|
38
40
|
url = @connection.put("a/a.jpg",load_file("foo.jpg"))
|
39
41
|
url.should == "http://foo.bar.com/a/a.jpg"
|
data/spec/spec_helper.rb
CHANGED
@@ -34,7 +34,8 @@ CarrierWave.configure do |config|
|
|
34
34
|
config.aliyun_access_key = ALIYUN_ACCESS_KEY
|
35
35
|
config.aliyun_bucket = ALIYUN_BUCKET
|
36
36
|
config.aliyun_area = "cn-hangzhou"
|
37
|
-
|
37
|
+
config.aliyun_internal = true
|
38
|
+
config.aliyun_host = "http://bison-dev.cn-hangzhou.oss.aliyun-inc.com"
|
38
39
|
end
|
39
40
|
|
40
41
|
def load_file(fname)
|
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.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
version: '0'
|
83
83
|
requirements: []
|
84
84
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.
|
85
|
+
rubygems_version: 2.4.2
|
86
86
|
signing_key:
|
87
87
|
specification_version: 4
|
88
88
|
summary: Aliyun OSS support for Carrierwave
|