Class: Baidubce::Services::BosClient
- Inherits:
-
BceBaseClient
- Object
- BceBaseClient
- Baidubce::Services::BosClient
- Defined in:
- lib/baidubce/services/bos/bos_client.rb
Constant Summary
Constants included from Http
Http::AUTHORIZATION, Http::BCE_ACL, Http::BCE_CONTENT_CRC32, Http::BCE_CONTENT_SHA256, Http::BCE_COPY_METADATA_DIRECTIVE, Http::BCE_COPY_SOURCE, Http::BCE_COPY_SOURCE_IF_MATCH, Http::BCE_COPY_SOURCE_IF_MODIFIED_SINCE, Http::BCE_COPY_SOURCE_IF_NONE_MATCH, Http::BCE_COPY_SOURCE_IF_UNMODIFIED_SINCE, Http::BCE_COPY_SOURCE_RANGE, Http::BCE_DATE, Http::BCE_PREFIX, Http::BCE_REQUEST_ID, Http::BCE_USER_METADATA_PREFIX, Http::BOS_DEBUG_ID, Http::BOS_STORAGE_CLASS, Http::CACHE_CONTROL, Http::CONTENT_DISPOSITION, Http::CONTENT_ENCODING, Http::CONTENT_LENGTH, Http::CONTENT_MD5, Http::CONTENT_RANGE, Http::CONTENT_TYPE, Http::DATE, Http::DELETE, Http::ETAG, Http::EXPIRES, Http::GET, Http::HEAD, Http::HOST, Http::JSON_TYPE, Http::LAST_MODIFIED, Http::OCTET_STREAM_TYPE, Http::POST, Http::PUT, Http::RANGE, Http::SERVER, Http::STS_SECURITY_TOKEN, Http::USER_AGENT
Instance Method Summary collapse
-
#abort_multipart_upload(bucket_name, key, upload_id) ⇒ Object
Abort upload a part which is being uploading.
-
#append_object(bucket_name, key, data, offset, content_md5, content_length, options = {}) ⇒ Object
Put an appendable object to BOS or add content to an appendable object.
-
#append_object_from_string(bucket_name, key, data, options = {}) ⇒ Object
Create an appendable object and put content of string to the object or add content of string to an appendable object.
-
#complete_multipart_upload(bucket_name, key, upload_id, part_list, options = {}) ⇒ Object
After finish all the task, complete multi_upload_file.
-
#copy_object(source_bucket_name, source_key, target_bucket_name, target_key, options = {}) ⇒ Object
Copy one object to another object.
-
#create_bucket(bucket_name) ⇒ Object
Create bucket with specific name.
-
#delete_bucket(bucket_name) ⇒ Object
Delete bucket with specific name.
-
#delete_bucket_cors(bucket_name) ⇒ Object
Delete Bucket Cors.
-
#delete_bucket_lifecycle(bucket_name) ⇒ Object
Delete Bucket Lifecycle.
-
#delete_bucket_logging(bucket_name) ⇒ Object
Delete Bucket Logging.
-
#delete_multiple_objects(bucket_name, key_list) ⇒ Object
Delete Multiple Objects.
-
#delete_object(bucket_name, key) ⇒ Object
Delete Object.
-
#delete_object_acl(bucket_name, key) ⇒ Object
Delete object acl.
-
#does_bucket_exist(bucket_name) ⇒ Object
Check whether there is a bucket with specific name.
- #gen_host(bucket_name, is_bucket_api) ⇒ Object
-
#generate_pre_signed_url(bucket_name, key, options = {}) ⇒ Object
Get an authorization url with expire time.
-
#get_bucket_acl(bucket_name) ⇒ Object
Get Access Control Level of bucket.
-
#get_bucket_cors(bucket_name) ⇒ Object
Get Bucket Cors.
-
#get_bucket_lifecycle(bucket_name) ⇒ Object
Gut Bucket Lifecycle.
-
#get_bucket_location(bucket_name) ⇒ Object
Get the region which the bucket located in.
-
#get_bucket_logging(bucket_name) ⇒ Object
Get Bucket Logging.
-
#get_bucket_storageclass(bucket_name) ⇒ Object
Get Bucket Storageclass.
- #get_object(bucket_name, key, range, save_path = nil, return_body = true) ⇒ Object
-
#get_object_acl(bucket_name, key) ⇒ Object
Get object acl.
-
#get_object_as_string(bucket_name, key, range = nil) ⇒ Object
Get Content of Object and Put Content to String.
-
#get_object_meta_data(bucket_name, key) ⇒ Object
Get meta of object.
-
#get_object_to_file(bucket_name, key, save_path, range = nil) ⇒ Object
Get Content of Object and Put Content to File.
- #get_range_header_dict(range) ⇒ Object
-
#initiate_multipart_upload(bucket_name, key, options = {}) ⇒ Object
Initialize multi_upload_file.
-
#list_buckets ⇒ Object
List buckets of user.
-
#list_multipart_uploads(bucket_name, options = {}) ⇒ Object
List all Multipart upload task which haven’t been ended.(Completed Init_MultiPartUpload but not completed Complete_MultiPartUpload or Abort_MultiPartUpload).
-
#list_objects(bucket_name, options = {}) ⇒ Object
Get Object Information of bucket.
-
#list_parts(bucket_name, key, upload_id, options = {}) ⇒ Object
List all the parts that have been upload success.
- #populate_headers_with_user_metadata(headers) ⇒ Object
-
#put_bucket_cors(bucket_name, cors_configuration) ⇒ Object
Put Bucket Cors.
-
#put_bucket_lifecycle(bucket_name, rules) ⇒ Object
Put Bucket Lifecycle.
-
#put_bucket_logging(source_bucket, target_bucket, target_prefix = "") ⇒ Object
Put Bucket Logging.
-
#put_bucket_storageclass(bucket_name, storage_class) ⇒ Object
Put Bucket Storageclass.
-
#put_object(bucket_name, key, data, content_md5, content_length, options, &block) ⇒ Object
Put object to BOS.
-
#put_object_from_file(bucket_name, key, file_name, options = {}) ⇒ Object
Put object and put content of file to the object.
-
#put_object_from_string(bucket_name, key, data, options = {}) ⇒ Object
Create object and put content of string to the object.
- #send_request(http_method, bucket_name = "", params = {}, key = "", headers = {}, body = "", save_path = nil, return_body = false, is_bucket_api = false, &block) ⇒ Object
-
#set_bucket_acl(bucket_name, acl) ⇒ Object
Set Access Control Level of bucket by body.
-
#set_bucket_canned_acl(bucket_name, canned_acl) ⇒ Object
Set Access Control Level of bucket by headers.
-
#set_object_acl(bucket_name, key, acl) ⇒ Object
Set object acl by body.
-
#set_object_canned_acl(bucket_name, key, canned_acl = {}) ⇒ Object
Set object acl by headers.
-
#upload_part(bucket_name, key, upload_id, part_number, part_size, options = {}, &block) ⇒ Object
Upload a part.
-
#upload_part_copy(source_bucket_name, source_key, target_bucket_name, target_key, upload_id, part_number, part_size, offset, options = {}) ⇒ Object
Copy part.
-
#upload_part_from_file(bucket_name, key, upload_id, part_number, part_size, file_name, offset = 0, options = {}) ⇒ Object
Upload a part from file.
Methods inherited from BceBaseClient
#compute_endpoint, #initialize
Constructor Details
This class inherits a constructor from Baidubce::BceBaseClient
Instance Method Details
#abort_multipart_upload(bucket_name, key, upload_id) ⇒ Object
Abort upload a part which is being uploading.
378 379 380 381 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 378 def abort_multipart_upload(bucket_name, key, upload_id) params = { uploadId: upload_id } send_request(DELETE, bucket_name, params, key) end |
#append_object(bucket_name, key, data, offset, content_md5, content_length, options = {}) ⇒ Object
Put an appendable object to BOS or add content to an appendable object.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 178 def append_object(bucket_name, key, data, offset, content_md5, content_length, ={}) if content_length > MAX_APPEND_OBJECT_LENGTH raise BceClientException.new("Object length should be less than #{MAX_APPEND_OBJECT_LENGTH}. Use multi-part upload instead.") end params = { append: "" } params[:offset] = offset unless offset.nil? headers = { CONTENT_MD5 => content_md5, CONTENT_LENGTH => content_length, } headers.merge! (headers) unless headers['user-metadata'].nil? send_request(POST, bucket_name, params, key, headers, data) end |
#append_object_from_string(bucket_name, key, data, options = {}) ⇒ Object
Create an appendable object and put content of string to the object or add content of string to an appendable object.
195 196 197 198 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 195 def append_object_from_string(bucket_name, key, data, ={}) data_md5 = Digest::MD5.base64digest(data) append_object(bucket_name, key, data, ['offset'], data_md5, data.bytesize, ) end |
#complete_multipart_upload(bucket_name, key, upload_id, part_list, options = {}) ⇒ Object
After finish all the task, complete multi_upload_file.
367 368 369 370 371 372 373 374 375 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 367 def complete_multipart_upload(bucket_name, key,upload_id, part_list, ={}) headers = params = { uploadId: upload_id } (headers) unless headers['user-metadata'].nil? part_list.each { |part| part['eTag'].gsub!("\"", "") } body = { parts: part_list }.to_json send_request(POST, bucket_name, params, key, headers, body) end |
#copy_object(source_bucket_name, source_key, target_bucket_name, target_key, options = {}) ⇒ Object
Copy one object to another object.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 277 def copy_object(source_bucket_name, source_key, target_bucket_name, target_key, ={}) headers = headers[BCE_COPY_SOURCE_IF_MATCH] = headers['etag'] unless headers['etag'].nil? if headers['user-metadata'].nil? headers[BCE_COPY_METADATA_DIRECTIVE] = 'copy' else headers[BCE_COPY_METADATA_DIRECTIVE] = 'replace' (headers) end headers[BCE_COPY_SOURCE] = Utils.url_encode_except_slash("/#{source_bucket_name}/#{source_key}") send_request(PUT, target_bucket_name, {}, target_key, headers) end |
#create_bucket(bucket_name) ⇒ Object
Create bucket with specific name.
31 32 33 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 31 def create_bucket(bucket_name) send_request(PUT, bucket_name) end |
#delete_bucket(bucket_name) ⇒ Object
Delete bucket with specific name.
36 37 38 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 36 def delete_bucket(bucket_name) send_request(DELETE, bucket_name) end |
#delete_bucket_cors(bucket_name) ⇒ Object
Delete Bucket Cors.
130 131 132 133 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 130 def delete_bucket_cors(bucket_name) params = { cors: "" } send_request(DELETE, bucket_name, params) end |
#delete_bucket_lifecycle(bucket_name) ⇒ Object
Delete Bucket Lifecycle.
95 96 97 98 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 95 def delete_bucket_lifecycle(bucket_name) params = { lifecycle: "" } send_request(DELETE, bucket_name, params) end |
#delete_bucket_logging(bucket_name) ⇒ Object
Delete Bucket Logging.
150 151 152 153 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 150 def delete_bucket_logging(bucket_name) params = { logging: "" } send_request(DELETE, bucket_name, params) end |
#delete_multiple_objects(bucket_name, key_list) ⇒ Object
Delete Multiple Objects.
299 300 301 302 303 304 305 306 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 299 def delete_multiple_objects(bucket_name, key_list) params = { delete: "" } key_arr = [] key_list.each { |item| key_arr << { key: item } } body = { objects: key_arr }.to_json ret = send_request(POST, bucket_name, params, "", {}, body, nil, true) return ret.empty? ? {} : JSON.parse(ret) end |
#delete_object(bucket_name, key) ⇒ Object
Delete Object.
294 295 296 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 294 def delete_object(bucket_name, key) send_request(DELETE, bucket_name, {}, key) end |
#delete_object_acl(bucket_name, key) ⇒ Object
Delete object acl.
419 420 421 422 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 419 def delete_object_acl(bucket_name, key) params = { acl: "" } send_request(DELETE, bucket_name, params, key) end |
#does_bucket_exist(bucket_name) ⇒ Object
Check whether there is a bucket with specific name.
41 42 43 44 45 46 47 48 49 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 41 def does_bucket_exist(bucket_name) begin send_request(HEAD, bucket_name) rescue BceServerException => e return false if e.status_code == 404 return true if e.status_code == 403 end true end |
#gen_host(bucket_name, is_bucket_api) ⇒ Object
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 424 def gen_host(bucket_name, is_bucket_api) endpoint = @config.endpoint unless endpoint.include?"://" protocol = @config.protocol.downcase raise "Invalid protocol #{protocol}." if protocol != "http" && protocol != 'https' endpoint = sprintf("%s://%s", protocol, endpoint) end parsed_endpoint = URI.parse(endpoint) scheme = parsed_endpoint.scheme.downcase host = parsed_endpoint.host parts = host.split('.') if is_bucket_api == true @config.cname_enabled = false if host.include?(DEFAULT_BOS_DOMAIN) last_three_elements = parts.last(3) host = last_three_elements.join('.') end else if @config.cname_enabled == false @config.cname_enabled = true if parts.length == 3 host = [bucket_name, parts[0], parts[1], parts[2]].join('.') end end end port = parsed_endpoint.port host += ":#{port}" unless scheme == 'http' && port == 80 || scheme == 'https' && port == 443 @config.endpoint = host end |
#generate_pre_signed_url(bucket_name, key, options = {}) ⇒ Object
Get an authorization url with expire time.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 247 def generate_pre_signed_url(bucket_name, key, ={}) headers = ['headers'].nil? ? {} : ['headers'] params = ['params'].nil? ? {} : ['params'] gen_host(bucket_name, false) path = Utils.append_uri("/", key) url, headers[HOST] = Utils.parse_url_host(@config) unless @config.cname_enabled || Utils.is_cname_like_host(@config.endpoint) url.insert(url.index('/') + 2, bucket_name + '.') headers[HOST] = bucket_name + '.' + headers[HOST] end params[AUTHORIZATION.downcase] = @signer.sign(@config.credentials, GET, path, headers, params, ['timestamp'], ['expiration_in_seconds'] || 1800, ['headers_to_sign']) url += Utils.url_encode_except_slash(path) query_str = Utils.get_canonical_querystring(params, false) url += "?#{query_str}" unless query_str.to_s.empty? url end |
#get_bucket_acl(bucket_name) ⇒ Object
Get Access Control Level of bucket.
60 61 62 63 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 60 def get_bucket_acl(bucket_name) params = { acl: "" } send_request(GET, bucket_name, params) end |
#get_bucket_cors(bucket_name) ⇒ Object
Get Bucket Cors.
124 125 126 127 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 124 def get_bucket_cors(bucket_name) params = { cors: "" } send_request(GET, bucket_name, params) end |
#get_bucket_lifecycle(bucket_name) ⇒ Object
Gut Bucket Lifecycle.
89 90 91 92 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 89 def get_bucket_lifecycle(bucket_name) params = { lifecycle: "" } send_request(GET, bucket_name, params) end |
#get_bucket_location(bucket_name) ⇒ Object
Get the region which the bucket located in. returns region of the bucket(bj/gz/sz).
53 54 55 56 57 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 53 def get_bucket_location(bucket_name) params = { location: "" } resp = send_request(GET, bucket_name, params) resp['locationConstraint'] end |
#get_bucket_logging(bucket_name) ⇒ Object
Get Bucket Logging.
144 145 146 147 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 144 def get_bucket_logging(bucket_name) params = { logging: "" } send_request(GET, bucket_name, params) end |
#get_bucket_storageclass(bucket_name) ⇒ Object
Get Bucket Storageclass.
109 110 111 112 113 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 109 def get_bucket_storageclass(bucket_name) params = { storageClass: "" } resp = send_request(GET, bucket_name, params) resp['storageClass'] end |
#get_object(bucket_name, key, range, save_path = nil, return_body = true) ⇒ Object
162 163 164 165 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 162 def get_object(bucket_name, key, range, save_path=nil, return_body=true) headers = range.nil? ? {} : get_range_header_dict(range) send_request(GET, bucket_name, {}, key, headers, "", save_path, return_body) end |
#get_object_acl(bucket_name, key) ⇒ Object
Get object acl.
399 400 401 402 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 399 def get_object_acl(bucket_name, key) params = { acl: "" } send_request(GET, bucket_name, params, key) end |
#get_object_as_string(bucket_name, key, range = nil) ⇒ Object
Get Content of Object and Put Content to String.
168 169 170 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 168 def get_object_as_string(bucket_name, key, range=nil) get_object(bucket_name, key, range) end |
#get_object_meta_data(bucket_name, key) ⇒ Object
Get meta of object.
272 273 274 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 272 def (bucket_name, key) send_request(HEAD, bucket_name, {}, key) end |
#get_object_to_file(bucket_name, key, save_path, range = nil) ⇒ Object
Get Content of Object and Put Content to File.
173 174 175 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 173 def get_object_to_file(bucket_name, key, save_path, range=nil) get_object(bucket_name, key, range, save_path, false) end |
#get_range_header_dict(range) ⇒ Object
491 492 493 494 495 496 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 491 def get_range_header_dict(range) raise BceClientException.new("range type should be a array") unless range.is_a? Array raise BceClientException.new("range should have length of 2") unless range.length == 2 raise BceClientException.new("range all element should be integer") unless range.all? { |i| i.is_a?(Integer) } { RANGE => "bytes=#{range[0]}-#{range[1]}" } end |
#initiate_multipart_upload(bucket_name, key, options = {}) ⇒ Object
Initialize multi_upload_file.
309 310 311 312 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 309 def initiate_multipart_upload(bucket_name, key, ={}) params = { uploads: "" } send_request(POST, bucket_name, params, key, ) end |
#list_buckets ⇒ Object
List buckets of user. returns all buckets owned by the user.
26 27 28 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 26 def list_buckets() send_request(GET, "", {}, "", {}, "", nil, false, true) end |
#list_multipart_uploads(bucket_name, options = {}) ⇒ Object
List all Multipart upload task which haven’t been ended.(Completed Init_MultiPartUpload but not completed Complete_MultiPartUpload or Abort_MultiPartUpload).
392 393 394 395 396 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 392 def list_multipart_uploads(bucket_name, ={}) params = { uploads: "" } params.merge! send_request(GET, bucket_name, params) end |
#list_objects(bucket_name, options = {}) ⇒ Object
Get Object Information of bucket.
156 157 158 159 160 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 156 def list_objects(bucket_name, ={}) params = { maxKeys: 1000 } params.merge! send_request(GET, bucket_name, params) end |
#list_parts(bucket_name, key, upload_id, options = {}) ⇒ Object
List all the parts that have been upload success.
384 385 386 387 388 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 384 def list_parts(bucket_name, key, upload_id, ={}) params = { uploadId: upload_id } params.merge! send_request(GET, bucket_name, params, key) end |
#populate_headers_with_user_metadata(headers) ⇒ Object
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 498 def (headers) = 0 = headers['user-metadata'] raise BceClientException.new("user_metadata should be of type hash.") unless .is_a? Hash .each do |k, v| k = k.encode(DEFAULT_ENCODING) v = v.encode(DEFAULT_ENCODING) normalized_key = BCE_USER_METADATA_PREFIX + k headers[normalized_key] = v += normalized_key.length += v.length end if > MAX_USER_METADATA_SIZE raise BceClientException.new("Metadata size should not be greater than #{MAX_USER_METADATA_SIZE}") end headers.delete('user-metadata') end |
#put_bucket_cors(bucket_name, cors_configuration) ⇒ Object
Put Bucket Cors.
116 117 118 119 120 121 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 116 def put_bucket_cors(bucket_name, cors_configuration) params = { cors: "" } headers = { CONTENT_TYPE => JSON_TYPE } body = { corsConfiguration: cors_configuration }.to_json send_request(PUT, bucket_name, params, "", headers, body) end |
#put_bucket_lifecycle(bucket_name, rules) ⇒ Object
Put Bucket Lifecycle.
81 82 83 84 85 86 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 81 def put_bucket_lifecycle(bucket_name, rules) params = { lifecycle: "" } headers = { CONTENT_TYPE => JSON_TYPE } body = { rule: rules }.to_json send_request(PUT, bucket_name, params, "", headers, body) end |
#put_bucket_logging(source_bucket, target_bucket, target_prefix = "") ⇒ Object
Put Bucket Logging.
136 137 138 139 140 141 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 136 def put_bucket_logging(source_bucket, target_bucket, target_prefix="") params = { logging: "" } headers = { CONTENT_TYPE => JSON_TYPE } body = { targetBucket: target_bucket, targetPrefix: target_prefix }.to_json send_request(PUT, source_bucket, params, "", headers, body) end |
#put_bucket_storageclass(bucket_name, storage_class) ⇒ Object
Put Bucket Storageclass.
101 102 103 104 105 106 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 101 def put_bucket_storageclass(bucket_name, storage_class) params = { storageClass: "" } headers = { CONTENT_TYPE => JSON_TYPE } body = { storageClass: storage_class }.to_json send_request(PUT, bucket_name, params, "", headers, body) end |
#put_object(bucket_name, key, data, content_md5, content_length, options, &block) ⇒ Object
Put object to BOS.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 201 def put_object(bucket_name, key, data, content_md5, content_length, , &block) if content_length > MAX_PUT_OBJECT_LENGTH raise BceClientException.new("Object length should be less than #{MAX_PUT_OBJECT_LENGTH}. Use multi-part upload instead.") end headers = { CONTENT_MD5 => content_md5, CONTENT_LENGTH => content_length, } headers.merge! headers[CONTENT_TYPE] = OCTET_STREAM_TYPE if headers[CONTENT_TYPE].nil? (headers) unless headers['user-metadata'].nil? send_request(PUT, bucket_name, {}, key, headers, data, &block) end |
#put_object_from_file(bucket_name, key, file_name, options = {}) ⇒ Object
Put object and put content of file to the object.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 223 def put_object_from_file(bucket_name, key, file_name, ={}) mime = MimeMagic.by_path(file_name) [CONTENT_TYPE] = mime.type if [CONTENT_TYPE].nil? && !mime.nil? buf_size = @config.recv_buf_size if [CONTENT_LENGTH].nil? data = File.open(file_name, "rb") data_md5 = Utils.get_md5_from_file(file_name, data.size, buf_size) put_object(bucket_name, key, data, data_md5, data.size, ) else left_size = [CONTENT_LENGTH] data_md5 = Utils.get_md5_from_file(file_name, left_size, buf_size) put_object(bucket_name, key, "", data_md5, left_size, ) do |buf_writer| File.open(file_name, "rb") do |part_fp| bytes_to_read = left_size > buf_size ? buf_size : left_size until left_size <= 0 buf_writer << part_fp.read(bytes_to_read) left_size -= bytes_to_read end end end end end |
#put_object_from_string(bucket_name, key, data, options = {}) ⇒ Object
Create object and put content of string to the object.
217 218 219 220 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 217 def put_object_from_string(bucket_name, key, data, ={}) data_md5 = Digest::MD5.base64digest(data) put_object(bucket_name, key, data, data_md5, data.length, ) end |
#send_request(http_method, bucket_name = "", params = {}, key = "", headers = {}, body = "", save_path = nil, return_body = false, is_bucket_api = false, &block) ⇒ Object
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 454 def send_request(http_method, bucket_name="", params={}, key="", headers={}, body="", save_path=nil, return_body=false, is_bucket_api=false, &block) gen_host(bucket_name, is_bucket_api) if @config.cname_enabled || Utils.is_cname_like_host(@config.endpoint) path = Utils.append_uri("/", key) else if is_bucket_api == false path = Utils.append_uri("/", bucket_name, key) end end begin resp_body, resp_headers = @http_client.send_request(@config, @signer, http_method, path, params, headers, body, save_path, false, &block) rescue BceClientException, BceServerException => err # retry backup endpoint if err.is_a?(BceClientException) || (err.is_a?(BceServerException) && (err.status_code.nil? || err.status_code == 408 || (err.status_code >= 500 && err.status_code != 501))) unless @config.backup_endpoint.to_s.empty? if @config.cname_enabled || Utils.is_cname_like_host(@config.backup_endpoint) path = Utils.append_uri("/", key) else path = Utils.append_uri("/", bucket_name, key) end resp_body, resp_headers = @http_client.send_request(@config, @signer, http_method, path, params, headers, body, save_path, true, &block) else raise err end else raise err end end # Generate result from headers and body Utils.generate_response(resp_headers, resp_body, return_body) end |
#set_bucket_acl(bucket_name, acl) ⇒ Object
Set Access Control Level of bucket by body.
66 67 68 69 70 71 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 66 def set_bucket_acl(bucket_name, acl) params = { acl: "" } headers = { CONTENT_TYPE => JSON_TYPE } body = { accessControlList: acl }.to_json send_request(PUT, bucket_name, params, "", headers, body) end |
#set_bucket_canned_acl(bucket_name, canned_acl) ⇒ Object
Set Access Control Level of bucket by headers.
74 75 76 77 78 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 74 def set_bucket_canned_acl(bucket_name, canned_acl) params = { acl: "" } headers = {BCE_ACL => canned_acl} send_request(PUT, bucket_name, params, "", headers) end |
#set_object_acl(bucket_name, key, acl) ⇒ Object
Set object acl by body.
405 406 407 408 409 410 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 405 def set_object_acl(bucket_name, key, acl) params = { acl: "" } headers = { CONTENT_TYPE => JSON_TYPE } body = { accessControlList: acl }.to_json send_request(PUT, bucket_name, params, key, headers, body) end |
#set_object_canned_acl(bucket_name, key, canned_acl = {}) ⇒ Object
Set object acl by headers.
413 414 415 416 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 413 def set_object_canned_acl(bucket_name, key, canned_acl={}) params = { acl: "" } send_request(PUT, bucket_name, params, key, canned_acl) end |
#upload_part(bucket_name, key, upload_id, part_number, part_size, options = {}, &block) ⇒ Object
Upload a part.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 315 def upload_part(bucket_name, key, upload_id, part_number, part_size, ={}, &block) headers = params={ partNumber: part_number, uploadId: upload_id } if part_number < MIN_PART_NUMBER || part_number > MAX_PART_NUMBER raise BceClientException.new(sprintf("Invalid part_number %d. The valid range is from %d to %d.", part_number, MIN_PART_NUMBER, MAX_PART_NUMBER)) end if part_size > MAX_PUT_OBJECT_LENGTH raise BceClientException.new(sprintf("Single part length should be less than %d.", MAX_PUT_OBJECT_LENGTH)) end headers[CONTENT_LENGTH] = part_size headers[CONTENT_TYPE] = OCTET_STREAM_TYPE send_request(POST, bucket_name, params, key, headers, &block) end |
#upload_part_copy(source_bucket_name, source_key, target_bucket_name, target_key, upload_id, part_number, part_size, offset, options = {}) ⇒ Object
Copy part.
352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 352 def upload_part_copy(source_bucket_name, source_key, target_bucket_name, target_key, upload_id, part_number, part_size, offset, ={}) headers = params={ partNumber: part_number, uploadId: upload_id } (headers) unless headers['user-metadata'].nil? headers[BCE_COPY_SOURCE_IF_MATCH] = headers['etag'] unless headers['etag'].nil? headers[BCE_COPY_SOURCE_RANGE] = sprintf("bytes=%d-%d", offset, offset + part_size - 1) headers[BCE_COPY_SOURCE] = Utils.url_encode_except_slash("/#{source_bucket_name}/#{source_key}") send_request(PUT, target_bucket_name, params, target_key, headers) end |
#upload_part_from_file(bucket_name, key, upload_id, part_number, part_size, file_name, offset = 0, options = {}) ⇒ Object
Upload a part from file.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/baidubce/services/bos/bos_client.rb', line 334 def upload_part_from_file(bucket_name, key, upload_id, part_number, part_size, file_name, offset=0, ={}) left_size = part_size buf_size = @config.send_buf_size upload_part(bucket_name, key, upload_id, part_number, part_size, ) do |buf_writer| File.open(file_name, "rb") do |part_fp| part_fp.seek(offset) bytes_to_read = left_size > buf_size ? buf_size : left_size until left_size <= 0 buf_writer << part_fp.read(bytes_to_read) left_size -= bytes_to_read end end end end |