mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[s3] Improve behavior of Files collection
* Make mock use max_keys and return IsTruncated correctly * Ensure that IsTruncated is passed into the model collection * Spec that max_keys is limited to 1000
This commit is contained in:
parent
6cd75661ab
commit
7e74c5cb23
3 changed files with 43 additions and 15 deletions
|
@ -30,6 +30,7 @@ module Fog
|
|||
options
|
||||
)
|
||||
if parent
|
||||
merge_attributes(parent.files.attributes)
|
||||
load(parent.files.map {|file| file.attributes})
|
||||
else
|
||||
nil
|
||||
|
|
|
@ -61,23 +61,28 @@ module Fog
|
|||
end
|
||||
response = Excon::Response.new
|
||||
if bucket = @data[:buckets][bucket_name]
|
||||
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|
|
||||
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Size', 'StorageClass'].include?(key)}
|
||||
data.merge!({
|
||||
'LastModified' => Time.parse(data['LastModified']),
|
||||
'Owner' => bucket['Owner'],
|
||||
'Size' => data['Size'].to_i
|
||||
})
|
||||
data
|
||||
end
|
||||
max_keys = options['max-keys'] || 1000
|
||||
size = [max_keys, 1000].min
|
||||
truncated_contents = contents[0...size]
|
||||
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'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|
|
||||
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Size', 'StorageClass'].include?(key)}
|
||||
data.merge!({
|
||||
'LastModified' => Time.parse(data['LastModified']),
|
||||
'Owner' => bucket['Owner'],
|
||||
'Size' => data['Size'].to_i
|
||||
})
|
||||
data
|
||||
end,
|
||||
'IsTruncated' => false,
|
||||
'Contents' => truncated_contents,
|
||||
'IsTruncated' => truncated_contents.size != contents.size,
|
||||
'Marker' => options['marker'],
|
||||
'MaxKeys' => options['max-keys'] || 1000,
|
||||
'MaxKeys' => max_keys,
|
||||
'Name' => bucket['Name'],
|
||||
'Prefix' => options['prefix']
|
||||
}
|
||||
|
|
|
@ -3,10 +3,13 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
|
|||
describe 'Fog::AWS::S3::Files' do
|
||||
|
||||
before(:each) do
|
||||
@directory = AWS[:s3].directories.create(:key => 'fogdirectoryname')
|
||||
@directory = AWS[:s3].directories.create(:key => "fog#{Time.now.to_f}")
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
until @directory.files.reload.empty?
|
||||
@directory.files.each {|file| file.destroy}
|
||||
end
|
||||
@directory.destroy
|
||||
end
|
||||
|
||||
|
@ -34,6 +37,25 @@ describe 'Fog::AWS::S3::Files' do
|
|||
directory.files.all.should be_nil
|
||||
end
|
||||
|
||||
it "should return 1000 files and report truncated" do
|
||||
1010.times do |n|
|
||||
@directory.files.create(:key => "file-#{n}")
|
||||
end
|
||||
response = @directory.files.all
|
||||
response.should have(1000).items
|
||||
response.is_truncated.should be_true
|
||||
end
|
||||
|
||||
it "should limit the max_keys to 1000" do
|
||||
1010.times do |n|
|
||||
@directory.files.create(:key => "file-#{n}")
|
||||
end
|
||||
response = @directory.files.all(:max_keys => 2000)
|
||||
response.should have(1000).items
|
||||
response.max_keys.should == 2000
|
||||
response.is_truncated.should be_true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#create" do
|
||||
|
|
Loading…
Add table
Reference in a new issue