Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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
  • Loading branch information...
commit 7e74c5cb232826231e16ec9774b2a9c7b0aaee26 1 parent 6cd7566
@halorgium halorgium authored geemus committed
View
1  lib/fog/aws/models/s3/files.rb
@@ -30,6 +30,7 @@ def all(options = {})
options
)
if parent
+ merge_attributes(parent.files.attributes)
load(parent.files.map {|file| file.attributes})
else
nil
View
33 lib/fog/aws/requests/s3/get_bucket.rb
@@ -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']
}
View
24 spec/aws/models/s3/files_spec.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.