Skip to content

Commit

Permalink
1.set crc default value to enable;
Browse files Browse the repository at this point in the history
2.add annotation for Client initialize function;
3.it won't check crc when oss server doesn't support crc.
4.add crc check to get_object api

Change-Id: I69ccbf0b1629daf2a28a7bd3615dba95abb1a6fc
  • Loading branch information
mars-coder committed Nov 4, 2016
1 parent 81c9bc0 commit 677331c
Show file tree
Hide file tree
Showing 15 changed files with 455 additions and 42 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,10 +1,10 @@
language: ruby
cache: bundler
rvm:
- 1.9.3
- 2.0
# - 1.9.3
# - 2.0
- 2.1
- 2.2
# - 2.2
env:
global:
- secure: cC3aeKKrNLGuK60zEPKNPmRlE3m/VlzQLAXjD8nYjhbPVL+zjaylY87vq657gSFmDDGY3ISF0aR3rjFpSHeoVr6CiVZDnQIgoaC2FD1oDxkzy6/8tEQBJ0YORLzfCO63NJTKltVMvSsPWck4xE+qIsQLIpDjia9fHc2InLtkn9pTnuhMpHxjBIat6TgFPSiIC9YXXjCveudTy2o+9Is2nCmkdsr7lqEkifLIyXmGzyUp99hmQy0OZ2lF06OMH/RwWSDRBZryqMT+ju1qsv+/LkDRFwaqKfxqDdewhxj6eZdA7Q1YyB7ChT+uFRNaHkrWwPCC+JcZnHq8kt8qykUfN+Saq/txZ65qpQFp0rDUG929iaUUnfreearniqe9+u+7LnrFeseVLa6dJD+fcsTcCQRgfUvuaMARLq3EwqUCvAXPWHFTnY6ynqerTEwlJbxpCXtYktXaO0KugOI+iF4APs/r2iroAM/zkf3AjH78UcSAIBn1AzxQy7+Ah6G+nS69/nvbtPGiiSAmJYgKfBcD1ctDTJXzen78MGz7ImTC64k+2J4kuu8CSi/j8wbdIRHrIIszBUEPKQrGp9RDx08MRffinhnInq1uGEBFvip2Q1qk3tuGXJT+2m/OgbK7KlVmlzRz9M+NGav6ixQpW4qPvu2mf2bdmMeAd/0sg4IIIRc=
Expand Down
16 changes: 10 additions & 6 deletions Rakefile
Expand Up @@ -52,17 +52,21 @@ task :smart_test do

if ENV.keys.include?('RUBY_SDK_OSS_KEY')
begin
env_crc_enable = ENV['RUBY_SDK_OSS_CRC_ENABLE']
env_upload_crc_enable = ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE']
env_download_crc_enable = ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE']

# run test without crc
ENV['RUBY_SDK_OSS_CRC_ENABLE'] = nil if ENV['RUBY_SDK_OSS_CRC_ENABLE']
# run test with crc
ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'] = 'true'
ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'] = 'true'
Rake::Task[:test].invoke

# run test with crc
ENV['RUBY_SDK_OSS_CRC_ENABLE'] = 'true'
# run test without crc
ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'] = 'false'
ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'] = 'false'
Rake::Task[:test].invoke
ensure
ENV['RUBY_SDK_OSS_CRC_ENABLE'] = env_crc_enable
ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'] = env_upload_crc_enable
ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'] = env_download_crc_enable
end
end
end
Expand Down
14 changes: 10 additions & 4 deletions lib/aliyun/oss/bucket.rb
Expand Up @@ -639,10 +639,16 @@ def sign(string_to_sign)
@protocol.sign(string_to_sign)
end

# Get the crc status
# @return true(crc enable) or false(crc disable)
def crc_enable
@protocol.crc_enable
# Get the download crc status
# @return true(download crc enable) or false(download crc disable)
def download_crc_enable
@protocol.download_crc_enable
end

# Get the upload crc status
# @return true(upload crc enable) or false(upload crc disable)
def upload_crc_enable
@protocol.upload_crc_enable
end

private
Expand Down
4 changes: 4 additions & 0 deletions lib/aliyun/oss/client.rb
Expand Up @@ -28,6 +28,10 @@ class Client
# KEY SECRET,如果不填则会尝试匿名访问
# @option opts [Boolean] :cname [可选] 指定endpoint是否是用户绑
# 定的域名
# @option opts [Boolean] :upload_crc_enable [可选]指定上传处理
# 是否开启CRC校验,默认为开启(true)
# @option opts [Boolean] :download_crc_enable [可选]指定下载处理
# 是否开启CRC校验,默认为不开启(false)
# @option opts [String] :sts_token [可选] 指定STS的
# SecurityToken,如果指定,则使用STS授权访问
# @option opts [Fixnum] :open_timeout [可选] 指定建立连接的超时
Expand Down
5 changes: 3 additions & 2 deletions lib/aliyun/oss/config.rb
Expand Up @@ -12,15 +12,16 @@ class Config < Common::Struct::Base
attrs :endpoint, :cname, :sts_token,
:access_key_id, :access_key_secret,
:open_timeout, :read_timeout,
:crc_enable
:download_crc_enable, :upload_crc_enable

def initialize(opts = {})
super(opts)

@access_key_id = @access_key_id.strip if @access_key_id
@access_key_secret = @access_key_secret.strip if @access_key_secret
normalize_endpoint if endpoint
@crc_enable ||= false
@upload_crc_enable = (@upload_crc_enable == 'false' || @upload_crc_enable == false) ? false : true
@download_crc_enable = (@download_crc_enable == 'true' || @download_crc_enable == true) ? true : false
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/aliyun/oss/http.rb
Expand Up @@ -169,6 +169,7 @@ def handle_response(r, &block)
r.read_body
else
# streaming read body on success
p "debug: handle_response x_oss_request_id=#{r['x_oss_request_id']} content-encoding=#{r['content-encoding']}"
encoding = r['content-encoding']
if encoding == 'gzip'
stream = StreamWriter.new { |s| r.read_body { |chunk| s << chunk } }
Expand Down
43 changes: 31 additions & 12 deletions lib/aliyun/oss/protocol.rb
Expand Up @@ -535,7 +535,7 @@ def put_object(bucket_name, object_name, opts = {}, &block)
headers[CALLBACK_HEADER] = opts[:callback].serialize
end

payload = HTTP::StreamPayload.new(@config.crc_enable, opts[:init_crc], &block)
payload = HTTP::StreamPayload.new(@config.upload_crc_enable, opts[:init_crc], &block)
r = @http.put(
{:bucket => bucket_name, :object => object_name},
{:headers => headers, :body => payload})
Expand All @@ -546,7 +546,7 @@ def put_object(bucket_name, object_name, opts = {}, &block)
raise e
end

if @config.crc_enable
if @config.upload_crc_enable && !r.headers[:x_oss_hash_crc64ecma].nil?
data_crc = payload.read.data_crc
Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'put')
end
Expand Down Expand Up @@ -593,13 +593,15 @@ def append_object(bucket_name, object_name, position, opts = {}, &block)

headers.merge!(to_lower_case(opts[:headers])) if opts.key?(:headers)

payload = HTTP::StreamPayload.new(@config.crc_enable && !opts[:init_crc].nil?, opts[:init_crc], &block)
payload = HTTP::StreamPayload.new(@config.upload_crc_enable && !opts[:init_crc].nil?, opts[:init_crc], &block)

r = @http.post(
{:bucket => bucket_name, :object => object_name, :sub_res => sub_res},
{:headers => headers, :body => payload})

if @config.crc_enable && !opts[:init_crc].nil?
if @config.upload_crc_enable &&
!r.headers[:x_oss_hash_crc64ecma].nil? &&
!opts[:init_crc].nil?
data_crc = payload.read.data_crc
Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'append')
end
Expand Down Expand Up @@ -773,11 +775,22 @@ def get_object(bucket_name, object_name, opts = {}, &block)
rewrites[:expires].httpdate if rewrites.key?(:expires)
end

data_crc = opts[:init_crc].nil? ? 0 : opts[:init_crc]
r = @http.get(
{:bucket => bucket_name, :object => object_name,
:sub_res => sub_res},
{:headers => headers}
) { |chunk| yield chunk if block_given? }
) do |chunk|
if block_given?
# crc enable and no range and oss server support crc
data_crc = Aliyun::OSS::Util.crc(chunk, data_crc) if @config.download_crc_enable && range.nil?
yield chunk
end
end

if @config.download_crc_enable && range.nil? && !r.headers[:x_oss_hash_crc64ecma].nil?
Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'get')
end

h = r.headers
metas = {}
Expand Down Expand Up @@ -1097,12 +1110,12 @@ def upload_part(bucket_name, object_name, txn_id, part_no, &block)

sub_res = {'partNumber' => part_no, 'uploadId' => txn_id}

payload = HTTP::StreamPayload.new(@config.crc_enable, &block)
payload = HTTP::StreamPayload.new(@config.upload_crc_enable, &block)
r = @http.put(
{:bucket => bucket_name, :object => object_name, :sub_res => sub_res},
{:body => payload})

if @config.crc_enable
if @config.upload_crc_enable && !r.headers[:x_oss_hash_crc64ecma].nil?
data_crc = payload.read.data_crc
Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'put')
end
Expand Down Expand Up @@ -1398,11 +1411,17 @@ def get_sts_token
def sign(string_to_sign)
Util.sign(@config.access_key_secret, string_to_sign)
end

# Get the crc status
# @return true(crc enable) or false(crc disable)
def crc_enable
@config.crc_enable

# Get the download crc status
# @return true(download crc enable) or false(download crc disable)
def download_crc_enable
@config.download_crc_enable
end

# Get the upload crc status
# @return true(upload crc enable) or false(upload crc disable)
def upload_crc_enable
@config.upload_crc_enable
end

private
Expand Down
54 changes: 54 additions & 0 deletions spec/aliyun/oss/bucket_spec.rb
Expand Up @@ -591,6 +591,60 @@ def err(msg, reqid = '0000')

end # acl, logging, cors, etc

context "crc" do
it "should download crc enable equal config setting" do
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => 'true'))
expect(protocol.download_crc_enable).to eq(true)

protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => true))
expect(protocol.download_crc_enable).to eq(true)

protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => 'false'))
expect(protocol.download_crc_enable).to eq(false)

protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => false))
expect(protocol.download_crc_enable).to eq(false)
end

it "should upload crc enable equal config setting" do
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => 'true'))
expect(protocol.upload_crc_enable).to eq(true)

protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => true))
expect(protocol.upload_crc_enable).to eq(true)

protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => 'false'))
expect(protocol.upload_crc_enable).to eq(false)

protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => false))
expect(protocol.upload_crc_enable).to eq(false)
end
end # crc

end # Bucket

end # OSS
Expand Down
66 changes: 66 additions & 0 deletions spec/aliyun/oss/client/bucket_spec.rb
Expand Up @@ -549,6 +549,72 @@ def err(msg, reqid = '0000')
end
end # multipart operations

context "crc" do
it "should download crc enable equal config setting" do
bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => 'true').get_bucket(@bucket_name)
expect(bucket.download_crc_enable).to eq(true)

bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => true).get_bucket(@bucket_name)
expect(bucket.download_crc_enable).to eq(true)

bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => 'false').get_bucket(@bucket_name)
expect(bucket.download_crc_enable).to eq(false)

bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => false).get_bucket(@bucket_name)
expect(bucket.download_crc_enable).to eq(false)

# check default value
bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy').get_bucket(@bucket_name)
expect(bucket.download_crc_enable).to eq(false)
end

it "should upload crc enable equal config setting" do
bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => 'true').get_bucket(@bucket_name)
expect(bucket.upload_crc_enable).to eq(true)

bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => true).get_bucket(@bucket_name)
expect(bucket.upload_crc_enable).to eq(true)

bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => 'false').get_bucket(@bucket_name)
expect(bucket.upload_crc_enable).to eq(false)

bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => false).get_bucket(@bucket_name)
expect(bucket.upload_crc_enable).to eq(false)

# check default value
bucket = Client.new(
:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy').get_bucket(@bucket_name)
expect(bucket.upload_crc_enable).to eq(true)
end
end # crc

end # Bucket
end # OSS
end # Aliyun
3 changes: 2 additions & 1 deletion spec/aliyun/oss/multipart_spec.rb
Expand Up @@ -28,7 +28,8 @@ def crc_protocol
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx',
:access_key_secret => 'yyy',
:crc_enable => true))
:upload_crc_enable => true,
:download_crc_enable => true))
end

def mock_txn_id(txn_id)
Expand Down

0 comments on commit 677331c

Please sign in to comment.