1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/aws/models/s3/files.rb

89 lines
2.3 KiB
Ruby
Raw Normal View History

2010-03-13 16:37:24 -05:00
require 'fog/collection'
require 'fog/aws/models/s3/file'
2009-08-02 19:37:54 -04:00
module Fog
module AWS
2010-03-13 16:37:24 -05:00
module S3
2009-08-02 19:37:54 -04:00
2010-01-14 23:44:39 -05:00
class Files < Fog::Collection
2009-08-02 19:37:54 -04:00
attribute :delimiter, 'Delimiter'
attribute :directory
attribute :is_truncated, 'IsTruncated'
attribute :marker, 'Marker'
2010-04-02 13:40:58 -04:00
attribute :max_keys, ['MaxKeys', 'max-keys']
attribute :prefix, 'Prefix'
2009-08-04 13:51:54 -04:00
2010-01-14 23:44:39 -05:00
model Fog::AWS::S3::File
def all(options = {})
options = {
'delimiter' => @delimiter,
'marker' => @marker,
'max-keys' => @max_keys,
'prefix' => @prefix
}.merge!(options)
options = options.reject {|key,value| value.nil? || value.to_s.empty?}
2010-01-05 01:03:24 -05:00
merge_attributes(options)
2010-03-08 19:19:48 -05:00
parent = directory.collection.get(
directory.key,
options
)
2010-03-08 19:19:48 -05:00
if parent
load(parent.files.map {|file| file.attributes})
2010-01-05 01:03:24 -05:00
else
nil
end
end
def get(key, options = {}, &block)
data = connection.get_object(directory.key, key, options, &block)
2010-01-14 23:44:39 -05:00
file_data = {
:body => data.body,
:key => key
}
for key, value in data.headers
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
2010-01-14 23:44:39 -05:00
file_data[key] = value
end
end
2010-01-14 23:44:39 -05:00
new(file_data)
rescue Excon::Errors::NotFound
nil
end
def get_url(key, expires)
connection.get_object_url(directory.key, key, expires)
end
def head(key, options = {})
data = connection.head_object(directory.key, key, options)
2010-01-14 23:44:39 -05:00
file_data = {
:key => key
}
for key, value in data.headers
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
2010-01-14 23:44:39 -05:00
file_data[key] = value
end
end
2010-01-14 23:44:39 -05:00
new(file_data)
rescue Excon::Errors::NotFound
nil
end
def new(attributes = {})
2010-01-14 23:44:39 -05:00
super({ :directory => directory }.merge!(attributes))
end
private
2010-01-14 23:44:39 -05:00
def directory=(new_directory)
@directory = new_directory
end
2009-08-02 19:37:54 -04:00
end
end
end
end