Compare commits

...
14 Commits
Author SHA1 Message Date
404 0f59b15100 Merge branch 'hotfix/v2.0.2' 2012-06-29 02:12:28 +08:00
404 459c32c278 fixed sdk doc link 2012-06-29 02:12:12 +08:00
404 5dc205ab46 Merge branch 'release/v2.0.1' 2012-06-27 20:44:49 +08:00
404 87da8ed256 added access_key and secret_key auth supported 2012-06-27 20:43:35 +08:00
404 d9738e4190 Merge pull request #2 from lidaobing/master
add Gemfile.lock. thx lidaobing
2012-06-10 05:43:30 -07:00
LI Daobing 1e08f5bb17 add Gemfile.lock 2012-06-10 19:21:59 +08:00
404 38326cbb83 Merge branch 'release/v1.1.0' 2012-06-08 14:05:17 +08:00
404 853c3b537d added file uploading crc32 check 2012-06-08 14:03:04 +08:00
404 285b766819 Merge branch 'hotfix/v1.0.4' into develop 2012-05-28 21:34:24 +08:00
404 e54081dd88 Merge branch 'hotfix/v1.0.4' 2012-05-28 21:33:52 +08:00
404 a0657ee26f added jruby-openssl bundling if RUBY_PLATFORM is JAVA 2012-05-28 21:33:27 +08:00
404 0fa0839a70 Merge branch 'hotfix/v1.0.3' into develop 2012-05-28 21:22:32 +08:00
404 4c6be498b5 Merge branch 'hotfix/v1.0.3' 2012-05-28 21:22:08 +08:00
404 47db00b7cf bundled gem json 2012-05-28 21:21:49 +08:00
17 changed files with 149 additions and 39 deletions
-1
View File
@@ -5,7 +5,6 @@
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
+5 -1
View File
@@ -1,4 +1,8 @@
source 'https://rubygems.org'
source 'http://ruby.taobao.org'
# Specify your gem's dependencies in qiniu-s3.gemspec
gemspec
# platforms :jruby do
# gem "jruby-openssl"
# end
+35
View File
@@ -0,0 +1,35 @@
PATH
remote: .
specs:
qiniu-rs (1.1.0)
json
mime-types
rest-client
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.1.3)
fakeweb (1.3.0)
json (1.7.3)
mime-types (1.18)
rake (0.9.2.2)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
rspec-mocks (~> 2.10.0)
rspec-core (2.10.1)
rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)
PLATFORMS
ruby
DEPENDENCIES
fakeweb
qiniu-rs!
rake
rspec
+2 -2
View File
@@ -2,7 +2,7 @@
# 关于
此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x 版本,基于 [七牛云存储官方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/v1/api/) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。
## 安装
@@ -20,7 +20,7 @@
## 使用
参考文档:[七牛云存储 Ruby SDK 使用指南](http://docs.qiniutek.com/v1/sdk/ruby/)
参考文档:[七牛云存储 Ruby SDK 使用指南](http://docs.qiniutek.com/v2/sdk/ruby/)
## 贡献代码
+9 -2
View File
@@ -31,8 +31,15 @@ module Qiniu
code == StatusOK ? data["url"] : false
end
def upload(url, local_file, bucket = '', key = '', mime_type = '', custom_meta = '', callback_params = {})
code, data = IO.put_file(url, local_file, bucket, key, mime_type, custom_meta, callback_params)
def upload opts = {}
code, data = IO.put_file(opts[:url],
opts[:file],
opts[:bucket],
opts[:key],
opts[:mime_type],
opts[:note],
opts[:callback_params],
opts[:enable_crc32_check])
code == StatusOK
end
+12 -3
View File
@@ -38,7 +38,7 @@ module Qiniu
@refresh_token = refresh_token
end
def call(url, data, retry_times = 0)
def call_with_logged_in(url, data, retry_times = 0)
raise MissingAccessToken if @access_token.nil?
code, data = http_request url, data, {:access_token => @access_token}
if code == 401
@@ -51,16 +51,25 @@ module Qiniu
if code == 200
retry_times += 1
if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times]
return call(url, data, retry_times)
return call_with_logged_in(url, data, retry_times)
end
end
end
[code, data]
end
def call_with_signature(url, data, retry_times = 0)
code, data = http_request url, data, {:signature_auth => true}
[code, data]
end
def request(url, data = nil)
begin
code, data = Auth.call(url, data)
if Config.settings[:access_key].empty? || Config.settings[:secret_key].empty?
code, data = Auth.call_with_logged_in(url, data)
else
code, data = Auth.call_with_signature(url, data)
end
rescue [MissingAccessToken, MissingRefreshToken, MissingUsernameOrPassword] => e
Log.logger.error e
code, data = 401, {}
+7 -3
View File
@@ -21,9 +21,13 @@ module Qiniu
:content_type => 'application/x-www-form-urlencoded',
:auth_url => "https://acc.qbox.me/oauth2/token",
:rs_host => "http://rs.qbox.me:10100",
:io_host => "http://io.qbox.me",
:client_id => "<YOUR_APP_CLIENT_ID>",
:client_secret => "<YOUR_APP_CLIENT_SECRET>",
#:rs_host => "http://localhost:10100",
:io_host => "http://iovip.qbox.me",
#:io_host => "http://localhost:10200",
:client_id => "a75604760c4da4caaa456c0c5895c061c3065c5a",
:client_secret => "75df554a39f58accb7eb293b550fa59618674b7d",
:access_key => "",
:secret_key => "",
:auto_reconnect => true,
:max_retry_times => 5
}
+6 -5
View File
@@ -20,17 +20,18 @@ module Qiniu
Auth.request(url)
end
def put_file(url, local_file, bucket = '', key = '', mime_type = '', custom_meta = '', callback_params = '')
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)
key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) if key.empty?
key = Digest::SHA1.hexdigest(local_file + Time.now.to_s) if key.nil?
entry_uri = bucket + ':' + key
if mime_type.empty?
if mime_type.nil?
mime = MIME::Types.type_for local_file
mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type
end
action_params = '/rs-put/' + Utils.urlsafe_base64_encode(entry_uri) + '/mimeType/' + Utils.urlsafe_base64_encode(mime_type)
action_params += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) unless custom_meta.empty?
callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type} if callback_params.empty?
action_params += '/meta/' + Utils.urlsafe_base64_encode(custom_meta) unless custom_meta.nil?
action_params += '/crc32/' + Utils.crc32checksum(local_file).to_s if enable_crc32_check
callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type} if callback_params.nil?
callback_query_string = Utils.generate_query_string(callback_params)
Utils.upload_multipart_data(url, local_file, action_params, callback_query_string)
end
+28 -1
View File
@@ -2,8 +2,10 @@
require 'uri'
require 'json'
require 'zlib'
require 'base64'
require 'rest_client'
require 'hmac-sha1'
require 'qiniu/rs/exceptions'
module Qiniu
@@ -36,7 +38,12 @@ module Qiniu
:accept => :json,
:user_agent => Config.settings[:user_agent]
}
header_options.merge!('Authorization' => "Bearer #{options[:access_token]}") if options[:access_token]
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}")
elsif options[:access_token]
header_options.merge!('Authorization' => "Bearer #{options[:access_token]}")
end
case options[:method]
when :get
response = RestClient.get url, header_options
@@ -119,6 +126,26 @@ module Qiniu
URI.escape(total_param.join("&"))
end
def crc32checksum(filepath)
File.open(filepath, "rb") { |f| Zlib.crc32 f.read }
end
def generate_qbox_signature(access_key, secret_key, url, params)
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
hmac = HMAC::SHA1.new(secret_key)
hmac.update(signature)
encoded_digest = urlsafe_base64_encode(hmac.digest)
%Q(#{access_key}:#{encoded_digest})
end
end
end
end
+1 -1
View File
@@ -2,6 +2,6 @@
module Qiniu
module RS
VERSION = "1.0.2"
VERSION = "2.0.2"
end
end
+10 -7
View File
@@ -5,8 +5,8 @@ require File.expand_path('../lib/qiniu/rs/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["why404"]
gem.email = ["why404@gmail.com"]
gem.description = %q{Qiniu Cloud Storage SDK for Ruby. See: http://docs.qiniutek.com/v1/sdk/ruby/}
gem.summary = %q{Qiniu Cloud Storage SDK for Ruby}
gem.description = %q{Qiniu Resource (Cloud) Storage SDK for Ruby. See: http://docs.qiniutek.com/v2/sdk/ruby/}
gem.summary = %q{Qiniu Resource (Cloud) Storage SDK for Ruby}
gem.homepage = "https://github.com/why404/qiniu-rs"
gem.files = `git ls-files`.split($\)
@@ -17,9 +17,12 @@ Gem::Specification.new do |gem|
gem.version = Qiniu::RS::VERSION
# specify any dependencies here; for example:
gem.add_development_dependency "rake"
gem.add_development_dependency "rspec"
gem.add_development_dependency "fakeweb"
gem.add_runtime_dependency "rest-client"
gem.add_runtime_dependency "mime-types"
gem.add_development_dependency "rake", "~> 0.9.2.2"
gem.add_development_dependency "rspec", "~> 2.10.0"
gem.add_development_dependency "fakeweb", "~> 1.3.0"
gem.add_runtime_dependency "json", "~> 1.7.3"
gem.add_runtime_dependency "rest-client", "~> 1.6.7"
gem.add_runtime_dependency "mime-types", "~> 1.19"
gem.add_runtime_dependency "ruby-hmac", "~> 0.4.0"
gem.add_runtime_dependency "jruby-openssl", "~> 0.7.7" if RUBY_PLATFORM == "java"
end
Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

+20 -9
View File
@@ -2,14 +2,15 @@
require 'spec_helper'
require 'qiniu/rs/auth'
require 'qiniu/rs/rs'
require 'qiniu/rs'
require 'qiniu/rs/image'
module Qiniu
module RS
describe Image do
before :all do
before :each do
=begin
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
code.should == 200
data.should be_an_instance_of(Hash)
@@ -17,20 +18,30 @@ module Qiniu
data["refresh_token"].should_not be_empty
data["refresh_token"].should_not be_empty
puts data.inspect
=end
@bucket = "test_images"
@key = "test_image.jpg"
code2, data2 = Qiniu::RS::RS.get(@bucket, @key)
code2.should == 200
data2["url"].should_not be_empty
puts data2.inspect
@key = "image_logo_for_test.png"
@download_url = data2["url"]
local_file = File.expand_path('../' + @key, __FILE__)
put_url = Qiniu::RS.put_auth(10)
put_url.should_not be_false
put_url.should_not be_empty
result = Qiniu::RS.upload :url => put_url,
:file => local_file,
:bucket => @bucket,
:key => @key,
:enable_crc32_check => true
result.should be_true
end
context ".info" do
it "should works" do
code, data = Qiniu::RS::Image.info(@download_url)
result = Qiniu::RS.get(@bucket, @key)
result["url"].should_not be_empty
puts result.inspect
code, data = Qiniu::RS::Image.info(result["url"])
code.should == 200
puts data.inspect
end
+2
View File
@@ -10,6 +10,7 @@ module Qiniu
describe IO do
before :all do
=begin
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
code.should == 200
data.should be_an_instance_of(Hash)
@@ -17,6 +18,7 @@ module Qiniu
data["refresh_token"].should_not be_empty
data["refresh_token"].should_not be_empty
puts data.inspect
=end
code2, data2 = Qiniu::RS::IO.put_auth()
code2.should == 200
+2
View File
@@ -11,6 +11,7 @@ module Qiniu
describe RS do
before :all do
=begin
code, data = Qiniu::RS::Auth.exchange_by_password!("test@qbox.net", "test")
code.should == 200
data.should be_an_instance_of(Hash)
@@ -18,6 +19,7 @@ module Qiniu
data["refresh_token"].should_not be_empty
data["refresh_token"].should_not be_empty
puts data.inspect
=end
code2, data2 = Qiniu::RS::IO.put_auth()
code2.should == 200
+8 -2
View File
@@ -12,12 +12,14 @@ module Qiniu
@domain = 'cdn.example.com'
end
=begin
context ".login!" do
it "should works" do
result = Qiniu::RS.login!("test@qbox.net", "test")
result.should be_true
end
end
=end
context ".put_auth" do
it "should works" do
@@ -34,7 +36,11 @@ module Qiniu
put_url.should_not be_false
put_url.should_not be_empty
puts put_url.inspect
result = Qiniu::RS.upload(put_url, __FILE__, @bucket, @key)
result = Qiniu::RS.upload :url => put_url,
:file => __FILE__,
:bucket => @bucket,
:key => @key,
:enable_crc32_check => true
result.should be_true
end
end
@@ -132,7 +138,7 @@ module Qiniu
context ".image_info" do
it "should works" do
data = Qiniu::RS.get("test_images", "test_image.jpg")
data = Qiniu::RS.get("test_images", "image_logo_for_test.png")
data.should_not be_false
data.should_not be_empty
puts data.inspect
+2 -2
View File
@@ -6,7 +6,7 @@ require 'rspec'
RSpec.configure do |config|
config.before :all do
Qiniu::RS.establish_connection! :client_id => "abcd0c7edcdf914228ed8aa7c6cee2f2bc6155e2",
:client_secret => "fc9ef8b171a74e197b17f85ba23799860ddf3b9c"
Qiniu::RS.establish_connection! :access_key => "3fPHl_SLkPXdioqI_A8_NGngPWVJhlDk2ktRjogH",
:secret_key => "bXTPMDJrVYRJUiSDRFtFYwycVD_mjXxYWrCYlDHy"
end
end