2010-03-13 16:37:24 -05:00
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class AWS
|
2010-03-13 16:37:24 -05:00
|
|
|
class Real
|
2009-08-08 15:31:32 -04:00
|
|
|
|
2011-08-24 14:50:42 -04:00
|
|
|
require 'fog/aws/parsers/storage/get_bucket'
|
2010-06-12 18:31:17 -04:00
|
|
|
|
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 {}.
|
2010-05-12 21:40:00 -04:00
|
|
|
# * 'delimiter'<~String> - causes keys with the same string between the prefix
|
|
|
|
# value and the first occurence of delimiter to be rolled up
|
2009-08-08 15:31:32 -04:00
|
|
|
# * 'marker'<~String> - limits object keys to only those that appear
|
|
|
|
# lexicographically after its value.
|
|
|
|
# * 'max-keys'<~Integer> - limits number of object keys returned
|
2010-05-12 21:40:00 -04:00
|
|
|
# * 'prefix'<~String> - limits object keys to those beginning with its value.
|
2009-08-08 15:31:32 -04:00
|
|
|
#
|
|
|
|
# ==== 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
|
2010-05-12 21:40:00 -04:00
|
|
|
# * 'IsTruncated'<~Boolean> - Whether or not the listing is truncated
|
2009-08-08 15:31:32 -04:00
|
|
|
# * '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
|
2010-11-19 17:09:06 -05:00
|
|
|
# * 'CommonPrefixes'<~Array> - Array of strings for common prefixes
|
2009-08-08 15:31:32 -04:00
|
|
|
# * '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
|
2010-05-12 21:40:00 -04:00
|
|
|
#
|
2010-10-29 21:05:59 -04:00
|
|
|
# ==== See Also
|
|
|
|
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html
|
|
|
|
|
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-08-08 15:31:32 -04:00
|
|
|
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',
|
2011-06-15 17:26:43 -04:00
|
|
|
:parser => Fog::Parsers::Storage::AWS::GetBucket.new,
|
2010-06-05 17:19:39 -04:00
|
|
|
:query => options
|
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-10-29 21:16:36 -04:00
|
|
|
class Mock # :nodoc:all
|
2009-08-08 15:31:32 -04:00
|
|
|
|
|
|
|
def get_bucket(bucket_name, options = {})
|
2011-07-30 05:25:29 -04:00
|
|
|
prefix, marker, delimiter, max_keys = \
|
|
|
|
options['prefix'], options['marker'], options['delimiter'], options['max-keys']
|
2011-08-03 15:21:24 -04:00
|
|
|
common_prefixes = []
|
2011-07-30 05:25:29 -04:00
|
|
|
|
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
|
2011-05-19 18:35:33 -04:00
|
|
|
if bucket = self.data[:buckets][bucket_name]
|
2012-02-08 12:39:14 -05:00
|
|
|
contents = bucket[:objects].values.collect(&:first).sort {|x,y| x['Key'] <=> y['Key']}.reject do |object|
|
2011-07-30 05:25:29 -04:00
|
|
|
(prefix && object['Key'][0...prefix.length] != prefix) ||
|
|
|
|
(marker && object['Key'] <= marker) ||
|
2011-08-03 15:21:24 -04:00
|
|
|
(delimiter && object['Key'][(prefix ? prefix.length : 0)..-1].include?(delimiter) \
|
2011-12-15 14:26:08 -05:00
|
|
|
&& common_prefixes << object['Key'].sub(/^(#{prefix}[^#{delimiter}]+.).*/, '\1')) ||
|
|
|
|
object.has_key?(:delete_marker)
|
2010-08-18 15:23:42 -04:00
|
|
|
end.map do |object|
|
2011-02-02 19:27:22 -05:00
|
|
|
data = object.reject {|key, value| !['ETag', 'Key', 'StorageClass'].include?(key)}
|
2010-08-18 15:23:42 -04:00
|
|
|
data.merge!({
|
2011-02-02 19:27:22 -05:00
|
|
|
'LastModified' => Time.parse(object['Last-Modified']),
|
2010-08-18 15:23:42 -04:00
|
|
|
'Owner' => bucket['Owner'],
|
2011-02-02 19:27:22 -05:00
|
|
|
'Size' => object['Content-Length'].to_i
|
2010-08-18 15:23:42 -04:00
|
|
|
})
|
|
|
|
data
|
|
|
|
end
|
2011-07-30 05:25:29 -04:00
|
|
|
max_keys = max_keys || 1000
|
2010-08-18 15:23:42 -04:00
|
|
|
size = [max_keys, 1000].min
|
|
|
|
truncated_contents = contents[0...size]
|
|
|
|
|
2009-08-08 15:31:32 -04:00
|
|
|
response.status = 200
|
|
|
|
response.body = {
|
2011-08-03 15:21:24 -04:00
|
|
|
'CommonPrefixes' => common_prefixes.uniq,
|
2010-12-04 18:46:19 -05:00
|
|
|
'Contents' => truncated_contents,
|
|
|
|
'IsTruncated' => truncated_contents.size != contents.size,
|
2011-07-30 05:25:29 -04:00
|
|
|
'Marker' => marker,
|
2010-12-04 18:46:19 -05:00
|
|
|
'MaxKeys' => max_keys,
|
|
|
|
'Name' => bucket['Name'],
|
2011-07-30 05:25:29 -04:00
|
|
|
'Prefix' => prefix
|
2009-08-08 15:31:32 -04:00
|
|
|
}
|
2011-07-30 05:25:29 -04:00
|
|
|
if max_keys && max_keys < response.body['Contents'].length
|
2009-08-18 12:32:25 -04:00
|
|
|
response.body['IsTruncated'] = true
|
2011-07-30 05:25:29 -04:00
|
|
|
response.body['Contents'] = response.body['Contents'][0...max_keys]
|
2009-08-18 12:32:25 -04:00
|
|
|
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
|