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
|
|
|
|
#
|
2013-01-02 20:55:08 -05:00
|
|
|
# @param bucket_name [String] name of bucket to list object keys from
|
|
|
|
# @param options [Hash] config arguments for list. Defaults to {}.
|
|
|
|
# @option options delimiter [String] causes keys with the same string between the prefix
|
2010-05-12 21:40:00 -04:00
|
|
|
# value and the first occurence of delimiter to be rolled up
|
2013-01-02 20:55:08 -05:00
|
|
|
# @option options marker [String] limits object keys to only those that appear
|
2009-08-08 15:31:32 -04:00
|
|
|
# lexicographically after its value.
|
2013-01-02 20:55:08 -05:00
|
|
|
# @option options max-keys [Integer] limits number of object keys returned
|
|
|
|
# @option options prefix [String] limits object keys to those beginning with its value.
|
2009-08-08 15:31:32 -04:00
|
|
|
#
|
2013-01-02 20:55:08 -05:00
|
|
|
# @return [Excon::Response] response:
|
|
|
|
# * body [Hash]:
|
|
|
|
# * Delimeter [String] - Delimiter specified for query
|
|
|
|
# * IsTruncated [Boolean] - Whether or not the listing is truncated
|
|
|
|
# * 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
|
|
|
|
# * CommonPrefixes [Array] - Array of strings for common prefixes
|
|
|
|
# * 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
|
|
|
#
|
2013-01-02 20:55:08 -05:00
|
|
|
# @see http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html
|
2010-10-29 21:05:59 -04:00
|
|
|
|
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 => {},
|
2013-04-12 06:31:10 -04:00
|
|
|
:bucket_name => bucket_name,
|
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
|