Skip to content

Commit

Permalink
[s3] Improve behavior of Files collection
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
halorgium authored and geemus committed Aug 18, 2010
1 parent 6cd7566 commit 7e74c5c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/fog/aws/models/s3/files.rb
Expand Up @@ -30,6 +30,7 @@ def all(options = {})
options
)
if parent
merge_attributes(parent.files.attributes)
load(parent.files.map {|file| file.attributes})
else
nil
Expand Down
33 changes: 19 additions & 14 deletions lib/fog/aws/requests/s3/get_bucket.rb
Expand Up @@ -61,23 +61,28 @@ def get_bucket(bucket_name, options = {})
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']
}
Expand Down
24 changes: 23 additions & 1 deletion spec/aws/models/s3/files_spec.rb
Expand Up @@ -3,10 +3,13 @@
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

Expand Down Expand Up @@ -34,6 +37,25 @@
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
Expand Down

0 comments on commit 7e74c5c

Please sign in to comment.