Class: S3::ObjectListRequest

Inherits:
Request
  • Object
show all
Defined in:
lib/s3/object_list_request.rb

Constant Summary

Constants inherited from Request

Request::AWS_ALGORITHM, Request::AWS_REQUEST, Request::SERVICE, Request::UNSIGNED_PAYLOAD

Instance Method Summary collapse

Methods inherited from Request

#initialize

Constructor Details

This class inherits a constructor from S3::Request

Instance Method Details

#parse_response(body) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/s3/object_list_request.rb', line 22

def parse_response( body )
  document = parse_xml( body )
  document.remove_namespaces!

  contents = document.xpath( '//Contents' ).map do | node |
    { key: node.at( 'Key' )&.text,
      size: node.at( 'Size' )&.text&.to_i,
      last_modified: Helpers.parse_iso8601( node.at( 'LastModified' )&.text ),
      etag: node.at( 'ETag' )&.text&.tr( '"', '' ),
      storage_class: node.at( 'StorageClass' )&.text
    }
  end

  common_prefixes = document.xpath( '//CommonPrefixes/Prefix' ).map( &:text )

  { contents: contents,
    common_prefixes: common_prefixes,
    is_truncated: document.at( '//IsTruncated' )&.text == 'true',
    next_continuation_token: document.at( '//NextContinuationToken' )&.text,
    key_count: document.at( '//KeyCount' )&.text&.to_i,
    name: document.at( '//Name' )&.text,
    prefix: document.at( '//Prefix' )&.text,
    max_keys: document.at( '//MaxKeys' )&.text&.to_i
  }
end

#submit(options = nil, bucket:, **kwargs) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/s3/object_list_request.rb', line 3

def submit( options = nil, bucket:, **kwargs )
  options = merge_options( options, kwargs, ObjectListOptions )

  params = { 'list-type' => '2' }
  params[ 'prefix' ] = options[ :prefix ] if options[ :prefix ]
  params[ 'delimiter' ] = options[ :delimiter ] if options[ :delimiter ]
  params[ 'max-keys' ] = options[ :max_keys ].to_s if options[ :max_keys ]
  params[ 'continuation-token' ] = options[ :continuation_token ] if options[ :continuation_token ]
  params[ 'start-after' ] = options[ :start_after ] if options[ :start_after ]

  query = Helpers.build_query_string( params )

  response = get( "/#{ bucket }", query: query )

  build_result( response, ObjectListResult ) do
    ObjectListResult.new( parse_response( response.body ) )
  end
end