qiniu-rs 2.3.3 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/qiniu/rs.rb CHANGED
@@ -11,6 +11,8 @@ module Qiniu
11
11
  autoload :Auth, 'qiniu/rs/auth'
12
12
  autoload :IO, 'qiniu/rs/io'
13
13
  autoload :RS, 'qiniu/rs/rs'
14
+ autoload :EU, 'qiniu/rs/eu'
15
+ autoload :Pub, 'qiniu/rs/pub'
14
16
  autoload :Image, 'qiniu/rs/image'
15
17
  autoload :AccessToken, 'qiniu/tokens/access_token'
16
18
  autoload :QboxToken, 'qiniu/tokens/qbox_token'
@@ -29,6 +31,46 @@ module Qiniu
29
31
  code == StatusOK
30
32
  end
31
33
 
34
+ def mkbucket(bucket_name)
35
+ code, data = RS.mkbucket(bucket_name)
36
+ code == StatusOK
37
+ end
38
+
39
+ def buckets
40
+ code, data = RS.buckets
41
+ code == StatusOK ? data : false
42
+ end
43
+
44
+ def set_protected(bucket, protected_mode)
45
+ code, data = Pub.set_protected(bucket, protected_mode)
46
+ code == StatusOK
47
+ end
48
+
49
+ def set_separator(bucket, separator)
50
+ code, data = Pub.set_separator(bucket, separator)
51
+ code == StatusOK
52
+ end
53
+
54
+ def set_style(bucket, name, style)
55
+ code, data = Pub.set_style(bucket, name, style)
56
+ code == StatusOK
57
+ end
58
+
59
+ def unset_style(bucket, name)
60
+ code, data = Pub.unset_style(bucket, name)
61
+ code == StatusOK
62
+ end
63
+
64
+ def set_watermark(customer_id, options = {})
65
+ code, data = EU.set_watermark(customer_id, options)
66
+ code == StatusOK
67
+ end
68
+
69
+ def get_watermark(customer_id = nil)
70
+ code, data = EU.get_watermark(customer_id)
71
+ code == StatusOK ? data : false
72
+ end
73
+
32
74
  def put_auth(expires_in = nil, callback_url = nil)
33
75
  code, data = IO.put_auth(expires_in, callback_url)
34
76
  code == StatusOK ? data["url"] : false
@@ -56,6 +98,18 @@ module Qiniu
56
98
  code == StatusOK
57
99
  end
58
100
 
101
+ def upload_file opts = {}
102
+ code, data = IO.upload_with_token(opts[:uptoken],
103
+ opts[:file],
104
+ opts[:bucket],
105
+ opts[:key],
106
+ opts[:mime_type],
107
+ opts[:note],
108
+ opts[:callback_params],
109
+ opts[:enable_crc32_check])
110
+ code == StatusOK ? data : false
111
+ end
112
+
59
113
  def stat(bucket, key)
60
114
  code, data = RS.stat(bucket, key)
61
115
  code == StatusOK ? data : false
@@ -142,10 +196,6 @@ module Qiniu
142
196
  code == StatusOK ? data : false
143
197
  end
144
198
 
145
- #def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
146
- # Utils.generate_upload_token(scope, expires_in, callback_url, return_url)
147
- #end
148
-
149
199
  def generate_upload_token(opts = {})
150
200
  token_obj = UploadToken.new(opts)
151
201
  token_obj.access_key = Config.settings[:access_key]
@@ -153,7 +203,8 @@ module Qiniu
153
203
  #token_obj.scope = opts[:scope]
154
204
  #token_obj.expires_in = opts[:expires_in]
155
205
  #token_obj.callback_url = opts[:callback_url]
156
- #token_obj.return_url = opts[:return_url]
206
+ #token_obj.callback_body_type = opts[:callback_body_type]
207
+ #token_obj.customer = opts[:customer]
157
208
  token_obj.generate_token
158
209
  end
159
210
 
@@ -22,6 +22,9 @@ module Qiniu
22
22
  :auth_url => "https://acc.qbox.me/oauth2/token",
23
23
  :rs_host => "http://rs.qbox.me:10100",
24
24
  :io_host => "http://iovip.qbox.me",
25
+ :up_host => "http://up.qbox.me",
26
+ :pub_host => "http://pu.qbox.me:10200",
27
+ :eu_host => "http://eu.qbox.me",
25
28
  :client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a",
26
29
  :client_secret => "75df554a39f58accb7eb293b550fa59618674b7d",
27
30
  :access_key => "",
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Qiniu
4
+ module RS
5
+ module EU
6
+ class << self
7
+ include Utils
8
+
9
+ def set_watermark(customer_id, options = {})
10
+ Auth.request Config.settings[:eu_host] + '/wmset', options.merge({:customer => customer_id})
11
+ end
12
+
13
+ def get_watermark(customer_id = nil)
14
+ post_data = customer_id.nil? ? nil : {:customer => customer_id}
15
+ Auth.request Config.settings[:eu_host] + '/wmget', post_data
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
22
+
data/lib/qiniu/rs/io.rb CHANGED
@@ -34,6 +34,14 @@ module Qiniu
34
34
  Auth.request url, ::IO.read(local_file), options
35
35
  end
36
36
 
37
+ def upload_with_token(uptoken, local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false)
38
+ action_params = _generate_action_params(local_file, bucket, key, mime_type, custom_meta, enable_crc32_check)
39
+ callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type} if callback_params.nil?
40
+ callback_query_string = Utils.generate_query_string(callback_params)
41
+ url = Config.settings[:up_host] + '/upload'
42
+ Utils.upload_multipart_data(url, local_file, action_params, callback_query_string, uptoken)
43
+ end
44
+
37
45
  private
38
46
  def _generate_action_params(local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, enable_crc32_check = false)
39
47
  raise NoSuchFileError, local_file unless File.exist?(local_file)
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Qiniu
4
+ module RS
5
+ module Pub
6
+ class << self
7
+ include Utils
8
+
9
+ def set_protected(bucket, protected_mode)
10
+ host = Config.settings[:pub_host]
11
+ Auth.request %Q(#{host}/accessMode/#{bucket}/mode/#{protected_mode})
12
+ end
13
+
14
+ def set_separator(bucket, separator)
15
+ host = Config.settings[:pub_host]
16
+ encoded_separator = Utils.urlsafe_base64_encode(separator)
17
+ Auth.request %Q(#{host}/separator/#{bucket}/sep/#{encoded_separator})
18
+ end
19
+
20
+ def set_style(bucket, name, style)
21
+ host = Config.settings[:pub_host]
22
+ encoded_name = Utils.urlsafe_base64_encode(name)
23
+ encoded_style = Utils.urlsafe_base64_encode(style)
24
+ Auth.request %Q(#{host}/style/#{bucket}/name/#{encoded_name}/style/#{encoded_style})
25
+ end
26
+
27
+ def unset_style(bucket, name)
28
+ host = Config.settings[:pub_host]
29
+ encoded_name = Utils.urlsafe_base64_encode(name)
30
+ Auth.request %Q(#{host}/unstyle/#{bucket}/name/#{encoded_name})
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
37
+
data/lib/qiniu/rs/rs.rb CHANGED
@@ -6,6 +6,14 @@ module Qiniu
6
6
  class << self
7
7
  include Utils
8
8
 
9
+ def buckets
10
+ Auth.request Config.settings[:rs_host] + '/buckets'
11
+ end
12
+
13
+ def mkbucket(bucket_name)
14
+ Auth.request Config.settings[:rs_host] + '/mkbucket/' + bucket_name
15
+ end
16
+
9
17
  def stat(bucket, key)
10
18
  Auth.request Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key)
11
19
  end
@@ -1,6 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  require 'uri'
4
+ require 'cgi'
4
5
  require 'json'
5
6
  require 'zlib'
6
7
  require 'base64'
@@ -91,20 +92,21 @@ module Qiniu
91
92
  end
92
93
  end
93
94
 
94
- def upload_multipart_data(url, filepath, action_string, callback_query_string = '')
95
+ def upload_multipart_data(url, filepath, action_string, callback_query_string = '', uptoken = nil)
95
96
  post_data = {
96
97
  :params => callback_query_string,
97
98
  :action => action_string,
98
99
  :file => File.new(filepath, 'rb'),
99
100
  :multipart => true
100
101
  }
102
+ post_data[:auth] = uptoken unless uptoken.nil?
101
103
  http_request url, post_data
102
104
  end
103
105
 
104
106
  def generate_query_string(params)
105
107
  return params if params.is_a?(String)
106
- total_param = params.map { |key, value| key.to_s+"="+value.to_s }
107
- URI.escape(total_param.join("&"))
108
+ total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
109
+ total_param.join("&")
108
110
  end
109
111
 
110
112
  def crc32checksum(filepath)
@@ -118,10 +120,10 @@ module Qiniu
118
120
  signature = uri.path
119
121
  query_string = uri.query
120
122
  signature += '?' + query_string if !query_string.nil? && !query_string.empty?
121
- signature += "\n";
123
+ signature += "\n"
122
124
  if params.is_a?(Hash)
123
- total_param = params.map { |key, value| key.to_s+"="+value.to_s }
124
- signature += total_param.join("&")
125
+ params_string = generate_query_string(params)
126
+ signature += params_string
125
127
  end
126
128
  hmac = HMAC::SHA1.new(secret_key)
127
129
  hmac.update(signature)
@@ -129,21 +131,6 @@ module Qiniu
129
131
  %Q(#{access_key}:#{encoded_digest})
130
132
  end
131
133
 
132
- =begin
133
- def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
134
- access_key = Config.settings[:access_key]
135
- secret_key = Config.settings[:secret_key]
136
- params = {:scope => scope, :deadline => Time.now.to_i + expires_in}
137
- params[:callbackUrl] = callback_url if !callback_url.nil? && !calback_url.empty?
138
- params[:returnUrl] = return_url if !return_url.nil? && !return_url.empty?
139
- signature = urlsafe_base64_encode(params.to_json)
140
- hmac = HMAC::SHA1.new(secret_key)
141
- hmac.update(signature)
142
- encoded_digest = urlsafe_base64_encode(hmac.digest)
143
- %Q(#{access_key}:#{encoded_digest}:#{signature})
144
- end
145
- =end
146
-
147
134
  end
148
135
  end
149
136
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Qiniu
4
4
  module RS
5
- VERSION = "2.3.3"
5
+ VERSION = "3.0.3"
6
6
  end
7
7
  end
@@ -1,5 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
+ require 'cgi'
3
4
  require 'json'
4
5
  require 'qiniu/tokens/access_token'
5
6
 
@@ -21,7 +22,7 @@ module Qiniu
21
22
  signature += '?' + query_string if !query_string.nil? && !query_string.empty?
22
23
  signature += "\n";
23
24
  if @params.is_a?(Hash)
24
- total_param = @params.map { |key, value| key.to_s+"="+value.to_s }
25
+ total_param = @params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
25
26
  signature += total_param.join("&")
26
27
  end
27
28
  signature
@@ -10,19 +10,21 @@ module Qiniu
10
10
 
11
11
  include Utils
12
12
 
13
- attr_accessor :scope, :expires_in, :callback_url, :return_url
13
+ attr_accessor :scope, :expires_in, :callback_url, :callback_body_type, :customer
14
14
 
15
15
  def initialize(opts = {})
16
16
  @scope = opts[:scope]
17
17
  @expires_in = opts[:expires_in]
18
18
  @callback_url = opts[:callback_url]
19
- @return_url = opts[:return_url]
19
+ @callback_body_type = opts[:callback_body_type]
20
+ @customer = opts[:customer]
20
21
  end
21
22
 
22
23
  def generate_signature
23
24
  params = {:scope => @scope, :deadline => Time.now.to_i + @expires_in}
24
- params[:callbackUrl] = @callback_url if !@callback_url.nil? && !@calback_url.empty?
25
- params[:returnUrl] = @return_url if !@return_url.nil? && !@return_url.empty?
25
+ params[:callbackUrl] = @callback_url if !@callback_url.nil? && !@callback_url.empty?
26
+ params[:callbackBodyType] = @callback_body_type if !@callback_body_type.nil? && !@callback_body_type.empty?
27
+ params[:customer] = @customer if !@customer.nil? && !@customer.empty?
26
28
  urlsafe_base64_encode(params.to_json)
27
29
  end
28
30
 
data/qiniu-rs.gemspec CHANGED
@@ -5,7 +5,7 @@ require File.expand_path('../lib/qiniu/rs/version', __FILE__)
5
5
  Gem::Specification.new do |gem|
6
6
  gem.authors = ["why404"]
7
7
  gem.email = ["why404@gmail.com"]
8
- gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniutek.com/v2/sdk/ruby/}
8
+ gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniutek.com/v3/sdk/ruby/}
9
9
  gem.summary = %q{Qiniu Resource (Cloud) Storage SDK for Ruby}
10
10
  gem.homepage = "https://github.com/qiniu/ruby-sdk"
11
11
 
@@ -17,8 +17,8 @@ Gem::Specification.new do |gem|
17
17
  gem.version = Qiniu::RS::VERSION
18
18
 
19
19
  # specify any dependencies here; for example:
20
- gem.add_development_dependency "rake", "~> 0.9.2.2"
21
- gem.add_development_dependency "rspec", "~> 2.10.0"
20
+ gem.add_development_dependency "rake", "~> 0.9"
21
+ gem.add_development_dependency "rspec", "~> 2.11"
22
22
  gem.add_development_dependency "fakeweb", "~> 1.3.0"
23
23
  gem.add_runtime_dependency "json", "~> 1.7.3"
24
24
  gem.add_runtime_dependency "rest-client", "~> 1.6.7"
@@ -8,8 +8,8 @@ module Qiniu
8
8
  describe Auth do
9
9
 
10
10
  before :all do
11
- @username = "test@qbox.net"
12
- @password = "test"
11
+ @username = "qboxtest"
12
+ @password = "qboxtest123"
13
13
 
14
14
  code, data = Qiniu::RS::Auth.exchange_by_password!(@username, @password)
15
15
  code.should == 200
@@ -0,0 +1,60 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/rs/io'
5
+ require 'qiniu/rs/pub'
6
+ require 'qiniu/rs/eu'
7
+
8
+ module Qiniu
9
+ module RS
10
+ describe EU do
11
+
12
+ before :all do
13
+ @customer_id = "awhy.xu@gmail.com"
14
+ @bucket = "wm_test_bucket"
15
+ @key = "image_logo_for_test.png"
16
+
17
+ local_file = File.expand_path('../' + @key, __FILE__)
18
+ upopts = {:scope => @bucket, :expires_in => 3600, :customer => @customer_id}
19
+ uptoken = Qiniu::RS.generate_upload_token(upopts)
20
+
21
+ code, data = Qiniu::RS::IO.upload_with_token(uptoken, local_file, @bucket, @key, nil, nil, nil, true)
22
+ code.should == 200
23
+ puts data.inspect
24
+
25
+ code2, data2 = Qiniu::RS::Pub.set_separator(@bucket, "-")
26
+ code2.should == 200
27
+ puts data2.inspect
28
+
29
+ code3, data3 = Qiniu::RS::Pub.set_style(@bucket, "small.jpg", "imageView/1/w/120/h/120/q/85/format/jpg/watermark/1")
30
+ code3.should == 200
31
+ puts data3.inspect
32
+ end
33
+
34
+ #after :all do
35
+ # result = Qiniu::RS.drop(@bucket)
36
+ # result.should_not be_false
37
+ #end
38
+
39
+ context ".set_watermark" do
40
+ it "should works" do
41
+ options = {
42
+ :text => "Powered by QiniuRS"
43
+ }
44
+ code, data = Qiniu::RS::EU.set_watermark(@customer_id, options)
45
+ code.should == 200
46
+ puts data.inspect
47
+ end
48
+ end
49
+
50
+ context ".get_watermark" do
51
+ it "should works" do
52
+ code, data = Qiniu::RS::EU.get_watermark(@customer_id)
53
+ code.should == 200
54
+ puts data.inspect
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -1,4 +1,4 @@
1
- # -*- encoding: utf-8 -*-
1
+ # Utils.-*- encoding: utf-8 -*-
2
2
 
3
3
  require 'spec_helper'
4
4
  require 'qiniu/rs/auth'
@@ -44,6 +44,16 @@ module Qiniu
44
44
  end
45
45
  end
46
46
 
47
+ context ".upload_with_token" do
48
+ it "should works" do
49
+ upopts = {:scope => @bucket, :expires_in => 3600, :customer => "awhy.xu@gmail.com"}
50
+ uptoken = Qiniu::RS.generate_upload_token(upopts)
51
+ code, data = Qiniu::RS::IO.upload_with_token(uptoken, __FILE__, @bucket, @key, nil, nil, nil, true)
52
+ code.should == 200
53
+ puts data.inspect
54
+ end
55
+ end
56
+
47
57
  end
48
58
  end
49
59
  end
@@ -0,0 +1,68 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/rs/auth'
5
+ require 'qiniu/rs/rs'
6
+ require 'qiniu/rs/pub'
7
+
8
+ module Qiniu
9
+ module RS
10
+ describe Pub do
11
+
12
+ before :all do
13
+ @bucket = "test123"
14
+ code, data = Qiniu::RS::RS.mkbucket(@bucket)
15
+ code.should == 200
16
+ puts data.inspect
17
+ end
18
+
19
+ =begin
20
+ context ".mkbucket" do
21
+ it "should works" do
22
+ code, data = Qiniu::RS::RS.mkbucket(@bucket)
23
+ code.should == 200
24
+ puts data.inspect
25
+ end
26
+ end
27
+ =end
28
+
29
+ after :all do
30
+ result = Qiniu::RS.drop(@bucket)
31
+ result.should_not be_false
32
+ end
33
+
34
+ context ".set_protected" do
35
+ it "should works" do
36
+ code, data = Qiniu::RS::Pub.set_protected(@bucket, 1)
37
+ code.should == 200
38
+ puts data.inspect
39
+ end
40
+ end
41
+
42
+ context ".set_separator" do
43
+ it "should works" do
44
+ code, data = Qiniu::RS::Pub.set_separator(@bucket, "-")
45
+ code.should == 200
46
+ puts data.inspect
47
+ end
48
+ end
49
+
50
+ context ".set_style" do
51
+ it "should works" do
52
+ code, data = Qiniu::RS::Pub.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80")
53
+ code.should == 200
54
+ puts data.inspect
55
+ end
56
+ end
57
+
58
+ context ".unset_style" do
59
+ it "should works" do
60
+ code, data = Qiniu::RS::Pub.unset_style(@bucket, "small.jpg")
61
+ code.should == 200
62
+ puts data.inspect
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end