baidubce-sdk 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fcf25bb36811b26a3d31ee13e740ddab94214d91
4
- data.tar.gz: 56d9ab32198c87d3121ef9aec564047a24e157d6
3
+ metadata.gz: 37ee3d088dfe300d2c4bc59ede9c3295460349d7
4
+ data.tar.gz: 9c7fcf154a0f0a51b6b4a731e1ab2193e8247f04
5
5
  SHA512:
6
- metadata.gz: 6b4caffe3a7384402d7ea0fcf203c638aaee588d5ab4e5a2b21f168babcf792c9e7ccaaef9c055612df048261662296a8a4739da016594e2a61da216589e45fb
7
- data.tar.gz: 79e634982aab61ba9f75b8bda7af86d5b2d5a4cde6c28b8edfd502a887726b2bb7ea1e30a0b1e2156be9b3b71e030b1ef65756c7c86ff0f1bda60b70f7a7e3b9
6
+ metadata.gz: 7eaa2b1a43bab2af6d5355cb24bf37d2ada15a10db184364a65f1c6ddab3f5a58ef1dddae0851c08f400f9c163b5c39a33058a0805c7582f9e865cb11490eba7
7
+ data.tar.gz: 70c5c1e51449b504d1a8a05b21dd1f5e2fb3f7831ce2713f48a83f4633814cb056d74384328229534bd8d0563933a3dda94f2a79d984218fca1b68df0d89a10d
data/.gitignore CHANGED
@@ -1,12 +1,2 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
-
11
- # rspec failure tracking
12
- .rspec_status
1
+ output
2
+ .svn
@@ -25,7 +25,8 @@ module Baidubce
25
25
 
26
26
  class BceClientConfiguration
27
27
  attr_accessor :credentials, :endpoint, :protocol, :region, :open_timeout_in_millis,
28
- :read_timeout_in_millis, :send_buf_size, :recv_buf_size, :retry_policy
28
+ :read_timeout_in_millis, :send_buf_size, :recv_buf_size, :retry_policy,
29
+ :cname_enabled, :backup_endpoint
29
30
 
30
31
  def initialize(credentials,
31
32
  endpoint,
@@ -41,6 +42,8 @@ module Baidubce
41
42
  @send_buf_size = options['send_buf_size'] || DEFAULT_SEND_BUF_SIZE
42
43
  @recv_buf_size = options['recv_buf_size'] || DEFAULT_RECV_BUF_SIZE
43
44
  @retry_policy = options['retry_policy'] || BackOffRetryPolicy.new
45
+ @cname_enabled = options['cname_enabled'] || false
46
+ @backup_endpoint = options['backup_endpoint']
44
47
  end
45
48
 
46
49
  end
@@ -17,4 +17,5 @@ module Baidubce
17
17
  DEFAULT_SERVICE_DOMAIN = 'baidubce.com'
18
18
  DEFAULT_ENCODING = 'UTF-8'
19
19
 
20
+ SOCKET_ERROR_CODE = -100
20
21
  end
@@ -32,7 +32,8 @@ module Baidubce
32
32
 
33
33
  include Log
34
34
  # Send request to BCE services.
35
- def send_request(config, signer, http_method, path, params, headers, body, save_path=nil, &block)
35
+ def send_request(config, signer, http_method, path, params, headers, body,
36
+ save_path=nil, use_backup_endpoint=false, &block)
36
37
  headers[USER_AGENT] = sprintf(
37
38
  'bce-sdk-ruby/%s/%s/%s',
38
39
  VERSION,
@@ -42,7 +43,7 @@ module Baidubce
42
43
 
43
44
  should_get_new_date = headers.has_key?(BCE_DATE) ? false : true
44
45
 
45
- url, headers[HOST] = Utils.parse_url_host(config)
46
+ url, headers[HOST] = Utils.parse_url_host(config, use_backup_endpoint)
46
47
  url += Utils.url_encode_except_slash(path)
47
48
  query_str = Utils.get_canonical_querystring(params, false)
48
49
  url += "?#{query_str}" unless query_str.to_s.empty?
@@ -90,6 +91,13 @@ module Baidubce
90
91
  logger.debug("Response headers: #{resp.headers.to_s}")
91
92
  return resp.body, resp.headers
92
93
  end
94
+ rescue SocketError => err
95
+ if config.retry_policy.should_retry(SOCKET_ERROR_CODE, retries_attempted)
96
+ delay_in_millis = config.retry_policy.get_delay_before_next_retry_in_millis(retries_attempted)
97
+ sleep(delay_in_millis / 1000.0)
98
+ else
99
+ raise BceClientException.new("SocketError: #{err}")
100
+ end
93
101
  rescue BceHttpException, RestClient::ExceptionWithResponse => err
94
102
  logger.debug("ExceptionWithResponse: #{err.http_code}, #{err.http_body}, #{err.http_headers}, #{err.message}")
95
103
  if config.retry_policy.should_retry(err.http_code, retries_attempted)
@@ -117,7 +125,8 @@ module Baidubce
117
125
  def set_content_length_header(headers, body, &block)
118
126
  unless block_given?
119
127
  if body.to_s.empty?
120
- headers[CONTENT_LENGTH] = 0
128
+ # headers[CONTENT_LENGTH] = 0
129
+ headers.delete(CONTENT_LENGTH)
121
130
  elsif body.instance_of?(String)
122
131
  body = body.encode('UTF-8') if body.encoding.name != 'UTF-8'
123
132
  headers[CONTENT_LENGTH] = body.bytesize
@@ -98,5 +98,6 @@ module Baidubce
98
98
  # STS HTTP Headers
99
99
 
100
100
  STS_SECURITY_TOKEN = "x-bce-security-token"
101
+
101
102
  end
102
103
  end
@@ -68,6 +68,12 @@ module Baidubce
68
68
  logger.debug('Retry for request timeout.')
69
69
  return true
70
70
  end
71
+ # retry for SocketError
72
+ if http_code == SOCKET_ERROR_CODE
73
+ logger.debug('Retry for request SocketError.')
74
+ return true
75
+ end
76
+
71
77
  if http_code >= 500 && http_code != 501
72
78
  logger.debug('Retry for server error.')
73
79
  return true
@@ -82,6 +88,7 @@ module Baidubce
82
88
  return @max_delay_in_millis if delay_in_millis > @max_delay_in_millis
83
89
  return delay_in_millis
84
90
  end
91
+
85
92
  end
86
93
 
87
94
  end
@@ -251,8 +251,10 @@ module Baidubce
251
251
 
252
252
  path = Utils.append_uri("/", key)
253
253
  url, headers[HOST] = Utils.parse_url_host(@config)
254
- url.insert(url.index('/') + 2, bucket_name + '.')
255
- headers[HOST] = bucket_name + '.' + headers[HOST]
254
+ unless @config.cname_enabled || Utils.is_cname_like_host(@config.endpoint)
255
+ url.insert(url.index('/') + 2, bucket_name + '.')
256
+ headers[HOST] = bucket_name + '.' + headers[HOST]
257
+ end
256
258
  params[AUTHORIZATION.downcase] = @signer.sign(@config.credentials,
257
259
  GET,
258
260
  path,
@@ -421,10 +423,38 @@ module Baidubce
421
423
  end
422
424
 
423
425
  def send_request(http_method, bucket_name="", params={}, key="", headers={}, body="", save_path=nil, return_body=false, &block)
424
- path = Utils.append_uri("/", bucket_name, key)
425
- body, headers = @http_client.send_request(@config, @signer, http_method, path, params, headers, body, save_path, &block)
426
+ if @config.cname_enabled || Utils.is_cname_like_host(@config.endpoint)
427
+ path = Utils.append_uri("/", key)
428
+ else
429
+ path = Utils.append_uri("/", bucket_name, key)
430
+ end
431
+
432
+ begin
433
+ resp_body, resp_headers = @http_client.send_request(@config, @signer, http_method, path, params,
434
+ headers, body, save_path, false, &block)
435
+ rescue BceClientException, BceServerException => err
436
+ # retry backup endpoint
437
+ if err.is_a?(BceClientException) || (err.is_a?(BceServerException) && (err.status_code.nil? ||
438
+ err.status_code == 408 || (err.status_code >= 500 && err.status_code != 501)))
439
+ unless @config.backup_endpoint.to_s.empty?
440
+ if @config.cname_enabled || Utils.is_cname_like_host(@config.backup_endpoint)
441
+ path = Utils.append_uri("/", key)
442
+ else
443
+ path = Utils.append_uri("/", bucket_name, key)
444
+ end
445
+
446
+ resp_body, resp_headers = @http_client.send_request(@config, @signer, http_method, path, params,
447
+ headers, body, save_path, true, &block)
448
+ else
449
+ raise err
450
+ end
451
+ else
452
+ raise err
453
+ end
454
+ end
455
+
426
456
  # Generate result from headers and body
427
- Utils.generate_response(headers, body, return_body)
457
+ Utils.generate_response(resp_headers, resp_body, return_body)
428
458
  end
429
459
 
430
460
  def get_range_header_dict(range)
@@ -23,9 +23,14 @@ module Baidubce
23
23
 
24
24
  class Utils
25
25
 
26
+ DEFAULT_CNAME_LIKE_LIST = [".cdn.bcebos.com"]
27
+
26
28
  # parse protocol, host, port from endpoint in config.
27
- def self.parse_url_host(config)
29
+ def self.parse_url_host(config, use_backup_endpoint=false)
28
30
  endpoint = config.endpoint
31
+ if use_backup_endpoint
32
+ endpoint = config.backup_endpoint
33
+ end
29
34
  unless endpoint.include?"://"
30
35
  protocol = config.protocol.downcase
31
36
  raise "Invalid protocol #{protocol}." if protocol != "http" && protocol != 'https'
@@ -91,7 +96,7 @@ module Baidubce
91
96
 
92
97
  def self.generate_response(headers, body, return_body)
93
98
  return body if return_body
94
- return generate_headers(headers) if body.empty?
99
+ return generate_headers(headers) if body.to_s.empty?
95
100
  ret = JSON.parse(body)
96
101
  return ret
97
102
  rescue JSON::ParserError
@@ -119,6 +124,13 @@ module Baidubce
119
124
  resp_headers
120
125
  end
121
126
 
127
+ def self.is_cname_like_host(host)
128
+ DEFAULT_CNAME_LIKE_LIST.each do |suffix|
129
+ return true if host.to_s.downcase.end_with?(suffix)
130
+ end
131
+ return false
132
+ end
133
+
122
134
  end
123
135
 
124
136
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Baidubce
4
4
 
5
- VERSION = "0.9.0"
5
+ VERSION = "0.9.1"
6
6
 
7
7
  end
@@ -274,7 +274,7 @@ demo "set/get/delete object acl" do
274
274
  client.delete_object_acl(bucket_name, key)
275
275
  end
276
276
 
277
- # create a 18MB file for multi upload
277
+ # create a 18MB file for multi upload.
278
278
  multi_file = "multi_upload.txt"
279
279
 
280
280
  demo "multi-upload" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baidubce-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - xiaoyong
8
8
  autorequire:
9
9
  bindir: lib/baidubce
10
10
  cert_chain: []
11
- date: 2018-05-22 00:00:00.000000000 Z
11
+ date: 2020-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -120,8 +120,6 @@ extensions: []
120
120
  extra_rdoc_files: []
121
121
  files:
122
122
  - ".gitignore"
123
- - ".rspec"
124
- - ".travis.yml"
125
123
  - CODE_OF_CONDUCT.md
126
124
  - Gemfile
127
125
  - LICENSE
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.0.0
5
- before_install: gem install bundler -v 1.15.4