class Oss::Bucket

Attributes

client[RW]
xml_obj[RW]

Public Class Methods

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

Public Instance Methods

contents() click to toggle source
# File lib/oss/bucket.rb, line 239
def contents
  contents = Array.new
  xml_contents = @xml_obj.xpath('Contents')
  xml_contents.each do |content|
    contents << {
        key:           content.xpath('Key').text,
        last_modified: content.xpath('LastModified').text,
        etag:          content.xpath('ETag').text,
        type:          content.xpath('Type').text,
        size:          content.xpath('Size').text,
        storage_class: content.xpath('StorageClass').text,
        owner: {
            id:           content.xpath('Owner/ID').text,
            display_name: content.xpath('Owner/DisplayName').text
        }
    }
  end
  contents
end
delete_bucket(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 445
def delete_bucket(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?logging',
      sign_configs: sign_configs
  )

  true
end
delete_bucket_lifecycle(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 429
def delete_bucket_lifecycle(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?lifecycle',
      sign_configs: sign_configs
  )

  true
end
delete_bucket_logging(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 397
def delete_bucket_logging(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?logging',
      sign_configs: sign_configs
  )

  true
end
delete_bucket_website(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 413
def delete_bucket_website(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?website',
      sign_configs: sign_configs
  )

  true
end
get_bucket(bucket_name, options = {}) click to toggle source

params:

  • options:

    • delimiter

    • prefix

    • marker

    • max_keys

    • encoding_type

# File lib/oss/bucket.rb, line 224
def get_bucket(bucket_name, options = {})
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  @xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/',
      sign_configs: sign_configs,
      query_string: options
  ).xpath('ListBucketResult')

  self
end
get_bucket_acl(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 261
def get_bucket_acl(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?acl',
      sign_configs: sign_configs
  ).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_bucket_lifecycle(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 366
def get_bucket_lifecycle(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?lifecycle',
      sign_configs: sign_configs
  ).xpath('LifecycleConfiguration')

  rules = Array.new
  xml_obj.xpath('Rule').each do |xml_rule|
    rule = {
        id: xml_rule.xpath('ID').text,
        prefix: xml_rule.xpath('Prefix').text,
        status: xml_rule.xpath('Status').text == 'Enabled',
    }
    if xml_rule.xpath('Expiration/Days').length > 0
      rule[:days] = xml_rule.xpath('Expiration/Days').text.to_i
    else
      rule[:date] = xml_rule.xpath('Expiration/Date').text
    end
    rules << rule
  end

  rules
end
get_bucket_location(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 283
def get_bucket_location(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?location',
      sign_configs: sign_configs
  )

  xml_obj.xpath('LocationConstraint').text
end
get_bucket_logging(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 299
def get_bucket_logging(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?logging',
      sign_configs: sign_configs
  ).xpath('BucketLoggingStatus/LoggingEnabled')

  if xml_obj.length > 0
    {
        logging_enabled: true,
        target_bucket:   xml_obj.xpath('TargetBucket').text,
        target_prefix:   xml_obj.xpath('TargetPrefix').text,
    }
  else
    { logging_enabled: false }
  end
end
get_bucket_referer(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 342
def get_bucket_referer(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?referer',
      sign_configs: sign_configs
  ).xpath('RefererConfiguration')

  referers = Array.new
  xml_obj.xpath('RefererList/Referer').each do |referer|
    referers << referer.text
  end

  {
      allow_empty_referer: xml_obj.xpath('AllowEmptyReferer').text == 'true',
      referers: referers,
  }
end
get_bucket_website(bucket_name) click to toggle source

params:

  • bucket_name

# File lib/oss/bucket.rb, line 323
def get_bucket_website(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?website',
      sign_configs: sign_configs
  ).xpath('WebsiteConfiguration')

  {
      index_doc: xml_obj.xpath('IndexDocument/Suffix').text,
      error_doc: xml_obj.xpath('ErrorDocument/Key').text,
  }
end
method_missing(method) click to toggle source
Calls superclass method
# File lib/oss/bucket.rb, line 460
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
put_bucket(bucket_name, acl) click to toggle source

params:

  • bucket_name

  • acl

# File lib/oss/bucket.rb, line 20
def put_bucket(bucket_name, acl)
  # get location
  location = client.endpoint.split('.')[0]

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    CreateBucketConfiguration do
      LocationConstraint location
    end
  end

  # set acl header
  headers = Hash.new
  headers['x-oss-acl'] = acl

  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:oss_headers]  = "x-oss-acl:#{acl}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

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

  true
end
put_bucket_acl(bucket_name, acl) click to toggle source

params:

  • bucket_name

  • acl

# File lib/oss/bucket.rb, line 55
def put_bucket_acl(bucket_name, acl)
  # set acl header
  headers = Hash.new
  headers['x-oss-acl'] = acl

  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:oss_headers]  = "x-oss-acl:#{acl}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

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

  true
end
put_bucket_lifecycle(bucket_name, rules = []) click to toggle source

params:

  • bucket_name

  • rules

    • prefix

    • id

    • status

    • days

    • date

# File lib/oss/bucket.rb, line 181
def put_bucket_lifecycle(bucket_name, rules = [])
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    LifecycleConfiguration do
      rules.each do |rule|
        Rule do
          ID rule[:id] unless rule[:id].nil?
          Prefix rule[:prefix]
          Status rule[:status] ? 'Enabled' : 'Disabled'
          Expiration do
            if rule[:days] != nil
              Days rule[:days]
            else
              Date rule[:date]
            end
          end
        end
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?lifecycle',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end
put_bucket_logging(bucket_name, target_bucket, enable = true, target_prefix = nil) click to toggle source

params:

  • bucket_name

  • target_bucket

  • enable

  • target_prefix

# File lib/oss/bucket.rb, line 81
def put_bucket_logging(bucket_name, target_bucket, enable = true, target_prefix = nil)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    BucketLoggingStatus do
      if enable
        LoggingEnabled do
          TargetBucket target_bucket
          TargetPrefix target_prefix unless target_prefix.nil?
        end
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?logging',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end
put_bucket_referer(bucket_name, allow_empty, referer_list = []) click to toggle source

params:

  • bucket_name

  • allow_empty

  • referer_list

# File lib/oss/bucket.rb, line 145
def put_bucket_referer(bucket_name, allow_empty, referer_list = [])
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    RefererConfiguration do
      AllowEmptyReferer allow_empty.to_s
      RefererList do
        referer_list.each do |referer|
          Referer referer
        end
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?referer',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end
put_bucket_website(bucket_name, index_doc, error_doc) click to toggle source

params:

  • bucket_name

  • index_doc

  • error_doc

# File lib/oss/bucket.rb, line 113
def put_bucket_website(bucket_name, index_doc, error_doc)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    WebsiteConfiguration do
      IndexDocument do
        Suffix index_doc
      end
      ErrorDocument do
        Key error_doc
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?website',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end