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