qiniu_jxb 6.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.gitignore +18 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG.md +118 -0
  5. data/Gemfile +8 -0
  6. data/Gemfile.lock +37 -0
  7. data/LICENSE +22 -0
  8. data/README.md +47 -0
  9. data/Rakefile +21 -0
  10. data/docs/README.md +790 -0
  11. data/lib/qiniu-rs.rb +2 -0
  12. data/lib/qiniu.rb +209 -0
  13. data/lib/qiniu/abstract.rb +22 -0
  14. data/lib/qiniu/adt.rb +46 -0
  15. data/lib/qiniu/auth.rb +234 -0
  16. data/lib/qiniu/config.rb +58 -0
  17. data/lib/qiniu/exceptions.rb +120 -0
  18. data/lib/qiniu/fop.rb +4 -0
  19. data/lib/qiniu/http.rb +137 -0
  20. data/lib/qiniu/image.rb +38 -0
  21. data/lib/qiniu/log.rb +15 -0
  22. data/lib/qiniu/management.rb +128 -0
  23. data/lib/qiniu/misc.rb +33 -0
  24. data/lib/qiniu/pfop.rb +124 -0
  25. data/lib/qiniu/resumable_upload.rb +319 -0
  26. data/lib/qiniu/storage.rb +5 -0
  27. data/lib/qiniu/tokens/access_token.rb +21 -0
  28. data/lib/qiniu/tokens/download_token.rb +31 -0
  29. data/lib/qiniu/tokens/qbox_token.rb +38 -0
  30. data/lib/qiniu/tokens/upload_token.rb +47 -0
  31. data/lib/qiniu/upload.rb +138 -0
  32. data/lib/qiniu/utils.rb +109 -0
  33. data/lib/qiniu/version.rb +17 -0
  34. data/qiniu.gemspec +29 -0
  35. data/spec/qiniu/abstract_spec.rb +30 -0
  36. data/spec/qiniu/auth_spec.rb +81 -0
  37. data/spec/qiniu/image_logo_for_test.png +0 -0
  38. data/spec/qiniu/image_spec.rb +89 -0
  39. data/spec/qiniu/management_spec.rb +156 -0
  40. data/spec/qiniu/misc_spec.rb +59 -0
  41. data/spec/qiniu/pfop_spec.rb +89 -0
  42. data/spec/qiniu/qiniu_spec.rb +329 -0
  43. data/spec/qiniu/tokens/qbox_token_spec.rb +29 -0
  44. data/spec/qiniu/upload_spec.rb +308 -0
  45. data/spec/qiniu/utils_spec.rb +49 -0
  46. data/spec/qiniu/version_spec.rb +10 -0
  47. data/spec/spec_helper.rb +19 -0
  48. metadata +220 -0
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/abstract'
5
+
6
+ describe Qiniu::Abstract do
7
+ before(:each) do
8
+ @klass = Class.new do
9
+ include Qiniu::Abstract
10
+
11
+ abstract_methods :foo, :bar
12
+ end
13
+ end
14
+
15
+ it "raises NotImplementedError" do
16
+ proc {
17
+ @klass.new.foo
18
+ }.should raise_error(NotImplementedError)
19
+ end
20
+
21
+ it "can be overridden" do
22
+ subclass = Class.new(@klass) do
23
+ def foo
24
+ :overridden
25
+ end
26
+ end
27
+
28
+ subclass.new.foo.should == :overridden
29
+ end
30
+ end
@@ -0,0 +1,81 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # vim: sw=2 ts=2
3
+
4
+ require 'spec_helper'
5
+ require 'qiniu/auth'
6
+ require 'qiniu/storage'
7
+ require 'digest/sha1'
8
+
9
+ module Qiniu
10
+ module Auth
11
+ describe Auth do
12
+
13
+ before :all do
14
+ @bucket = 'RubySDK-Test-Private'
15
+ @bucket = make_unique_bucket(@bucket)
16
+
17
+ ### 尝试创建Bucket
18
+ result = Qiniu::Storage.make_a_private_bucket(@bucket)
19
+ puts result.inspect
20
+ end
21
+
22
+ after :all do
23
+ ### 不删除Bucket以备下次使用
24
+ end
25
+
26
+ ### 测试私有资源下载
27
+ context ".download_private_file" do
28
+ it "should works" do
29
+ ### 生成Key
30
+ key = 'a_private_file'
31
+ key = make_unique_key_in_bucket(key)
32
+ puts "key=#{key}"
33
+
34
+ ### 上传测试文件
35
+ pp = Auth::PutPolicy.new(@bucket, key)
36
+ code, data, raw_headers = Qiniu::Storage.upload_with_put_policy(
37
+ pp,
38
+ __FILE__
39
+ )
40
+ code.should == 200
41
+ puts data.inspect
42
+ puts raw_headers.inspect
43
+
44
+ ### 获取下载地址
45
+ code, data = Qiniu::Storage.get(@bucket, key)
46
+ code.should == 200
47
+ puts data.inspect
48
+
49
+ url = data['url']
50
+
51
+ ### 授权下载地址(不带参数)
52
+ download_url = Qiniu::Auth.authorize_download_url(url)
53
+ puts "download_url=#{download_url}"
54
+
55
+ result = RestClient.get(download_url)
56
+ result.code.should == 200
57
+ result.body.should_not be_empty
58
+
59
+ ### 授权下载地址(带参数)
60
+ download_url = Qiniu::Auth.authorize_download_url(
61
+ url,
62
+ {
63
+ :fop => 'download/a.m3u8'
64
+ }
65
+ )
66
+ puts "download_url=#{download_url}"
67
+
68
+ result = RestClient.get(download_url)
69
+ result.code.should == 200
70
+ result.body.should_not be_empty
71
+
72
+ ### 删除文件
73
+ code, data = Qiniu::Storage.delete(@bucket, key)
74
+ code.should == 200
75
+ puts data.inspect
76
+ end
77
+ end
78
+
79
+ end
80
+ end # module Storage
81
+ end # module Qiniu
Binary file
@@ -0,0 +1,89 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/auth'
5
+ require 'qiniu'
6
+ require 'qiniu/fop'
7
+
8
+ module Qiniu
9
+ module Fop
10
+ describe Fop do
11
+
12
+ before :all do
13
+
14
+ ### 复用RubySDK-Test-Storage空间
15
+ @bucket = 'RubySDK-Test-Storage'
16
+ @bucket = make_unique_bucket(@bucket)
17
+
18
+ ### 尝试创建空间
19
+ result = Qiniu.mkbucket(@bucket)
20
+ puts result.inspect
21
+
22
+ pic_fname = "image_logo_for_test.png"
23
+ @key = make_unique_key_in_bucket(pic_fname)
24
+
25
+ local_file = File.expand_path('../' + pic_fname, __FILE__)
26
+
27
+ upopts = {
28
+ :scope => @bucket,
29
+ :expires_in => 3600,
30
+ :customer => "why404@gmail.com",
31
+ :async_options => "imageView/1/w/120/h/120",
32
+ :return_body => '{"size":$(fsize), "hash":$(etag), "width":$(imageInfo.width), "height":$(imageInfo.height)}'
33
+ }
34
+ uptoken = Qiniu.generate_upload_token(upopts)
35
+ data = Qiniu.upload_file :uptoken => uptoken, :file => local_file, :bucket => @bucket, :key => @key
36
+ puts data.inspect
37
+
38
+ data["size"].should_not be_zero
39
+ data["hash"].should_not be_empty
40
+ data["width"].should_not be_zero
41
+ data["height"].should_not be_zero
42
+
43
+ result = Qiniu.get(@bucket, @key)
44
+ result["url"].should_not be_empty
45
+ puts result.inspect
46
+ @source_image_url = result["url"]
47
+
48
+ @mogrify_options = {
49
+ :thumbnail => "!120x120>",
50
+ :gravity => "center",
51
+ :crop => "!120x120a0a0",
52
+ :quality => 85,
53
+ :rotate => 45,
54
+ :format => "jpg",
55
+ :auto_orient => true
56
+ }
57
+ end
58
+
59
+ after :all do
60
+ ### 不删除Bucket以备下次使用
61
+ end
62
+
63
+ context ".info" do
64
+ it "should works" do
65
+ code, data = Qiniu::Fop::Image.info(@source_image_url)
66
+ code.should == 200
67
+ puts data.inspect
68
+ end
69
+ end
70
+
71
+ =begin
72
+ context ".exif" do
73
+ it "should works" do
74
+ code, data = Qiniu::Fop::Image.exif(@source_image_url)
75
+ puts data.inspect
76
+ end
77
+ end
78
+ =end
79
+
80
+ context ".mogrify_preview_url" do
81
+ it "should works" do
82
+ mogrify_preview_url = Qiniu::Fop::Image.mogrify_preview_url(@source_image_url, @mogrify_options)
83
+ puts mogrify_preview_url.inspect
84
+ end
85
+ end
86
+
87
+ end
88
+ end # module Fop
89
+ end # module Qiniu
@@ -0,0 +1,156 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # vim: sw=2 ts=2
3
+
4
+ require 'digest/sha1'
5
+ require 'spec_helper'
6
+ require 'qiniu/auth'
7
+ require 'qiniu/management'
8
+ require 'qiniu'
9
+
10
+ module Qiniu
11
+ module Storage
12
+ describe Storage do
13
+
14
+ before :all do
15
+ @bucket = 'RubySDK-Test-Management'
16
+ @bucket = make_unique_bucket(@bucket)
17
+
18
+ ### 尝试创建Bucket
19
+ result = Qiniu.mkbucket(@bucket)
20
+ puts result.inspect
21
+
22
+ @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s)
23
+ @key = make_unique_key_in_bucket(@key)
24
+
25
+ @key2 = @key + rand(100).to_s
26
+ end
27
+
28
+ after :all do
29
+ ### 不删除Bucket以备下次使用
30
+ end
31
+
32
+ ### 准备数据
33
+ context ".prepare_file" do
34
+ it "should works" do
35
+ pp = Auth::PutPolicy.new(@bucket, @key)
36
+ code, data, raw_headers = Qiniu::Storage.upload_with_put_policy(
37
+ pp,
38
+ __FILE__
39
+ )
40
+ code.should == 200
41
+ puts data.inspect
42
+ end
43
+ end
44
+
45
+ ### 列举Bucket
46
+ context ".buckets" do
47
+ it "should works" do
48
+ code, data = Storage.buckets
49
+ code.should == 200
50
+ puts data.inspect
51
+ end
52
+ end
53
+
54
+ context ".stat" do
55
+ it "should works" do
56
+ code, data = Storage.stat(@bucket, @key)
57
+ code.should == 200
58
+ puts data.inspect
59
+ end
60
+ end
61
+
62
+ context ".get" do
63
+ it "should works" do
64
+ code, data = Storage.get(@bucket, @key)
65
+ code.should == 200
66
+ puts data.inspect
67
+ end
68
+ end
69
+
70
+ context ".batch" do
71
+ it "should works" do
72
+ code, data = Storage.batch("stat", @bucket, [@key])
73
+ code.should == 200
74
+ puts data.inspect
75
+ end
76
+ end
77
+
78
+ context ".batch_stat" do
79
+ it "should works" do
80
+ code, data = Storage.batch_stat(@bucket, [@key])
81
+ code.should == 200
82
+ puts data.inspect
83
+ end
84
+ end
85
+
86
+ context ".batch_get" do
87
+ it "should works" do
88
+ code, data = Storage.batch_get(@bucket, [@key])
89
+ code.should == 200
90
+ puts data.inspect
91
+ end
92
+ end
93
+
94
+ context ".batch_copy" do
95
+ it "should works" do
96
+ code, data = Storage.batch_copy @bucket, @key, @bucket, @key2
97
+ code.should == 200
98
+ puts data.inspect
99
+
100
+ code, data = Storage.delete @bucket, @key2
101
+ code.should == 200
102
+ puts data.inspect
103
+ end
104
+ end
105
+
106
+ context ".batch_move" do
107
+ it "should works" do
108
+ code, data = Storage.batch_move @bucket, @key, @bucket, @key2
109
+ code.should == 200
110
+ puts data.inspect
111
+
112
+ code3, data3 = Storage.batch_move @bucket, @key2, @bucket, @key
113
+ code3.should == 200
114
+ puts data3.inspect
115
+ end
116
+ end
117
+
118
+ context ".move" do
119
+ it "should works" do
120
+ code, data = Storage.move(@bucket, @key, @bucket, @key2)
121
+ code.should == 200
122
+ puts data.inspect
123
+
124
+ code2, data2 = Storage.stat(@bucket, @key2)
125
+ code2.should == 200
126
+ puts data2.inspect
127
+
128
+ code3, data3 = Storage.move(@bucket, @key2, @bucket, @key)
129
+ code3.should == 200
130
+ puts data3.inspect
131
+ end
132
+ end
133
+
134
+ context ".copy" do
135
+ it "should works" do
136
+ code, data = Storage.copy(@bucket, @key, @bucket, @key2)
137
+ code.should == 200
138
+ puts data.inspect
139
+
140
+ code, data = Storage.delete(@bucket, @key2)
141
+ code.should == 200
142
+ puts data.inspect
143
+ end
144
+ end
145
+
146
+ context ".delete" do
147
+ it "should works" do
148
+ code, data = Storage.delete(@bucket, @key)
149
+ code.should == 200
150
+ puts data.inspect
151
+ end
152
+ end
153
+
154
+ end
155
+ end # module Storage
156
+ end # module Qiniu
@@ -0,0 +1,59 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/auth'
5
+ require 'qiniu'
6
+ require 'qiniu/misc'
7
+
8
+ module Qiniu
9
+ module Misc
10
+ describe Misc do
11
+
12
+ before :all do
13
+ ### 复用RubySDK-Test-Management空间
14
+ @bucket = 'RubySDK-Test-Management'
15
+ @bucket = make_unique_bucket(@bucket)
16
+
17
+ result = Qiniu.mkbucket(@bucket)
18
+ puts result.inspect
19
+ end
20
+
21
+ after :all do
22
+ ### 不删除Bucket以备下次使用
23
+ end
24
+
25
+ context ".set_protected" do
26
+ it "should works" do
27
+ code, data = Qiniu::Misc.set_protected(@bucket, 1)
28
+ code.should == 200
29
+ puts data.inspect
30
+ end
31
+ end
32
+
33
+ context ".set_separator" do
34
+ it "should works" do
35
+ code, data = Qiniu::Misc.set_separator(@bucket, "-")
36
+ code.should == 200
37
+ puts data.inspect
38
+ end
39
+ end
40
+
41
+ context ".set_style" do
42
+ it "should works" do
43
+ code, data = Qiniu::Misc.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80")
44
+ code.should == 200
45
+ puts data.inspect
46
+ end
47
+ end
48
+
49
+ context ".unset_style" do
50
+ it "should works" do
51
+ code, data = Qiniu::Misc.unset_style(@bucket, "small.jpg")
52
+ code.should == 200
53
+ puts data.inspect
54
+ end
55
+ end
56
+
57
+ end
58
+ end # module Misc
59
+ end # module Qiniu
@@ -0,0 +1,89 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # vim: sw=2 ts=2
3
+
4
+ require 'spec_helper'
5
+ require 'qiniu/auth'
6
+ require 'qiniu'
7
+ require 'qiniu/fop'
8
+
9
+ module Qiniu
10
+ module Fop
11
+ module Persistance
12
+ describe Persistance do
13
+
14
+ before :all do
15
+ ### 复用RubySDK-Test-Storage空间
16
+ @bucket = 'RubySDK-Test-Storage'
17
+ @bucket = make_unique_bucket(@bucket)
18
+
19
+ ### 尝试创建空间
20
+ code, data, headers = Qiniu::Storage.make_a_public_bucket(@bucket)
21
+ puts code.inspect
22
+ puts data.inspect
23
+ puts headers.inspect
24
+
25
+ pic_fname = "image_logo_for_test.png"
26
+ @key = make_unique_key_in_bucket(pic_fname)
27
+
28
+ local_file = File.expand_path('../' + pic_fname, __FILE__)
29
+
30
+ ### 检查测试文件存在性
31
+ code, body, headers = Qiniu::Storage.stat(@bucket, @key)
32
+ if code == 404 || code == 612 then
33
+ # 文件不存在,尝试上传
34
+ pp = Qiniu::Auth.PutPolicy.new(@bucket, @key)
35
+ code, body, headers = Qiniu::Storage.upload_with_put_policy(
36
+ pp,
37
+ local_file
38
+ )
39
+ puts "Put a test file for Persistance cases"
40
+ puts code.inspect
41
+ puts body.inspect
42
+ puts header.inspect
43
+ end
44
+ end
45
+
46
+ after :all do
47
+ ### 不删除Bucket以备下次使用
48
+ end
49
+
50
+ context ".pfop" do
51
+ it "should works" do
52
+ pp = Persistance::PfopPolicy.new(
53
+ @bucket,
54
+ @key,
55
+ 'imageView2/1/w/80/h/80', # fops
56
+ 'www.baidu.com' # notify_url
57
+ )
58
+
59
+ code, data, headers = Qiniu::Fop::Persistance.pfop(pp)
60
+ code.should == 200
61
+ puts data.inspect
62
+ end
63
+ end
64
+
65
+ context ".prefop" do
66
+ it "should works" do
67
+ code, data, headers = Qiniu::Fop::Persistance.prefop('fakePersistentId')
68
+ code.should == 612
69
+ puts code.inspect
70
+ puts data.inspect
71
+ puts headers.inspect
72
+ end
73
+ end
74
+
75
+ context ".p1" do
76
+ it "should works" do
77
+ url = 'http://fake.qiniudn.com/fake.jpg'
78
+ fop = 'imageView2/1/w/80/h/80'
79
+ target_url = "#{url}?p/1/#{CGI.escape(fop).gsub('+', '%20')}"
80
+
81
+ p1_url = Qiniu::Fop::Persistance.generate_p1_url(url, fop)
82
+ p1_url.should == target_url
83
+ puts p1_url.inspect
84
+ end
85
+ end
86
+ end
87
+ end # module Persistance
88
+ end # module Fop
89
+ end # module Qiniu