class Baidubce::Services::BosClient

Public Instance Methods

abort_multipart_upload(bucket_name, key, upload_id) click to toggle source

Abort upload a part which is being uploading.

# File lib/baidubce/services/bos/bos_client.rb, line 382
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={}) click to toggle source

Put an appendable object to BOS or add content to an appendable object.

# File lib/baidubce/services/bos/bos_client.rb, line 182
def append_object(bucket_name, key, data, offset, content_md5, content_length, options={})
    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! options
    populate_headers_with_user_metadata(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={}) click to toggle source

Create an appendable object and put content of string to the object or add content of string to an appendable object.

# File lib/baidubce/services/bos/bos_client.rb, line 199
def append_object_from_string(bucket_name, key, data, options={})
    data_md5 = Digest::MD5.base64digest(data)
    append_object(bucket_name, key, data, options['offset'], data_md5, data.bytesize, options)
end
complete_multipart_upload(bucket_name, key,upload_id, part_list, options={}) click to toggle source

After finish all the task, complete multi_upload_file.

# File lib/baidubce/services/bos/bos_client.rb, line 371
def complete_multipart_upload(bucket_name, key,upload_id, part_list, options={})
    headers = options
    params = { uploadId: upload_id }

    populate_headers_with_user_metadata(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={}) click to toggle source

Copy one object to another object.

# File lib/baidubce/services/bos/bos_client.rb, line 281
def copy_object(source_bucket_name, source_key, target_bucket_name, target_key, options={})
    headers = options
    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'
        populate_headers_with_user_metadata(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) click to toggle source

Create bucket with specific name.

# File lib/baidubce/services/bos/bos_client.rb, line 32
def create_bucket(bucket_name)
    send_request(PUT, bucket_name)
end
delete_bucket(bucket_name) click to toggle source

Delete bucket with specific name.

# File lib/baidubce/services/bos/bos_client.rb, line 37
def delete_bucket(bucket_name)
    send_request(DELETE, bucket_name)
end
delete_bucket_cors(bucket_name) click to toggle source

Delete Bucket Cors.

# File lib/baidubce/services/bos/bos_client.rb, line 131
def delete_bucket_cors(bucket_name)
    params = { cors: "" }
    send_request(DELETE, bucket_name, params)
end
delete_bucket_lifecycle(bucket_name) click to toggle source

Delete Bucket Lifecycle.

# File lib/baidubce/services/bos/bos_client.rb, line 96
def delete_bucket_lifecycle(bucket_name)
    params = { lifecycle: "" }
    send_request(DELETE, bucket_name, params)
end
delete_bucket_logging(bucket_name) click to toggle source

Delete Bucket Logging.

# File lib/baidubce/services/bos/bos_client.rb, line 151
def delete_bucket_logging(bucket_name)
    params = { logging: "" }
    send_request(DELETE, bucket_name, params)
end
delete_multiple_objects(bucket_name, key_list) click to toggle source

Delete Multiple Objects.

# File lib/baidubce/services/bos/bos_client.rb, line 303
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) click to toggle source

Delete Object.

# File lib/baidubce/services/bos/bos_client.rb, line 298
def delete_object(bucket_name, key)
    send_request(DELETE, bucket_name, {}, key)
end
delete_object_acl(bucket_name, key) click to toggle source

Delete object acl.

# File lib/baidubce/services/bos/bos_client.rb, line 423
def delete_object_acl(bucket_name, key)
    params = { acl: "" }
    send_request(DELETE, bucket_name, params, key)
end
does_bucket_exist(bucket_name) click to toggle source

Check whether there is a bucket with specific name.

# File lib/baidubce/services/bos/bos_client.rb, line 42
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
generate_pre_signed_url(bucket_name, key, options={}) click to toggle source

Get an authorization url with expire time.

# File lib/baidubce/services/bos/bos_client.rb, line 251
def generate_pre_signed_url(bucket_name, key, options={})
    headers = options['headers'].nil? ? {} : options['headers']
    params = options['params'].nil? ? {} : options['params']

    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,
                                                        options['timestamp'],
                                                        options['expiration_in_seconds'] || 1800,
                                                        options['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) click to toggle source

Get Access Control Level of bucket.

# File lib/baidubce/services/bos/bos_client.rb, line 61
def get_bucket_acl(bucket_name)
    params = { acl: "" }
    send_request(GET, bucket_name, params)
end
get_bucket_cors(bucket_name) click to toggle source

Get Bucket Cors.

# File lib/baidubce/services/bos/bos_client.rb, line 125
def get_bucket_cors(bucket_name)
    params = { cors: "" }
    send_request(GET, bucket_name, params)
end
get_bucket_lifecycle(bucket_name) click to toggle source

Gut Bucket Lifecycle.

# File lib/baidubce/services/bos/bos_client.rb, line 90
def get_bucket_lifecycle(bucket_name)
    params = { lifecycle: "" }
    send_request(GET, bucket_name, params)
end
get_bucket_location(bucket_name) click to toggle source

Get the region which the bucket located in. returns region of the bucket(bj/gz/sz).

# File lib/baidubce/services/bos/bos_client.rb, line 54
def get_bucket_location(bucket_name)
    params = { location: "" }
    resp = send_request(GET, bucket_name, params)
    resp['locationConstraint']
end
get_bucket_logging(bucket_name) click to toggle source

Get Bucket Logging.

# File lib/baidubce/services/bos/bos_client.rb, line 145
def get_bucket_logging(bucket_name)
    params = { logging: "" }
    send_request(GET, bucket_name, params)
end
get_bucket_storageclass(bucket_name) click to toggle source

Get Bucket Storageclass.

# File lib/baidubce/services/bos/bos_client.rb, line 110
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) click to toggle source
# File lib/baidubce/services/bos/bos_client.rb, line 163
def get_object(bucket_name, key, range, save_path=nil, return_body=true)
    headers = range.nil? ? {} : get_range_header_dict(range)
    if key.size == 0 || key  =~ /^\/+$/
        raise BceClientException.new("Key can not be empty or all is '/'.")
    end
    send_request(GET, bucket_name, {}, key, headers, "", save_path, return_body)
end
get_object_acl(bucket_name, key) click to toggle source

Get object acl.

# File lib/baidubce/services/bos/bos_client.rb, line 403
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) click to toggle source

Get Content of Object and Put Content to String.

# File lib/baidubce/services/bos/bos_client.rb, line 172
def get_object_as_string(bucket_name, key, range=nil)
    get_object(bucket_name, key, range)
end
get_object_meta_data(bucket_name, key) click to toggle source

Get meta of object.

# File lib/baidubce/services/bos/bos_client.rb, line 276
def get_object_meta_data(bucket_name, key)
    send_request(HEAD, bucket_name, {}, key)
end
get_object_to_file(bucket_name, key, save_path, range=nil) click to toggle source

Get Content of Object and Put Content to File.

# File lib/baidubce/services/bos/bos_client.rb, line 177
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) click to toggle source
# File lib/baidubce/services/bos/bos_client.rb, line 463
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={}) click to toggle source

Initialize multi_upload_file.

# File lib/baidubce/services/bos/bos_client.rb, line 313
def initiate_multipart_upload(bucket_name, key, options={})
    params = { uploads: "" }
    send_request(POST, bucket_name, params, key, options)
end
list_buckets() click to toggle source

List buckets of user. returns all buckets owned by the user.

# File lib/baidubce/services/bos/bos_client.rb, line 27
def list_buckets()
    send_request(GET)
end
list_multipart_uploads(bucket_name, options={}) click to toggle source

List all Multipart upload task which haven't been ended.(Completed Init_MultiPartUpload but not completed Complete_MultiPartUpload or Abort_MultiPartUpload).

# File lib/baidubce/services/bos/bos_client.rb, line 396
def list_multipart_uploads(bucket_name, options={})
    params = { uploads: "" }
    params.merge! options
    send_request(GET, bucket_name, params)
end
list_objects(bucket_name, options={}) click to toggle source

Get Object Information of bucket.

# File lib/baidubce/services/bos/bos_client.rb, line 157
def list_objects(bucket_name, options={})
    params = { maxKeys: 1000 }
    params.merge! options
    send_request(GET, bucket_name, params)
end
list_parts(bucket_name, key, upload_id, options={}) click to toggle source

List all the parts that have been upload success.

# File lib/baidubce/services/bos/bos_client.rb, line 388
def list_parts(bucket_name, key, upload_id, options={})
    params = { uploadId: upload_id }
    params.merge! options
    send_request(GET, bucket_name, params, key)
end
populate_headers_with_user_metadata(headers) click to toggle source
# File lib/baidubce/services/bos/bos_client.rb, line 470
def populate_headers_with_user_metadata(headers)
    meta_size = 0
    user_metadata = headers['user-metadata']
    raise BceClientException.new("user_metadata should be of type hash.") unless user_metadata.is_a? Hash

    user_metadata.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
        meta_size += normalized_key.length
        meta_size += v.length
    end

    if meta_size > 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) click to toggle source

Put Bucket Cors.

# File lib/baidubce/services/bos/bos_client.rb, line 117
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) click to toggle source

Put Bucket Lifecycle.

# File lib/baidubce/services/bos/bos_client.rb, line 82
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="") click to toggle source

Put Bucket Logging.

# File lib/baidubce/services/bos/bos_client.rb, line 137
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) click to toggle source

Put Bucket Storageclass.

# File lib/baidubce/services/bos/bos_client.rb, line 102
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) click to toggle source

Put object to BOS.

# File lib/baidubce/services/bos/bos_client.rb, line 205
def put_object(bucket_name, key, data, content_md5, content_length, options, &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! options
    headers[CONTENT_TYPE] = OCTET_STREAM_TYPE if headers[CONTENT_TYPE].nil?
    populate_headers_with_user_metadata(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={}) click to toggle source

Put object and put content of file to the object.

# File lib/baidubce/services/bos/bos_client.rb, line 227
def put_object_from_file(bucket_name, key, file_name, options={})
    mime = MimeMagic.by_path(file_name)
    options[CONTENT_TYPE] = mime.type if options[CONTENT_TYPE].nil? && !mime.nil?
    buf_size = @config.recv_buf_size
    if options[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, options)
    else
        left_size = options[CONTENT_LENGTH]
        data_md5 = Utils.get_md5_from_file(file_name, left_size, buf_size)
        put_object(bucket_name, key, "", data_md5, left_size, options) 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={}) click to toggle source

Create object and put content of string to the object.

# File lib/baidubce/services/bos/bos_client.rb, line 221
def put_object_from_string(bucket_name, key, data, options={})
    data_md5 = Digest::MD5.base64digest(data)
    put_object(bucket_name, key, data, data_md5, data.length, options)
end
send_request(http_method, bucket_name="", params={}, key="", headers={}, body="", save_path=nil, return_body=false, &block) click to toggle source
# File lib/baidubce/services/bos/bos_client.rb, line 428
def send_request(http_method, bucket_name="", params={}, key="", headers={}, body="", save_path=nil, return_body=false, &block)
    if @config.cname_enabled || Utils.is_cname_like_host(@config.endpoint)
        path = Utils.append_uri("/", key)
    else
        path = Utils.append_uri("/", bucket_name, key)
    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) click to toggle source

Set Access Control Level of bucket by body.

# File lib/baidubce/services/bos/bos_client.rb, line 67
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) click to toggle source

Set Access Control Level of bucket by headers.

# File lib/baidubce/services/bos/bos_client.rb, line 75
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) click to toggle source

Set object acl by body.

# File lib/baidubce/services/bos/bos_client.rb, line 409
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={}) click to toggle source

Set object acl by headers.

# File lib/baidubce/services/bos/bos_client.rb, line 417
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) click to toggle source

Upload a part.

# File lib/baidubce/services/bos/bos_client.rb, line 319
def upload_part(bucket_name, key, upload_id, part_number, part_size, options={}, &block)
    headers = options
    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={}) click to toggle source

Copy part.

# File lib/baidubce/services/bos/bos_client.rb, line 356
def upload_part_copy(source_bucket_name, source_key, target_bucket_name, target_key, upload_id,
                     part_number, part_size, offset, options={})
    headers = options
    params={ partNumber: part_number, uploadId: upload_id }

    populate_headers_with_user_metadata(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={}) click to toggle source

Upload a part from file.

# File lib/baidubce/services/bos/bos_client.rb, line 338
def upload_part_from_file(bucket_name, key, upload_id, part_number,
                          part_size, file_name, offset=0, options={})

    left_size = part_size
    buf_size = @config.send_buf_size
    upload_part(bucket_name, key, upload_id, part_number, part_size, options) 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