class Oss::Object

Attributes

client[RW]
xml_obj[RW]

Public Class Methods

new(client) click to toggle source
# File lib/oss/object.rb, line 14
def initialize(client)
  @client = client
end

Public Instance Methods

append_object(bucket_name, object, file, position = 0, options = {}) click to toggle source
# File lib/oss/object.rb, line 141
def append_object(bucket_name, object, file, position = 0, options = {})
  # set header
  headers = Util.hash_filter(options, {
                                        expires:             'Expires',
                                        content_control:     'Cache-Control',
                                        content_encoding:    'Content-Encoding',
                                        content_type:        'Content-Type',
                                        content_md5:         'Content-MD5',
                                        content_disposition: 'Content-Disposition',
                                        encryption:          'x-oss-server-side-encryption',
                                        acl:                 'x-oss-object-acl',
                                    })

  # sign configs
  sign_configs = {
      resource:     "/#{bucket_name}",
      content_type: options[:content_type],
      content_md5:  options[:content_md5],
      sign_query_string: true,
      oss_headers:  Util.oss_headers_to_s(options, {
                                                     acl:        'x-oss-object-acl',
                                                     encryption: 'x-oss-server-side-encryption',
                                                 })
  }

  resp = client.post(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object}?append&position=#{position}",
      headers:      headers,
      sign_configs: sign_configs,
      payload:      file,
      as:           :raw
  )

  {
      hash_crc64ecma:       resp.headers[:x_oss_hash_crc64ecma],
      next_append_position: resp.headers[:x_oss_next_append_position].to_i
  }
end
copy_object(bucket_name, object, old_bucket, old_object, options = {}) click to toggle source
# File lib/oss/object.rb, line 56
def copy_object(bucket_name, object, old_bucket, old_object, options = {})
  # copy source format
  options[:copy_source] = "/#{old_bucket}/#{old_object}"

  # set header
  headers = Util.hash_filter(options, {
                                        metadata_directive:  'x-oss-metadata-directive',
                                        if_modified_since:   'x-oss-copy-source-if-modified-since',
                                        if_unmodified_since: 'x-oss-copy-source-if-unmodified-since',
                                        if_match:            'x-oss-copy-source-if-match',
                                        if_none_match:       'x-oss-copy-source-if-none-match',
                                        encryption:          'x-oss-server-side-encryption',
                                        acl:                 'x-oss-object-acl',
                                        copy_source:         'x-oss-copy-source'
                                    })

  # sign configs
  sign_configs = {
      resource:     "/#{bucket_name}",
      content_type: 'application/x-www-form-urlencoded',
      oss_headers:  Util.oss_headers_to_s(options, {
                                                     metadata_directive:  'x-oss-metadata-directive',
                                                     if_modified_since:   'x-oss-copy-source-if-modified-since',
                                                     if_unmodified_since: 'x-oss-copy-source-if-unmodified-since',
                                                     if_match:            'x-oss-copy-source-if-match',
                                                     if_none_match:       'x-oss-copy-source-if-none-match',
                                                     encryption:          'x-oss-server-side-encryption',
                                                     acl:                 'x-oss-object-acl',
                                                     copy_source:         'x-oss-copy-source'
                                                 })
  }

  @xml_obj = client.put(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object}",
      headers:      headers,
      sign_configs: sign_configs,
  )

  {
      last_modify: @xml_obj.xpath('CopyObjectResult/LastModified').text,
      etag:        @xml_obj.xpath('CopyObjectResult/ETag').text,
  }
end
delete_multiple_objects(bucket_name, objects = []) click to toggle source
# File lib/oss/object.rb, line 195
def delete_multiple_objects(bucket_name, objects = [])
  # build payload xml
  payload = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do
    Delete do
      Quiet 'false'
      objects.each do |obj|
        Object do
          Key obj
        end
      end
    end
  end

  xml_obj = client.post(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/?delete",
      sign_configs: {resource: "/#{bucket_name}", content_type: 'application/x-www-form-urlencoded'},
      content_md5_check: true,
      content_length_check: true,
      payload: payload.to_xml
  )

  # parse return deleted keys
  deleted = Array.new
  xml_obj.xpath('DeleteResult/Deleted').each do |obj|
    deleted << obj.xpath('Key').text
  end

  deleted
end
delete_object(bucket_name, object_name) click to toggle source

params:

  • bucket_name

  • object_name

# File lib/oss/object.rb, line 184
def delete_object(bucket_name, object_name)

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/#{object_name}",
      sign_configs: {resource: "/#{bucket_name}"}
  )

  true
end
get_object(bucket_name, object, options = {}) click to toggle source
# File lib/oss/object.rb, line 111
def get_object(bucket_name, object, options = {})
  # set header
  headers = Util.hash_filter(options, {
                                        range:               'Range',
                                        if_modified_since:   'If-Modified-Since',
                                        if_unmodified_since: 'If-Unmodified-Since',
                                        if_match:            'If-Match',
                                        if_none_match:       'If-None-Match'
                                    })

  # request params query string
  query_string = Util.hash_filter(options, {
                                             response_content_type:        'response-content-type',
                                             response_content_language:    'response-content-language',
                                             response_expires:             'response-expires',
                                             response_cache_control:       'response-cache-control',
                                             response_content_disposition: 'response-content-disposition',
                                             response_content_encoding:    'response-content-encoding'
                                         })

  client.get(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object}",
      headers:      headers,
      sign_configs: { resource: "/#{bucket_name}", sign_query_string: true },
      query_string: query_string,
      as:           :raw
  )
end
get_object_acl(bucket_name, object_name) click to toggle source

params:

  • bucket_name

  • object_name

# File lib/oss/object.rb, line 271
def get_object_acl(bucket_name, object_name)
  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/#{object_name}?acl",
      sign_configs: {resource: "/#{bucket_name}"}
  ).xpath('AccessControlPolicy')

  {
      grant: xml_obj.xpath('AccessControlList/Grant').text,
      owner: {
          id:           xml_obj.xpath('Owner/ID').text,
          display_name: xml_obj.xpath('Owner/DisplayName').text
      }
  }
end
get_object_url(bucket_name, object, options = {}) click to toggle source
# File lib/oss/object.rb, line 101
def get_object_url(bucket_name, object, options = {})
  if options[:from].to_s == 'internal'
    "http://#{bucket_name}.#{client.endpoint.gsub('-internal', '').gsub('.aliyuncs.com', '-internal.aliyuncs.com')}/#{object}"
  elsif options[:from].to_s == 'cdn'
    "http://#{options[:cdn_domain]}/#{object}"
  else
    "http://#{bucket_name}.#{client.endpoint}/#{object}"
  end
end
head_object(bucket_name, object_name, options = {}) click to toggle source
# File lib/oss/object.rb, line 226
def head_object(bucket_name, object_name, options = {})
  # set header
  headers = Util.hash_filter(options, {
                                        if_modified_since:   'If-Modified-Since',
                                        if_unmodified_since: 'If-Unmodified-Since',
                                        if_match:            'If-Match',
                                        if_none_match:       'If-None-Match'
                                    })

  resp = client.head(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/#{object_name}",
      headers: headers,
      sign_configs: {resource: "/#{bucket_name}"},
      as: :raw
  )

  # head request returns hole response headers
  resp.headers
end
method_missing(method) click to toggle source
Calls superclass method
# File lib/oss/object.rb, line 317
def method_missing(method)
  if @xml_obj.nil?
    super
  else
    camel = Util.camelize(method)
    value = @xml_obj.xpath(camel)
    raise "missing xml attribute #{camel}" if value.length == 0
    value.inner_text
  end
end
post_object(bucket_name, key, options = {}) click to toggle source

params:

  • bucket_name

  • key

  • options

# File lib/oss/object.rb, line 291
def post_object(bucket_name, key, options = {})

  # build form upload info hash
  form_hash = { 'key' => key, 'action' => "http://#{bucket_name}.#{client.endpoint}/"}
  options.each do |k, v|
    # need Signature
    if k == :policy
      # json policy string to base64 policy string
      form_hash['policy'] = Base64.encode64(v).gsub("\n", '')

      # create signature
      digest = OpenSSL::Digest.new('sha1')
      h = OpenSSL::HMAC.digest(digest, client.access_key_secret, form_hash['policy'])
      # base64 result
      form_hash['Signature'] = Base64.encode64(h).gsub("\n", '')
      # add access key id
      form_hash['OSSAccessKeyId'] = client.access_key_id
    else
      form_hash[k.to_s] = v
    end
  end

  # return a hash for render a html upload form
  form_hash
end
put_object(bucket_name, object, file, options = {}) click to toggle source
# File lib/oss/object.rb, line 18
def put_object(bucket_name, object, file, options = {})
  options[:content_type] = 'application/x-www-form-urlencoded' if options[:content_type].nil?

  # set header
  headers = Util.hash_filter(options, {
                                        expires:             'Expires',
                                        content_control:     'Cache-Control',
                                        content_encoding:    'Content-Encoding',
                                        content_type:        'Content-Type',
                                        content_md5:         'Content-MD5',
                                        content_disposition: 'Content-Disposition',
                                        encryption:          'x-oss-server-side-encryption',
                                        acl:                 'x-oss-object-acl',
                                    })

  # sign configs
  sign_configs = {
      resource:              "/#{bucket_name}",
      content_type:          options[:content_type],
      content_md5_check:     options[:content_md5_check],
      content_length_check:  options[:content_length_check],
      oss_headers:  Util.oss_headers_to_s(options, {
                                                    acl:        'x-oss-object-acl',
                                                    encryption: 'x-oss-server-side-encryption',
                                                })
  }

  client.put(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object}",
      headers:      headers,
      sign_configs: sign_configs,
      payload:      file,
  )

  true
end
put_object_acl(bucket_name, object_name, acl) click to toggle source

params:

  • bucket_name

  • object_name

  • acl

# File lib/oss/object.rb, line 251
def put_object_acl(bucket_name, object_name, acl)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:oss_headers]  = "x-oss-object-acl:#{acl}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/#{object_name}?acl",
      headers: {'x-oss-object-acl' => acl},
      sign_configs: sign_configs
  )

  true
end