2010-03-13 16:37:24 -05:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
module S3
|
|
|
|
class Real
|
2009-08-08 15:31:32 -04:00
|
|
|
|
|
|
|
# List information about objects in an S3 bucket
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * bucket_name<~String> - name of bucket to list object keys from
|
|
|
|
# * options<~Hash> - config arguments for list. Defaults to {}.
|
|
|
|
# * 'prefix'<~String> - limits object keys to those beginning with its value.
|
|
|
|
# * 'marker'<~String> - limits object keys to only those that appear
|
|
|
|
# lexicographically after its value.
|
|
|
|
# * 'max-keys'<~Integer> - limits number of object keys returned
|
|
|
|
# * 'delimiter'<~String> - causes keys with the same string between the prefix
|
|
|
|
# value and the first occurence of delimiter to be rolled up
|
|
|
|
#
|
|
|
|
# ==== Returns
|
2009-11-02 21:48:49 -05:00
|
|
|
# * response<~Excon::Response>:
|
2009-08-08 15:31:32 -04:00
|
|
|
# * body<~Hash>:
|
|
|
|
# * 'Delimeter'<~String> - Delimiter specified for query
|
|
|
|
# * 'Marker'<~String> - Marker specified for query
|
|
|
|
# * 'MaxKeys'<~Integer> - Maximum number of keys specified for query
|
|
|
|
# * 'Name'<~String> - Name of the bucket
|
|
|
|
# * 'Prefix'<~String> - Prefix specified for query
|
|
|
|
# * 'Contents'<~Array>:
|
|
|
|
# * 'ETag'<~String>: Etag of object
|
|
|
|
# * 'Key'<~String>: Name of object
|
|
|
|
# * 'LastModified'<~String>: Timestamp of last modification of object
|
|
|
|
# * 'Owner'<~Hash>:
|
|
|
|
# * 'DisplayName'<~String> - Display name of object owner
|
|
|
|
# * 'ID'<~String> - Id of object owner
|
|
|
|
# * 'Size'<~Integer> - Size of object
|
|
|
|
# * 'StorageClass'<~String> - Storage class of object
|
|
|
|
def get_bucket(bucket_name, options = {})
|
2009-09-08 23:24:21 -04:00
|
|
|
unless bucket_name
|
|
|
|
raise ArgumentError.new('bucket_name is required')
|
|
|
|
end
|
2010-03-27 16:23:12 -04:00
|
|
|
options.reject! {|key, value| !['prefix', 'marker', 'max-keys', 'delimiter'].include?(key)}
|
2009-08-17 20:38:59 -04:00
|
|
|
query = ''
|
2009-08-08 15:31:32 -04:00
|
|
|
for key, value in options
|
2010-01-05 01:03:24 -05:00
|
|
|
query << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')};"
|
2009-08-08 15:31:32 -04:00
|
|
|
end
|
|
|
|
query.chop!
|
|
|
|
request({
|
2009-08-16 14:44:50 -04:00
|
|
|
:expects => 200,
|
|
|
|
:headers => {},
|
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-12-08 14:22:46 -05:00
|
|
|
:idempotent => true,
|
2009-08-16 14:44:50 -04:00
|
|
|
:method => 'GET',
|
|
|
|
:parser => Fog::Parsers::AWS::S3::GetBucket.new,
|
|
|
|
:query => query
|
2009-08-08 15:31:32 -04:00
|
|
|
})
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
2009-08-08 15:31:32 -04:00
|
|
|
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
2009-08-08 15:31:32 -04:00
|
|
|
|
2010-03-13 16:37:24 -05:00
|
|
|
class Mock
|
2009-08-08 15:31:32 -04:00
|
|
|
|
2009-08-18 12:32:25 -04:00
|
|
|
# FIXME: implement delimiter
|
2009-08-08 15:31:32 -04:00
|
|
|
def get_bucket(bucket_name, options = {})
|
2009-09-08 23:24:21 -04:00
|
|
|
unless bucket_name
|
|
|
|
raise ArgumentError.new('bucket_name is required')
|
|
|
|
end
|
2009-11-20 14:08:08 -05:00
|
|
|
response = Excon::Response.new
|
2010-03-13 16:37:24 -05:00
|
|
|
if bucket = @data[:buckets][bucket_name]
|
2009-08-08 15:31:32 -04:00
|
|
|
response.status = 200
|
|
|
|
response.body = {
|
2009-08-18 12:32:25 -04:00
|
|
|
'Contents' => bucket[:objects].values.sort {|x,y| x['Key'] <=> y['Key']}.reject do |object|
|
|
|
|
(options['prefix'] && object['Key'][0...options['prefix'].length] != options['prefix']) ||
|
|
|
|
(options['marker'] && object['Key'] <= options['marker'])
|
|
|
|
end.map do |object|
|
2010-03-13 16:37:24 -05:00
|
|
|
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Size', 'StorageClass'].include?(key)}
|
2009-08-18 12:32:25 -04:00
|
|
|
data.merge!({
|
|
|
|
'LastModified' => Time.parse(data['LastModified']),
|
2010-03-13 16:37:24 -05:00
|
|
|
'Owner' => bucket['Owner'],
|
2009-08-18 12:32:25 -04:00
|
|
|
'Size' => data['Size'].to_i
|
|
|
|
})
|
2009-08-08 17:46:13 -04:00
|
|
|
data
|
2009-08-09 01:40:42 -04:00
|
|
|
end,
|
|
|
|
'IsTruncated' => false,
|
2009-08-18 12:32:25 -04:00
|
|
|
'Marker' => options['marker'] || '',
|
|
|
|
'MaxKeys' => options['max-keys'] || 1000,
|
2009-08-09 01:40:42 -04:00
|
|
|
'Name' => bucket['Name'],
|
2009-08-18 12:32:25 -04:00
|
|
|
'Prefix' => options['prefix'] || ''
|
2009-08-08 15:31:32 -04:00
|
|
|
}
|
2009-08-18 12:32:25 -04:00
|
|
|
if options['max-keys'] && options['max-keys'] < response.body['Contents'].length
|
|
|
|
response.body['IsTruncated'] = true
|
|
|
|
response.body['Contents'] = response.body['Contents'][0...options['max-keys']]
|
|
|
|
end
|
2009-08-16 16:31:36 -04:00
|
|
|
else
|
|
|
|
response.status = 404
|
2009-11-20 14:08:08 -05:00
|
|
|
raise(Excon::Errors.status_error({:expects => 200}, response))
|
2009-08-08 15:31:32 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|