mirror of
https://github.com/wahyd4/ruby-sdk.git
synced 2026-08-10 05:18:37 +10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5dc205ab46 | ||
|
|
87da8ed256 | ||
|
|
d9738e4190 | ||
|
|
1e08f5bb17 | ||
|
|
38326cbb83 | ||
|
|
853c3b537d | ||
|
|
285b766819 | ||
|
|
e54081dd88 | ||
|
|
a0657ee26f | ||
|
|
0fa0839a70 |
@@ -5,7 +5,6 @@
|
||||
.bundle
|
||||
.config
|
||||
.yardoc
|
||||
Gemfile.lock
|
||||
InstalledFiles
|
||||
_yardoc
|
||||
coverage
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
+9
-2
@@ -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
@@ -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, {}
|
||||
|
||||
@@ -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
@@ -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
@@ -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
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
|
||||
module Qiniu
|
||||
module RS
|
||||
VERSION = "1.0.3"
|
||||
VERSION = "2.0.1"
|
||||
end
|
||||
end
|
||||
|
||||
+10
-8
@@ -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,10 +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 "json"
|
||||
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 |
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user