Compare commits

...
15 Commits
Author SHA1 Message Date
qiniu b2f496551b Merge pull request #5 from why404/master
release/v2.1.2 - added generate upload token logic
2012-07-03 19:27:39 -07:00
404 07a01834d6 Merge branch 'release/v2.1.2' 2012-07-03 16:32:35 +08:00
404 c60cc6e4b8 updated Qiniu::RS.generate_upload_token() 2012-07-03 16:29:41 +08:00
404 0a4f8c1471 Merge branch 'hotfix/v2.1.1' into develop 2012-07-02 18:18:56 +08:00
404 9719aae614 Merge branch 'hotfix/v2.1.1' 2012-07-02 18:18:47 +08:00
404 f8c3f93d29 updated README 2012-07-02 18:18:34 +08:00
404 971fa3e92b Merge branch 'release/v2.1.0' 2012-07-02 18:08:08 +08:00
404 70d6d55e0c added Qiniu::RS.generate_upload_token() 2012-07-02 18:07:11 +08:00
404 8e90310b9c Update master 2012-06-29 16:45:17 +08:00
404 8cf75eecbd Merge branch 'hotfix/v2.0.5' 2012-06-29 11:46:12 +08:00
404 ccd46828c6 fixed raise NoSuchFileError bug. #3 2012-06-29 11:45:42 +08:00
404 2cc53fabdd Merge branch 'hotfix/v2.0.4' 2012-06-29 02:25:46 +08:00
404 7eb27d0deb fixed VERSION number 2012-06-29 02:25:31 +08:00
404 b0b568d0c7 Merge branch 'hotfix/v2.0.3' 2012-06-29 02:20:51 +08:00
404 cffec32884 updated gem source and Gemfile 2012-06-29 02:20:35 +08:00
13 changed files with 164 additions and 21 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
source 'http://ruby.taobao.org' source 'https://rubygems.org'
# Specify your gem's dependencies in qiniu-s3.gemspec # Specify your gem's dependencies in qiniu-s3.gemspec
gemspec gemspec
+10 -8
View File
@@ -1,10 +1,11 @@
PATH PATH
remote: . remote: .
specs: specs:
qiniu-rs (1.1.0) qiniu-rs (2.1.2)
json json (~> 1.7.3)
mime-types mime-types (~> 1.19)
rest-client rest-client (~> 1.6.7)
ruby-hmac (~> 0.4.0)
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
@@ -12,7 +13,7 @@ GEM
diff-lcs (1.1.3) diff-lcs (1.1.3)
fakeweb (1.3.0) fakeweb (1.3.0)
json (1.7.3) json (1.7.3)
mime-types (1.18) mime-types (1.19)
rake (0.9.2.2) rake (0.9.2.2)
rest-client (1.6.7) rest-client (1.6.7)
mime-types (>= 1.16) mime-types (>= 1.16)
@@ -24,12 +25,13 @@ GEM
rspec-expectations (2.10.0) rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3) diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1) rspec-mocks (2.10.1)
ruby-hmac (0.4.0)
PLATFORMS PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
fakeweb fakeweb (~> 1.3.0)
qiniu-rs! qiniu-rs!
rake rake (~> 0.9.2.2)
rspec rspec (~> 2.10.0)
+2 -2
View File
@@ -1,8 +1,8 @@
# Qiniu Resource (Cloud) Storage SDK for Ruby - [![Build Status](https://secure.travis-ci.org/why404/qiniu-rs.png?branch=master)](http://travis-ci.org/why404/qiniu-rs) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs.png)](https://gemnasium.com/why404/qiniu-rs) # Qiniu Resource (Cloud) Storage SDK for Ruby - [![Build Status](https://secure.travis-ci.org/why404/qiniu-rs-for-ruby.png?branch=master)](http://travis-ci.org/why404/qiniu-rs-for-ruby) [![Dependency Status](https://gemnasium.com/why404/qiniu-rs-for-ruby.png)](https://gemnasium.com/why404/qiniu-rs-for-ruby)
# 关于 # 关于
此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](http://docs.qiniutek.com/v1/api/) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。 此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, jruby, rbx, ree 版本,基于 [七牛云存储官方API](http://docs.qiniutek.com/v2/api/) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。
## 安装 ## 安装
+18
View File
@@ -12,6 +12,9 @@ module Qiniu
autoload :IO, 'qiniu/rs/io' autoload :IO, 'qiniu/rs/io'
autoload :RS, 'qiniu/rs/rs' autoload :RS, 'qiniu/rs/rs'
autoload :Image, 'qiniu/rs/image' autoload :Image, 'qiniu/rs/image'
autoload :AccessToken, 'qiniu/tokens/access_token'
autoload :QboxToken, 'qiniu/tokens/qbox_token'
autoload :UploadToken, 'qiniu/tokens/upload_token'
class << self class << self
@@ -115,6 +118,21 @@ module Qiniu
Image.preivew_url(url, spec) Image.preivew_url(url, spec)
end end
#def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
# Utils.generate_upload_token(scope, expires_in, callback_url, return_url)
#end
def generate_upload_token(opts = {})
token_obj = UploadToken.new(opts)
token_obj.access_key = Config.settings[:access_key]
token_obj.secret_key = Config.settings[:secret_key]
#token_obj.scope = opts[:scope]
#token_obj.expires_in = opts[:expires_in]
#token_obj.callback_url = opts[:callback_url]
#token_obj.return_url = opts[:return_url]
token_obj.generate_token
end
end end
end end
+1 -1
View File
@@ -59,7 +59,7 @@ module Qiniu
end end
def call_with_signature(url, data, retry_times = 0) def call_with_signature(url, data, retry_times = 0)
code, data = http_request url, data, {:signature_auth => true} code, data = http_request url, data, {:qbox_signature_token => generate_qbox_signature(url, data)}
[code, data] [code, data]
end end
-2
View File
@@ -21,9 +21,7 @@ module Qiniu
:content_type => 'application/x-www-form-urlencoded', :content_type => 'application/x-www-form-urlencoded',
:auth_url => "https://acc.qbox.me/oauth2/token", :auth_url => "https://acc.qbox.me/oauth2/token",
:rs_host => "http://rs.qbox.me:10100", :rs_host => "http://rs.qbox.me:10100",
#:rs_host => "http://localhost:10100",
:io_host => "http://iovip.qbox.me", :io_host => "http://iovip.qbox.me",
#:io_host => "http://localhost:10200",
:client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a", :client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a",
:client_secret => "75df554a39f58accb7eb293b550fa59618674b7d", :client_secret => "75df554a39f58accb7eb293b550fa59618674b7d",
:access_key => "", :access_key => "",
+1 -1
View File
@@ -21,7 +21,7 @@ module Qiniu
end end
def put_file(url, local_file, bucket = nil, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false) def put_file(url, local_file, bucket = nil, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false)
raise NoSuchFileError unless File.exist?(local_file) raise NoSuchFileError, local_file unless File.exist?(local_file)
key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) if key.nil? key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) if key.nil?
entry_uri = bucket + ':' + key entry_uri = bucket + ':' + key
if mime_type.nil? if mime_type.nil?
+25 -5
View File
@@ -38,12 +38,15 @@ module Qiniu
:accept => :json, :accept => :json,
:user_agent => Config.settings[:user_agent] :user_agent => Config.settings[:user_agent]
} }
if options[:signature_auth] && options[:signature_auth] == true auth_token = nil
signature_token = generate_qbox_signature(Config.settings[:access_key], Config.settings[:secret_key], url, data) if !options[:qbox_signature_token].nil? && !options[:qbox_signature_token].empty?
header_options.merge!('Authorization' => "QBox #{signature_token}") auth_token = 'QBox ' + options[:qbox_signature_token]
#elsif !options[:upload_signature_token].nil? && !options[:upload_signature_token].empty?
# auth_token = 'UpToken ' + options[:upload_signature_token]
elsif options[:access_token] elsif options[:access_token]
header_options.merge!('Authorization' => "Bearer #{options[:access_token]}") auth_token = 'Bearer ' + options[:access_token]
end end
header_options.merge!('Authorization' => auth_token) unless auth_token.nil?
case options[:method] case options[:method]
when :get when :get
response = RestClient.get url, header_options response = RestClient.get url, header_options
@@ -130,7 +133,9 @@ module Qiniu
File.open(filepath, "rb") { |f| Zlib.crc32 f.read } File.open(filepath, "rb") { |f| Zlib.crc32 f.read }
end end
def generate_qbox_signature(access_key, secret_key, url, params) def generate_qbox_signature(url, params)
access_key = Config.settings[:access_key]
secret_key = Config.settings[:secret_key]
uri = URI.parse(url) uri = URI.parse(url)
signature = uri.path signature = uri.path
query_string = uri.query query_string = uri.query
@@ -146,6 +151,21 @@ module Qiniu
%Q(#{access_key}:#{encoded_digest}) %Q(#{access_key}:#{encoded_digest})
end end
=begin
def generate_upload_token(scope, expires_in, callback_url = nil, return_url = nil)
access_key = Config.settings[:access_key]
secret_key = Config.settings[:secret_key]
params = {:scope => scope, :deadline => Time.now.to_i + expires_in}
params[:callbackUrl] = callback_url if !callback_url.nil? && !calback_url.empty?
params[:returnUrl] = return_url if !return_url.nil? && !return_url.empty?
signature = urlsafe_base64_encode(params.to_json)
hmac = HMAC::SHA1.new(secret_key)
hmac.update(signature)
encoded_digest = urlsafe_base64_encode(hmac.digest)
%Q(#{access_key}:#{encoded_digest}:#{signature})
end
=end
end end
end end
end end
+1 -1
View File
@@ -2,6 +2,6 @@
module Qiniu module Qiniu
module RS module RS
VERSION = "2.0.2" VERSION = "2.1.2"
end end
end end
+23
View File
@@ -0,0 +1,23 @@
# -*- encoding: utf-8 -*-
require 'hmac-sha1'
require 'qiniu/rs/config'
require 'qiniu/rs/utils'
module Qiniu
module RS
class AccessToken
include Utils
attr_accessor :access_key, :secret_key
def generate_encoded_digest(signature)
hmac = HMAC::SHA1.new(@secret_key)
hmac.update(signature)
urlsafe_base64_encode(hmac.digest)
end
end
end
end
+37
View File
@@ -0,0 +1,37 @@
# -*- encoding: utf-8 -*-
require 'json'
require 'qiniu/tokens/access_token'
module Qiniu
module RS
class QboxToken < AccessToken
attr_accessor :url, :params
def initialize(opts = {})
@url = opts[:url]
@params = opts[:params]
end
def generate_signature
uri = URI.parse(@url)
signature = uri.path
query_string = uri.query
signature += '?' + query_string if !query_string.nil? && !query_string.empty?
signature += "\n";
if @params.is_a?(Hash)
total_param = @params.map { |key, value| key.to_s+"="+value.to_s }
signature += total_param.join("&")
end
signature
end
def generate_token
encoded_digest = generate_encoded_digest(signature)
%Q(#{@access_key}:#{encoded_digest})
end
end
end
end
+37
View File
@@ -0,0 +1,37 @@
# -*- encoding: utf-8 -*-
require 'json'
require 'qiniu/tokens/access_token'
require 'qiniu/rs/utils'
module Qiniu
module RS
class UploadToken < AccessToken
include Utils
attr_accessor :scope, :expires_in, :callback_url, :return_url
def initialize(opts = {})
@scope = opts[:scope]
@expires_in = opts[:expires_in]
@callback_url = opts[:callback_url]
@return_url = opts[:return_url]
end
def generate_signature
params = {:scope => @scope, :deadline => Time.now.to_i + @expires_in}
params[:callbackUrl] = @callback_url if !@callback_url.nil? && !@calback_url.empty?
params[:returnUrl] = @return_url if !@return_url.nil? && !@return_url.empty?
urlsafe_base64_encode(params.to_json)
end
def generate_token
signature = generate_signature
encoded_digest = generate_encoded_digest(signature)
%Q(#{@access_key}:#{encoded_digest}:#{signature})
end
end
end
end
+8
View File
@@ -149,5 +149,13 @@ module Qiniu
end end
end end
context ".generate_upload_token" do
it "should works" do
data = Qiniu::RS.generate_upload_token({:scope => 'test_bucket', :expires_in => 3600})
data.should_not be_empty
puts data.inspect
end
end
end end
end end