Compare commits

..
9 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
11 changed files with 153 additions and 12 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
qiniu-rs (2.0.5)
qiniu-rs (2.1.2)
json (~> 1.7.3)
mime-types (~> 1.19)
rest-client (~> 1.6.7)
+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 :RS, 'qiniu/rs/rs'
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
@@ -115,6 +118,21 @@ module Qiniu
Image.preivew_url(url, spec)
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
+1 -1
View File
@@ -59,7 +59,7 @@ module Qiniu
end
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]
end
-2
View File
@@ -21,9 +21,7 @@ module Qiniu
:content_type => 'application/x-www-form-urlencoded',
:auth_url => "https://acc.qbox.me/oauth2/token",
:rs_host => "http://rs.qbox.me:10100",
#:rs_host => "http://localhost:10100",
:io_host => "http://iovip.qbox.me",
#:io_host => "http://localhost:10200",
:client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a",
:client_secret => "75df554a39f58accb7eb293b550fa59618674b7d",
:access_key => "",
+25 -5
View File
@@ -38,12 +38,15 @@ module Qiniu
:accept => :json,
:user_agent => Config.settings[:user_agent]
}
if options[:signature_auth] && options[:signature_auth] == true
signature_token = generate_qbox_signature(Config.settings[:access_key], Config.settings[:secret_key], url, data)
header_options.merge!('Authorization' => "QBox #{signature_token}")
auth_token = nil
if !options[:qbox_signature_token].nil? && !options[:qbox_signature_token].empty?
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]
header_options.merge!('Authorization' => "Bearer #{options[:access_token]}")
auth_token = 'Bearer ' + options[:access_token]
end
header_options.merge!('Authorization' => auth_token) unless auth_token.nil?
case options[:method]
when :get
response = RestClient.get url, header_options
@@ -130,7 +133,9 @@ module Qiniu
File.open(filepath, "rb") { |f| Zlib.crc32 f.read }
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)
signature = uri.path
query_string = uri.query
@@ -146,6 +151,21 @@ module Qiniu
%Q(#{access_key}:#{encoded_digest})
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
+1 -1
View File
@@ -2,6 +2,6 @@
module Qiniu
module RS
VERSION = "2.0.5"
VERSION = "2.1.2"
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
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