Method: Aliyun::OSS::Protocol#list_objects

Defined in:
lib/aliyun/oss/protocol.rb

#list_objects(bucket_name, opts = {}) ⇒ Array<Objects>, Hash

List objects in a bucket.

Examples:

Assume we have the following objects:
   /foo/bar/obj1
   /foo/bar/obj2
   ...
   /foo/bar/obj9999999
   /foo/xxx/
use 'foo/' as the prefix, '/' as the delimiter, the common
prefixes we get are: '/foo/bar/', '/foo/xxx/'. They are
coincidentally the sub-directories under '/foo/'. Using
delimiter we avoid list all the objects whose number may be
large.

Parameters:

  • bucket_name (String)

    the bucket name

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :prefix (String)

    return only those buckets prefixed with it if specified

  • :marker (String)

    return buckets after where it indicates (exclusively). All buckets are sorted by name alphabetically

  • :limit (Integer)

    return only the first N buckets if specified

  • :delimiter (String)

    the delimiter to get common prefixes of all objects

  • :encoding (String)

    the encoding of object key in the response body. Only KeyEncoding::URL is supported now.

Returns:

  • (Array<Objects>, Hash)

    the returned object and a hash including the next tokens, which includes:

    • :common_prefixes [String] the common prefixes returned

    • :prefix [String] the prefix used

    • :delimiter [String] the delimiter used

    • :marker [String] the marker used

    • :limit [Integer] the limit used

    • :next_marker [String] marker to continue list objects

    • :truncated [Boolean] whether there are more objects to be returned



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/aliyun/oss/protocol.rb', line 606

def list_objects(bucket_name, opts = {})
  logger.debug("Begin list object, bucket: #{bucket_name}, options: #{opts}")

  params = {
    'prefix' => opts[:prefix],
    'delimiter' => opts[:delimiter],
    'marker' => opts[:marker],
    'max-keys' => opts[:limit],
    'encoding-type' => opts[:encoding]
  }.reject { |_, v| v.nil? }

  _, body = @http.get({:bucket => bucket_name}, {:query => params})

  doc = parse_xml(body)

  encoding = get_node_text(doc.root, 'EncodingType')

  objects = doc.css("Contents").map do |node|
    Object.new(
      :key => get_node_text(node, "Key") { |x| decode_key(x, encoding) },
      :type => get_node_text(node, "Type"),
      :size => get_node_text(node, "Size", &:to_i),
      :etag => get_node_text(node, "ETag"),
      :last_modified =>
        get_node_text(node, "LastModified") { |x| Time.parse(x) }
    )
  end || []

  more = {
    :prefix => 'Prefix',
    :delimiter => 'Delimiter',
    :limit => 'MaxKeys',
    :marker => 'Marker',
    :next_marker => 'NextMarker',
    :truncated => 'IsTruncated',
    :encoding => 'EncodingType'
  }.reduce({}) { |h, (k, v)|
    value = get_node_text(doc.root, v)
    value.nil?? h : h.merge(k => value)
  }

  update_if_exists(
    more, {
      :limit => ->(x) { x.to_i },
      :truncated => ->(x) { x.to_bool },
      :delimiter => ->(x) { decode_key(x, encoding) },
      :marker => ->(x) { decode_key(x, encoding) },
      :next_marker => ->(x) { decode_key(x, encoding) }
    }
  )

  common_prefixes = []
  doc.css("CommonPrefixes Prefix").map do |node|
    common_prefixes << decode_key(node.text, encoding)
  end
  more[:common_prefixes] = common_prefixes unless common_prefixes.empty?

  logger.debug("Done list object. objects: #{objects}, more: #{more}")

  [objects, more]
end