2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/collection'
|
2011-08-24 14:50:42 -04:00
|
|
|
require 'fog/aws/models/storage/file'
|
2010-03-13 16:37:24 -05:00
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class AWS
|
2010-09-02 19:01:19 -04:00
|
|
|
|
2010-01-14 23:44:39 -05:00
|
|
|
class Files < Fog::Collection
|
2011-06-27 17:50:32 -04:00
|
|
|
extend Fog::Deprecation
|
|
|
|
deprecate :get_url, :get_https_url
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2010-11-19 17:09:06 -05:00
|
|
|
attribute :common_prefixes, :aliases => 'CommonPrefixes'
|
|
|
|
attribute :delimiter, :aliases => 'Delimiter'
|
2010-04-26 20:04:47 -04:00
|
|
|
attribute :directory
|
2010-11-19 17:09:06 -05:00
|
|
|
attribute :is_truncated, :aliases => 'IsTruncated'
|
|
|
|
attribute :marker, :aliases => 'Marker'
|
|
|
|
attribute :max_keys, :aliases => ['MaxKeys', 'max-keys']
|
|
|
|
attribute :prefix, :aliases => 'Prefix'
|
2009-08-04 13:51:54 -04:00
|
|
|
|
2011-06-15 17:26:43 -04:00
|
|
|
model Fog::Storage::AWS::File
|
2009-10-30 02:35:28 -04:00
|
|
|
|
2009-08-29 14:20:54 -04:00
|
|
|
def all(options = {})
|
2010-09-02 19:01:19 -04:00
|
|
|
requires :directory
|
2010-03-31 00:04:27 -04:00
|
|
|
options = {
|
2010-11-19 16:45:45 -05:00
|
|
|
'delimiter' => delimiter,
|
|
|
|
'marker' => marker,
|
|
|
|
'max-keys' => max_keys,
|
|
|
|
'prefix' => prefix
|
2010-03-31 00:04:27 -04:00
|
|
|
}.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(
|
2010-05-03 18:02:08 -04:00
|
|
|
directory.key,
|
2009-12-24 19:10:14 -05:00
|
|
|
options
|
2009-12-14 13:28:43 -05:00
|
|
|
)
|
2010-03-08 19:19:48 -05:00
|
|
|
if parent
|
2010-08-18 15:23:42 -04:00
|
|
|
merge_attributes(parent.files.attributes)
|
2010-03-08 19:19:48 -05:00
|
|
|
load(parent.files.map {|file| file.attributes})
|
2010-01-05 01:03:24 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2011-04-26 18:13:56 -04:00
|
|
|
alias :each_file_this_page :each
|
2011-04-19 17:02:36 -04:00
|
|
|
def each
|
2011-04-26 18:13:56 -04:00
|
|
|
if !block_given?
|
|
|
|
self
|
|
|
|
else
|
|
|
|
subset = dup.all
|
|
|
|
|
|
|
|
subset.each_file_this_page {|f| yield f}
|
|
|
|
while subset.is_truncated
|
|
|
|
subset = subset.all(:marker => subset.last.key)
|
|
|
|
subset.each_file_this_page {|f| yield f}
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
2011-04-19 17:02:36 -04:00
|
|
|
end
|
|
|
|
|
2009-09-10 14:30:50 -04:00
|
|
|
def get(key, options = {}, &block)
|
2010-09-02 19:01:19 -04:00
|
|
|
requires :directory
|
2010-05-03 18:02:08 -04:00
|
|
|
data = connection.get_object(directory.key, key, options, &block)
|
2010-11-10 15:46:50 -05:00
|
|
|
file_data = data.headers.merge({
|
2009-09-02 23:17:53 -04:00
|
|
|
:body => data.body,
|
|
|
|
:key => key
|
2010-11-10 15:46:50 -05:00
|
|
|
})
|
2011-01-19 13:10:47 -05:00
|
|
|
normalise_headers(file_data)
|
2010-01-14 23:44:39 -05:00
|
|
|
new(file_data)
|
2011-05-27 14:07:31 -04:00
|
|
|
rescue Excon::Errors::NotFound => error
|
|
|
|
case error.message
|
|
|
|
when /<Code>NoSuchKey<\/Code>/
|
|
|
|
nil
|
|
|
|
when /<Code>NoSuchBucket<\/Code>/
|
2011-06-15 17:26:43 -04:00
|
|
|
raise(Fog::Storage::AWS::NotFound.new("Directory #{directory.identity} does not exist."))
|
2011-05-27 14:07:31 -04:00
|
|
|
else
|
|
|
|
raise(error)
|
|
|
|
end
|
2009-08-19 00:26:13 -04:00
|
|
|
end
|
|
|
|
|
2011-06-27 17:50:32 -04:00
|
|
|
def get_http_url(key, expires)
|
2010-09-02 19:01:19 -04:00
|
|
|
requires :directory
|
2011-06-27 17:50:32 -04:00
|
|
|
connection.get_object_http_url(directory.key, key, expires)
|
|
|
|
end
|
|
|
|
|
2011-12-09 06:42:53 -05:00
|
|
|
def get_https_url(key, expires, options = {})
|
2011-06-27 17:50:32 -04:00
|
|
|
requires :directory
|
2011-12-09 06:42:53 -05:00
|
|
|
connection.get_object_https_url(directory.key, key, expires, options)
|
2009-10-03 18:43:19 -04:00
|
|
|
end
|
|
|
|
|
2009-08-29 14:20:54 -04:00
|
|
|
def head(key, options = {})
|
2010-09-02 19:01:19 -04:00
|
|
|
requires :directory
|
2010-05-03 18:02:08 -04:00
|
|
|
data = connection.head_object(directory.key, key, options)
|
2010-11-10 15:46:50 -05:00
|
|
|
file_data = data.headers.merge({
|
2009-09-02 23:17:53 -04:00
|
|
|
:key => key
|
2010-11-10 15:46:50 -05:00
|
|
|
})
|
2011-01-19 13:10:47 -05:00
|
|
|
normalise_headers(file_data)
|
2010-01-14 23:44:39 -05:00
|
|
|
new(file_data)
|
2009-11-08 15:16:52 -05:00
|
|
|
rescue Excon::Errors::NotFound
|
2009-09-02 23:17:53 -04:00
|
|
|
nil
|
2009-08-19 00:26:13 -04:00
|
|
|
end
|
|
|
|
|
2009-08-04 23:09:08 -04:00
|
|
|
def new(attributes = {})
|
2010-09-02 19:01:19 -04:00
|
|
|
requires :directory
|
2010-01-14 23:44:39 -05:00
|
|
|
super({ :directory => directory }.merge!(attributes))
|
2009-09-02 00:41:52 -04:00
|
|
|
end
|
|
|
|
|
2011-01-19 13:10:47 -05:00
|
|
|
def normalise_headers(headers)
|
|
|
|
headers['Last-Modified'] = Time.parse(headers['Last-Modified'])
|
|
|
|
headers['ETag'].gsub!('"','')
|
|
|
|
end
|
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|