upyun 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +20 -11
- data/lib/upyun/rest.rb +44 -18
- data/lib/upyun/version.rb +1 -1
- data/spec/upyun_spec.rb +27 -2
- data/upyun.gemspec +2 -4
- metadata +9 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c53c29e162493c788f931b0eab39850a404fe70
|
4
|
+
data.tar.gz: 04a51ddba3f0eff2d3862ee4d3ede5d15f948a96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d26b50b05d5ed484d3b308676772fc84a9f6b2885baef919b421dd4d57dc8632c680510827aae5ef92f7cbabb221f45d328d02c5bd1cb7f91bf3129f83c848f9
|
7
|
+
data.tar.gz: 5f01e1b9c185217cca77b94ac433f2bd379632ee169caf17b74cf0f199044dcbc466fd1f272258b7481816d7c545a3619de251d0852723ff0085149aab5da069
|
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.3'
|
17
17
|
```
|
18
18
|
|
19
19
|
然后执行如下命令安装:
|
@@ -93,8 +93,12 @@ upyun.put('/save/to/path', 'file or binary', headers)
|
|
93
93
|
|
94
94
|
**返回**
|
95
95
|
|
96
|
-
上传成功返回 `true`,失败返回一个 `Hash` 结构: `{error: {code: code, message: message}}`,
|
97
|
-
|
96
|
+
上传成功返回 `true`,失败返回一个 `Hash` 结构: `{request_id: request_id, error: {code: code, message: message}}`,
|
97
|
+
其中:
|
98
|
+
|
99
|
+
* `request_id` 为本次请求的请求码,由 UPYUN 本台返回,可用该值查询 UPYUN 日志;
|
100
|
+
* `code` 为又拍云返回的错误码;
|
101
|
+
* `message` 为错误信息;
|
98
102
|
|
99
103
|
|
100
104
|
#### 下载文件
|
@@ -110,8 +114,13 @@ file = upyun.get('/path/to/file')
|
|
110
114
|
* `'/path/to/file'`: 文件在 UPYUN 空间中的路径
|
111
115
|
|
112
116
|
**返回**
|
113
|
-
下载成功返回文件信息,失败返回一个 `Hash`: `{error: {code: code, message: message}}`,
|
114
|
-
|
117
|
+
下载成功返回文件信息,失败返回一个 `Hash`: `{request_id: request_id, error: {code: code, message: message}}`,
|
118
|
+
其中:
|
119
|
+
|
120
|
+
* `request_id` 为本次请求的请求码,由 UPYUN 本台返回,可用该值查询 UPYUN 日志;
|
121
|
+
* `code` 为又拍云返回的错误码;
|
122
|
+
* `message` 为错误信息;
|
123
|
+
|
115
124
|
|
116
125
|
##### 保存文件至本地
|
117
126
|
|
@@ -125,7 +134,7 @@ upyun.get('/path/to/file', 'saved/foo.png')
|
|
125
134
|
* `saved/foo.png`: 文件本地保存路径
|
126
135
|
|
127
136
|
**返回**
|
128
|
-
|
137
|
+
下载成功返回获取的文件长度, 失败返回内容和上例一致。
|
129
138
|
|
130
139
|
|
131
140
|
#### 获取文件信息
|
@@ -151,7 +160,7 @@ upyun.getinfo('/path/to/file')
|
|
151
160
|
* `:file_size` 是文件的大小
|
152
161
|
* `:file_date` 是文件最后的更改时间。
|
153
162
|
|
154
|
-
失败返回一个 `Hash`: `{error: {code: code, message: message}}`。
|
163
|
+
失败返回一个 `Hash`: `{request_id: request_id, error: {code: code, message: message}}`。
|
155
164
|
|
156
165
|
|
157
166
|
#### 删除文件或者目录
|
@@ -167,7 +176,7 @@ upyun.delete('/path/to/file')
|
|
167
176
|
|
168
177
|
成功返回: `true`,
|
169
178
|
|
170
|
-
失败返回一个 `Hash`: `{error: {code: code, message: message}}`。
|
179
|
+
失败返回一个 `Hash`: `{request_id: request_id, error: {code: code, message: message}}`。
|
171
180
|
|
172
181
|
#### 创建目录
|
173
182
|
|
@@ -183,7 +192,7 @@ upyun.mkdir('/path/to/dir')
|
|
183
192
|
|
184
193
|
成功返回: `true`,
|
185
194
|
|
186
|
-
失败返回一个 `Hash`: `{error: {code: code, message: message}}`。
|
195
|
+
失败返回一个 `Hash`: `{request_id: request_id, error: {code: code, message: message}}`。
|
187
196
|
|
188
197
|
#### 获取目录文件列表
|
189
198
|
|
@@ -202,7 +211,7 @@ upyun.getlist('/path/to/dir')
|
|
202
211
|
{:name=>"bar.txt", :type=>:file, :length=>25, :last_modified=>1415261057}]
|
203
212
|
```
|
204
213
|
|
205
|
-
失败返回一个 `Hash`: `{error: {code: code, message: message}}`。
|
214
|
+
失败返回一个 `Hash`: `{request_id: request_id, error: {code: code, message: message}}`。
|
206
215
|
|
207
216
|
#### 获取空间使用情况
|
208
217
|
|
@@ -214,7 +223,7 @@ upyun.usage
|
|
214
223
|
|
215
224
|
成功返回空间使用量(单位为 `Byte`): `12400`,
|
216
225
|
|
217
|
-
失败返回一个 `Hash`: `{error: {code: code, message: message}}`。
|
226
|
+
失败返回一个 `Hash`: `{request_id: request_id, error: {code: code, message: message}}`。
|
218
227
|
|
219
228
|
### Form API 使用
|
220
229
|
|
data/lib/upyun/rest.rb
CHANGED
@@ -25,7 +25,20 @@ module Upyun
|
|
25
25
|
{body: file, length: file.length, headers: headers}
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
# If the type of current bucket is Picture,
|
29
|
+
# put an image maybe return a set of headers
|
30
|
+
# represent the image's metadata
|
31
|
+
# x-upyun-width
|
32
|
+
# x-upyun-height
|
33
|
+
# x-upyun-frames
|
34
|
+
# x-upyun-file-type
|
35
|
+
res = request(:put, path, options) do |hds|
|
36
|
+
hds.select { |k| k.to_s.match(/^x_upyun_/i) }.reduce({}) do |memo, (k, v)|
|
37
|
+
memo.merge!({k[8..-1].to_sym => /^\d+$/.match(v) ? v.to_i : v})
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
res == {} ? true : res
|
29
42
|
end
|
30
43
|
|
31
44
|
def get(path, savepath=nil)
|
@@ -38,10 +51,19 @@ module Upyun
|
|
38
51
|
end
|
39
52
|
|
40
53
|
def getinfo(path)
|
41
|
-
|
42
|
-
|
54
|
+
request(:head, path) do |hds|
|
55
|
+
# File info:
|
56
|
+
# x-upyun-file-type
|
57
|
+
# x-upyun-file-size
|
58
|
+
# x-upyun-file-date
|
59
|
+
hds.select { |k| k.to_s.match(/^x_upyun_file/i) }.reduce({}) do |memo, (k, v)|
|
60
|
+
memo.merge!({k[8..-1].to_sym => /^\d+$/.match(v) ? v.to_i : v})
|
61
|
+
end
|
62
|
+
end
|
43
63
|
end
|
44
64
|
|
65
|
+
alias :head :getinfo
|
66
|
+
|
45
67
|
def delete(path)
|
46
68
|
request(:delete, path)
|
47
69
|
end
|
@@ -54,8 +76,8 @@ module Upyun
|
|
54
76
|
res = request(:get, path)
|
55
77
|
return res if res.is_a?(Hash)
|
56
78
|
|
57
|
-
res.split(
|
58
|
-
attrs = f.split(
|
79
|
+
res.split("\n").map do |f|
|
80
|
+
attrs = f.split("\t")
|
59
81
|
{
|
60
82
|
name: attrs[0],
|
61
83
|
type: attrs[1] == 'N' ? :file : :folder,
|
@@ -76,19 +98,12 @@ module Upyun
|
|
76
98
|
|
77
99
|
private
|
78
100
|
|
79
|
-
def format_info(hds)
|
80
|
-
selected = hds.select { |k| k.to_s.match(/^x_upyun/i) }
|
81
|
-
selected.reduce({}) do |memo, (k, v)|
|
82
|
-
memo.merge!({k[8..-1].to_sym => /^\d+$/.match(v) ? v.to_i : v})
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
101
|
def fullpath(path)
|
87
|
-
decoded = URI::encode(URI::decode(path.force_encoding('utf-8')))
|
102
|
+
decoded = URI::encode(URI::decode(path.to_s.force_encoding('utf-8')))
|
88
103
|
"/#{@bucket}#{decoded.start_with?('/') ? decoded : '/' + decoded}"
|
89
104
|
end
|
90
105
|
|
91
|
-
def request(method, path, options={})
|
106
|
+
def request(method, path, options={}, &block)
|
92
107
|
fullpath = fullpath(path)
|
93
108
|
query = options[:query]
|
94
109
|
fullpath_query = "#{fullpath}#{query.nil? ? '' : '?' + query}"
|
@@ -104,21 +119,32 @@ module Upyun
|
|
104
119
|
if [:post, :patch, :put].include? method
|
105
120
|
body = options[:body].nil? ? '' : options[:body]
|
106
121
|
rest_client[fullpath_query].send(method, body, headers) do |res|
|
107
|
-
res.code
|
122
|
+
if res.code / 100 == 2
|
123
|
+
block_given? ? yield(res.headers) : true
|
124
|
+
else
|
125
|
+
{
|
126
|
+
request_id: res.headers[:x_request_id],
|
127
|
+
error: {code: res.code, message: res.body}
|
128
|
+
}
|
129
|
+
end
|
108
130
|
end
|
131
|
+
|
109
132
|
else
|
110
133
|
rest_client[fullpath_query].send(method, headers) do |res|
|
111
|
-
if res.code ==
|
134
|
+
if res.code / 100 == 2
|
112
135
|
case method
|
113
136
|
when :get
|
114
137
|
res.body
|
115
138
|
when :head
|
116
|
-
res.headers
|
139
|
+
yield(res.headers)
|
117
140
|
else
|
118
141
|
true
|
119
142
|
end
|
120
143
|
else
|
121
|
-
|
144
|
+
{
|
145
|
+
request_id: res.headers[:x_request_id],
|
146
|
+
error: {code: res.code, message: res.body}
|
147
|
+
}
|
122
148
|
end
|
123
149
|
end
|
124
150
|
end
|
data/lib/upyun/version.rb
CHANGED
data/spec/upyun_spec.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
4
|
require 'uri'
|
3
5
|
|
4
6
|
describe "Upyun Restful API Basic testing" do
|
5
7
|
before :all do
|
6
|
-
@upyun = Upyun::Rest.new('sdkfile', 'tester', 'grjxv2mxELR3')
|
8
|
+
@upyun = Upyun::Rest.new('sdkfile', 'tester', 'grjxv2mxELR3', {}, Upyun::ED_TELECOM)
|
7
9
|
@file = File.expand_path('../upyun.jpg', __FILE__)
|
8
10
|
@str = 'This is a binary string, not a file'
|
9
11
|
end
|
@@ -61,6 +63,16 @@ describe "Upyun Restful API Basic testing" do
|
|
61
63
|
after(:all) { @upyun.delete(@path_cn) }
|
62
64
|
end
|
63
65
|
|
66
|
+
it "put a file to Picture bucket should return the image's metadata" do
|
67
|
+
upyunp = Upyun::Rest.new('sdkimg', 'tester', 'grjxv2mxELR3', {}, Upyun::ED_TELECOM)
|
68
|
+
metas = upyunp.put(@path, @file, content_type: 'image/jpeg')
|
69
|
+
expect(metas).to include(:width, :height, :frames, :file_type)
|
70
|
+
expect(metas[:width].is_a?(Integer))
|
71
|
+
expect(metas[:height].is_a?(Integer))
|
72
|
+
expect(metas[:frames].is_a?(Integer))
|
73
|
+
expect(metas[:file_type].is_a?(String))
|
74
|
+
end
|
75
|
+
|
64
76
|
after { @upyun.delete(@path) }
|
65
77
|
end
|
66
78
|
|
@@ -121,7 +133,7 @@ describe "Upyun Restful API Basic testing" do
|
|
121
133
|
end
|
122
134
|
|
123
135
|
describe ".mkdir" do
|
124
|
-
before(:all) { @path = '/ruby-
|
136
|
+
before(:all) { @path = '/ruby-sdk/foo/dir' }
|
125
137
|
|
126
138
|
it "should success" do
|
127
139
|
expect(@upyun.mkdir(@path)).to be true
|
@@ -131,9 +143,22 @@ describe "Upyun Restful API Basic testing" do
|
|
131
143
|
end
|
132
144
|
|
133
145
|
describe ".getlist" do
|
146
|
+
before :all do
|
147
|
+
@dir = '/ruby-sdk/foo'
|
148
|
+
10.times { |i| @upyun.put("#@dir/#{i}", @file) }
|
149
|
+
end
|
150
|
+
|
134
151
|
it "should get a list of file record" do
|
135
152
|
expect(@upyun.getlist("/")).to be_instance_of(Array)
|
136
153
|
end
|
154
|
+
|
155
|
+
it "should return the correct number of records" do
|
156
|
+
expect(@upyun.getlist(@dir).length).to eq(10)
|
157
|
+
end
|
158
|
+
|
159
|
+
after :all do
|
160
|
+
10.times { |i| @upyun.delete("#@dir/#{i}") }
|
161
|
+
end
|
137
162
|
end
|
138
163
|
|
139
164
|
describe ".usage" do
|
data/upyun.gemspec
CHANGED
@@ -19,10 +19,8 @@ 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"
|
23
23
|
|
24
|
-
spec.add_development_dependency "
|
25
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
-
spec.add_development_dependency "rspec", "~> 3.1.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.6"
|
27
25
|
end
|
28
26
|
|
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.3
|
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-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -28,58 +28,30 @@ dependencies:
|
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 4.1.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
|
-
- - ~>
|
38
|
+
- - '>='
|
67
39
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
40
|
+
version: '0'
|
69
41
|
- !ruby/object:Gem::Dependency
|
70
42
|
name: rspec
|
71
43
|
requirement: !ruby/object:Gem::Requirement
|
72
44
|
requirements:
|
73
45
|
- - ~>
|
74
46
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
47
|
+
version: '2.6'
|
76
48
|
type: :development
|
77
49
|
prerelease: false
|
78
50
|
version_requirements: !ruby/object:Gem::Requirement
|
79
51
|
requirements:
|
80
52
|
- - ~>
|
81
53
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
54
|
+
version: '2.6'
|
83
55
|
description: UPYUN Rest API and Form API SDK
|
84
56
|
email:
|
85
57
|
- delweng@gmail.com
|
@@ -133,3 +105,4 @@ test_files:
|
|
133
105
|
- spec/spec_helper.rb
|
134
106
|
- spec/upyun.jpg
|
135
107
|
- spec/upyun_spec.rb
|
108
|
+
has_rdoc:
|