upyun 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +7 -4
- data/lib/upyun/form.rb +1 -1
- data/lib/upyun/rest.rb +12 -6
- data/lib/upyun/version.rb +1 -1
- data/spec/spec_helper.rb +6 -0
- data/spec/upyun_spec.rb +26 -18
- data/upyun.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e8945188015a56c10d3c46361797fd39ae8679a
|
4
|
+
data.tar.gz: c694593f3027180c55e9bfb5915f8fbe4ff94fa5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 108b4b9642f54e17b928f7482fa73e9d875d1bc3db952908dc41dabda035d8158dc98eec202afdfb9a41f97ab8452acec0111bca0ee150fb363ac52e4b1e0119
|
7
|
+
data.tar.gz: 1412e721624c65c989ab5e909994663e97ffa613815bdebe4b969e79d56e9bb60f48b7e0453f9d833fdc5727cdbc1dd22eacd08ac6a357dab08fbe21592820ac
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -13,7 +13,7 @@
|
|
13
13
|
在 `Gemfile` 中加入以下代码
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
gem 'upyun', '~> 1.0.
|
16
|
+
gem 'upyun', '~> 1.0.4'
|
17
17
|
```
|
18
18
|
|
19
19
|
然后执行如下命令安装:
|
@@ -74,12 +74,13 @@ upyun.endpoint = Upyun::ED_CMCC
|
|
74
74
|
> 这种方式只指定了又拍云必选的 `Date`, `Content-Length` 两个 Header,其它 Header 信息均未指定
|
75
75
|
|
76
76
|
```ruby
|
77
|
-
upyun.put('/save/to/path', 'file
|
77
|
+
upyun.put('/save/to/path', File.new('file.txt', 'rb')) # 上传一个文件
|
78
|
+
upyun.put('/save/to/path', 'binary') # 直接上传内容
|
78
79
|
```
|
79
80
|
**参数**
|
80
81
|
|
81
82
|
* `/save/to/path`: 文件在 UPYUN 空间的保存路径
|
82
|
-
* `file or binary
|
83
|
+
* `file or binary`:已打开的文件描述符或文件内容,如果为文件描述符,在上传结束后该描述符会自动关闭
|
83
84
|
|
84
85
|
##### 自定义方式
|
85
86
|
您也可以选择使用 API 允许的额外可选 HTTP Header 参数,以使用 API 提供的预处理等功能:
|
@@ -260,8 +261,10 @@ upyun.endpoint = Upyun::ED_CMCC
|
|
260
261
|
> 使用简化版本,将不使用额外的策略参数:
|
261
262
|
|
262
263
|
```ruby
|
263
|
-
upyun.upload('
|
264
|
+
upyun.upload('filepath.png')
|
265
|
+
upyun.upload(File.new('filepath.png'))
|
264
266
|
```
|
267
|
+
参数可以是文件路径或者已经打开的文件文件描述符
|
265
268
|
|
266
269
|
**返回**
|
267
270
|
上传结果返回一个 `Hash` 结构:
|
data/lib/upyun/form.rb
CHANGED
@@ -52,7 +52,7 @@ module Upyun
|
|
52
52
|
payload = {
|
53
53
|
policy: policy(base_opts.merge(opts)),
|
54
54
|
signature: signature,
|
55
|
-
file: File.new(file, 'rb')
|
55
|
+
file: file.is_a?(File) ? file : File.new(file, 'rb')
|
56
56
|
}
|
57
57
|
|
58
58
|
rest_client.post(payload, {'User-Agent' => "Upyun-Ruby-SDK-#{VERSION}"}) do |res|
|
data/lib/upyun/rest.rb
CHANGED
@@ -17,13 +17,9 @@ module Upyun
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def put(path, file, headers={})
|
20
|
-
raise ArgumentError, "'file' is not an instance of String" unless file.is_a?(String)
|
21
20
|
headers = headers.merge({'mkdir' => true}) unless headers.key?('mkdir')
|
22
|
-
|
23
|
-
|
24
|
-
else
|
25
|
-
{body: file, length: file.length, headers: headers}
|
26
|
-
end
|
21
|
+
body = file.respond_to?(:read) ? IO.binread(file) : file
|
22
|
+
options = {body: body, length: size(file), headers: headers}
|
27
23
|
|
28
24
|
# If the type of current bucket is Picture,
|
29
25
|
# put an image maybe return a set of headers
|
@@ -39,6 +35,8 @@ module Upyun
|
|
39
35
|
end
|
40
36
|
|
41
37
|
res == {} ? true : res
|
38
|
+
ensure
|
39
|
+
file.close if file.respond_to?(:close)
|
42
40
|
end
|
43
41
|
|
44
42
|
def get(path, savepath=nil)
|
@@ -162,5 +160,13 @@ module Upyun
|
|
162
160
|
sign = "#{method.to_s.upcase}&#{path}&#{date}&#{length}&#{@password}"
|
163
161
|
"UpYun #{@operator}:#{md5(sign)}"
|
164
162
|
end
|
163
|
+
|
164
|
+
def size(param)
|
165
|
+
if param.respond_to?(:size)
|
166
|
+
param.size
|
167
|
+
elsif param.is_a?(IO)
|
168
|
+
param.stat.size
|
169
|
+
end
|
170
|
+
end
|
165
171
|
end
|
166
172
|
end
|
data/lib/upyun/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/upyun_spec.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
2
|
require File.dirname(__FILE__) + '/spec_helper'
|
4
3
|
require 'uri'
|
5
4
|
|
@@ -25,10 +24,10 @@ describe "Upyun Restful API Basic testing" do
|
|
25
24
|
end
|
26
25
|
|
27
26
|
describe ".put" do
|
28
|
-
before { @path =
|
27
|
+
before { @path = "/ruby-sdk/foo/#{String.random}/test.jpg" }
|
29
28
|
|
30
29
|
it "PUT a file" do
|
31
|
-
expect(@upyun.put(@path, @file)).to be true
|
30
|
+
expect(@upyun.put(@path, File.new(@file, 'rb'))).to be true
|
32
31
|
end
|
33
32
|
|
34
33
|
it "PUT a binary string" do
|
@@ -42,11 +41,11 @@ describe "Upyun Restful API Basic testing" do
|
|
42
41
|
'x-gmkerl-value' => 42,
|
43
42
|
'x-gmkerl-unsharp' => true
|
44
43
|
}
|
45
|
-
expect(@upyun.put(@path, @file, headers)).to be true
|
44
|
+
expect(@upyun.put(@path, File.new(@file, 'rb'), headers)).to be true
|
46
45
|
end
|
47
46
|
|
48
|
-
describe "PUT
|
49
|
-
before(:all) { @path_cn = '/ruby-sdk/这是中文路径/foo.txt' }
|
47
|
+
describe "PUT while the path is not encoded" do
|
48
|
+
before(:all) { @path_cn = '/ruby-sdk/foo/这是中文路径/foo.txt' }
|
50
49
|
|
51
50
|
it "should success" do
|
52
51
|
expect(@upyun.put(@path_cn, @str)).to be true
|
@@ -65,7 +64,7 @@ describe "Upyun Restful API Basic testing" do
|
|
65
64
|
|
66
65
|
it "put a file to Picture bucket should return the image's metadata" do
|
67
66
|
upyunp = Upyun::Rest.new('sdkimg', 'tester', 'grjxv2mxELR3', {}, Upyun::ED_TELECOM)
|
68
|
-
metas = upyunp.put(@path, @file, content_type: 'image/jpeg')
|
67
|
+
metas = upyunp.put(@path, File.new(@file), content_type: 'image/jpeg')
|
69
68
|
expect(metas).to include(:width, :height, :frames, :file_type)
|
70
69
|
expect(metas[:width].is_a?(Integer))
|
71
70
|
expect(metas[:height].is_a?(Integer))
|
@@ -78,7 +77,7 @@ describe "Upyun Restful API Basic testing" do
|
|
78
77
|
|
79
78
|
describe ".get" do
|
80
79
|
before :all do
|
81
|
-
@path =
|
80
|
+
@path = "/ruby-sdk/foo/#{String.random}/test.jpg"
|
82
81
|
@upyun.put(@path, @str, {'Content-Type' => 'text/plain'})
|
83
82
|
end
|
84
83
|
|
@@ -87,14 +86,14 @@ describe "Upyun Restful API Basic testing" do
|
|
87
86
|
end
|
88
87
|
|
89
88
|
it "GET a file and save" do
|
90
|
-
expect(@upyun.get(@path, './save.jpg')).
|
91
|
-
expect(File.exists?('./save.jpg')).to
|
89
|
+
expect(@upyun.get(@path, './save.jpg')).to eq(@str.length)
|
90
|
+
expect(File.exists?('./save.jpg')).to eq(true)
|
92
91
|
expect(File.read('./save.jpg')).to eq(@str)
|
93
92
|
File.delete('./save.jpg')
|
94
93
|
end
|
95
94
|
|
96
95
|
it "GET a not-exist file" do
|
97
|
-
res = @upyun.get(
|
96
|
+
res = @upyun.get("/ruby-sdk/foo/#{String.random}/test-not-exist.jpg")
|
98
97
|
expect(res.is_a?(Hash) && res[:error][:code] == 404)
|
99
98
|
end
|
100
99
|
|
@@ -103,7 +102,7 @@ describe "Upyun Restful API Basic testing" do
|
|
103
102
|
|
104
103
|
describe ".getinfo" do
|
105
104
|
before :all do
|
106
|
-
@dir =
|
105
|
+
@dir = "/ruby-sdk/foo/#{String.random}"
|
107
106
|
@path = "#{@dir}/test.jpg"
|
108
107
|
@upyun.put(@path, @str, {'Content-Type' => 'text/plain'})
|
109
108
|
end
|
@@ -123,8 +122,8 @@ describe "Upyun Restful API Basic testing" do
|
|
123
122
|
|
124
123
|
describe ".delete" do
|
125
124
|
before do
|
126
|
-
@path =
|
127
|
-
@upyun.put(@path, @file)
|
125
|
+
@path = "/ruby-sdk/foo/#{String.random}/test.jpg"
|
126
|
+
@upyun.put(@path, File.new(@file))
|
128
127
|
end
|
129
128
|
|
130
129
|
it "DELETE a file" do
|
@@ -133,7 +132,7 @@ describe "Upyun Restful API Basic testing" do
|
|
133
132
|
end
|
134
133
|
|
135
134
|
describe ".mkdir" do
|
136
|
-
before(:all) { @path =
|
135
|
+
before(:all) { @path = "/ruby-sdk/foo/dir/#{String.random}" }
|
137
136
|
|
138
137
|
it "should success" do
|
139
138
|
expect(@upyun.mkdir(@path)).to be true
|
@@ -144,8 +143,8 @@ describe "Upyun Restful API Basic testing" do
|
|
144
143
|
|
145
144
|
describe ".getlist" do
|
146
145
|
before :all do
|
147
|
-
@dir =
|
148
|
-
10.times { |i| @upyun.put("#@dir/#{i}", @file) }
|
146
|
+
@dir = "/ruby-sdk/foo/#{String.random}"
|
147
|
+
10.times { |i| @upyun.put("#@dir/#{i}", File.new(@file)) }
|
149
148
|
end
|
150
149
|
|
151
150
|
it "should get a list of file record" do
|
@@ -189,7 +188,7 @@ describe "Form Upload", current: true do
|
|
189
188
|
end
|
190
189
|
|
191
190
|
describe ".upload" do
|
192
|
-
it "with
|
191
|
+
it "with file path should success" do
|
193
192
|
res = @form.upload(@file)
|
194
193
|
expect(res.keys).to include(:code, :message, :url, :time)
|
195
194
|
expect(res[:code]).to eq(200)
|
@@ -198,6 +197,15 @@ describe "Form Upload", current: true do
|
|
198
197
|
expect(res[:url]).to eq("/#{now.year}/#{now.mon}/#{now.day}/upyun.jpg")
|
199
198
|
end
|
200
199
|
|
200
|
+
it "with file descriptor should success" do
|
201
|
+
fd = File.new(@file, 'rb')
|
202
|
+
res = @form.upload(fd)
|
203
|
+
expect(res.keys).to include(:code, :message, :url, :time)
|
204
|
+
expect(res[:code]).to eq(200)
|
205
|
+
expect(res[:message]).to match(/ok/)
|
206
|
+
expect(fd.closed?).to eq(true)
|
207
|
+
end
|
208
|
+
|
201
209
|
it "set 'save-key' should success" do
|
202
210
|
res = @form.upload(@file, {'save-key' => 'name-ed keypath'})
|
203
211
|
expect(res[:code]).to eq(200)
|
data/upyun.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency "rest-client", ">= 1.6.7"
|
22
|
-
spec.add_dependency "activesupport"
|
22
|
+
spec.add_dependency "activesupport", ">= 3.2.8"
|
23
23
|
|
24
24
|
spec.add_development_dependency "rspec", "~> 2.6"
|
25
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: upyun
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jsvisa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 3.2.8
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 3.2.8
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|