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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b7c6794e4b53be8b1781733433933bb0fcec534
4
- data.tar.gz: cbec6afebb360c4085155ce1f93d96657306c075
3
+ metadata.gz: 25e4c65f600597e1c2cc8ec0e9f4cf06044b0e60
4
+ data.tar.gz: 364f0df6e6f470ec901df437f0831a2f1c473040
5
5
  SHA512:
6
- metadata.gz: a51ac3e32565f55992a3ca2ff282c958c30da2be5739a27faf5aafe5e735cef28e54aa1384f9417dc40f8dfdfbf79e826d1a166d70a502c7838a224e9c0e49f3
7
- data.tar.gz: 189874b6216fb7345d8799cb33284f9104d8a64d5444bae4ebee61d57e52c792daf9bbaece22a0fe5b218e7f776cabf73641c62d6435ceef21417c17dbf49457
6
+ metadata.gz: 60925c4e88c63a2bc82caf7856c7703cf55b9cd634f855b931d2aee9a2150b915627c1e341cbb9ace24f7d9d303bae67871e118132f51976e81a26c0364ed0c2
7
+ data.tar.gz: 4e82ed9ba1c8c212bfc22f9299c778e1ee16b741a271a31408df58ed5230c23abe308a25a75bc34549e3dcceb9c945625acf79c47b8e6a2124b28e384644506b
data/Changelogs.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.2
2
+
3
+ * 请注意 `config.aliyun_host` 要求修改带 HTTP 协议,以便支持设置 http:// 或 https://.
4
+
1
5
  ## 0.3.1
2
6
 
3
7
  * 修复当文件名中包含了 "+",在 OSS 中上传会遇到签名不对应的问题;
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 = :aliyun
27
- config.aliyun_access_id = "xxxxxx"
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 = "simple"
30
+ config.aliyun_bucket = "simple"
31
31
  # 是否使用内部连接,true - 使用 Aliyun 局域网的方式访问 false - 外部网络访问
32
- config.aliyun_internal = true
32
+ config.aliyun_internal = true
33
33
  # 配置存储的地区数据中心,默认: cn-hangzhou
34
- # config.aliyun_area = "cn-hangzhou"
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 = "foo.bar.com"
37
+ config.aliyun_host = "http://foo.bar.com"
38
38
  end
39
39
  ```
40
40
 
@@ -1,7 +1,7 @@
1
1
  module CarrierWave
2
2
  module Aliyun
3
3
  class Version
4
- MAJOR, MINOR, PATCH = 0, 3, 1
4
+ MAJOR, MINOR, PATCH = 0, 3, 2
5
5
 
6
6
  ##
7
7
  # Returns the major version ( big release based off of multiple minor releases )
@@ -11,91 +11,93 @@ module CarrierWave
11
11
 
12
12
  class Connection
13
13
  def initialize(options={})
14
- @aliyun_access_id = options[:aliyun_access_id]
14
+ @aliyun_access_id = options[:aliyun_access_id]
15
15
  @aliyun_access_key = options[:aliyun_access_key]
16
- @aliyun_bucket = options[:aliyun_bucket]
17
- @aliyun_area = options[:aliyun_area] || 'cn-hangzhou'
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 = "#{@aliyun_bucket}.oss-#{@aliyun_area}-internal.aliyuncs.com"
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] || "#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
25
- end
27
+ @aliyun_host = options[:aliyun_host] || "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com"
26
28
 
27
- =begin rdoc
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
- =end
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 = format_path(path)
41
- bucket_path = get_bucket_path(path)
42
- content_md5 = Digest::MD5.file(file)
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 = gmtdate
45
- url = path_to_url(path)
46
- auth_sign = sign("PUT", bucket_path, content_md5, content_type,date)
47
- headers = {
48
- "Authorization" => auth_sign,
49
- "Content-Type" => content_type,
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" => date,
52
- "Host" => @aliyun_upload_host,
53
- "Expect" => "100-Continue"
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
- =begin rdoc
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 = path_to_url(path)
73
+ url = path_to_url(path)
72
74
  RestClient.get(URI.encode(url))
73
75
  end
74
76
 
75
- =begin rdoc
76
- 删除 Remote 的文件
77
-
78
- == 参数:
79
- - path - remote 存储路径
80
-
81
- == 返回值:
82
- 图片的下载地址
83
- =end
77
+ # 删除 Remote 的文件
78
+ #
79
+ # params:
80
+ # - path - remote 存储路径
81
+ #
82
+ # returns:
83
+ # 图片的下载地址
84
84
  def delete(path)
85
- path = format_path(path)
85
+ path = format_path(path)
86
86
  bucket_path = get_bucket_path(path)
87
- date = gmtdate
88
- headers = {
89
- "Host" => @aliyun_upload_host,
90
- "Date" => date,
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
- url = path_to_url(path)
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
- "http://#{@aliyun_host}/#{path}"
121
+ "#{@aliyun_host}/#{path}"
120
122
  else
121
- "http://#{@aliyun_upload_host}/#{path}"
123
+ "#{@aliyun_upload_host}/#{path}"
122
124
  end
123
125
  end
124
126
 
125
- private
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::Digest.new('sha1')
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 = path
141
- @base = 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 => @uploader.aliyun_access_id,
214
+ :aliyun_access_id => @uploader.aliyun_access_id,
213
215
  :aliyun_access_key => @uploader.aliyun_access_key,
214
- :aliyun_area => @uploader.aliyun_area,
215
- :aliyun_bucket => @uploader.aliyun_bucket,
216
- :aliyun_internal => @uploader.aliyun_internal,
217
- :aliyun_host => @uploader.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
- # config.aliyun_internal = false
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.1
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-08-22 00:00:00.000000000 Z
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.2.2
85
+ rubygems_version: 2.4.2
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: Aliyun OSS support for Carrierwave