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/objects.rb

92 lines
2.4 KiB
Ruby
Raw Normal View History

2009-08-02 19:37:54 -04:00
module Fog
module AWS
class S3
class Objects < Fog::Collection
attribute :is_truncated, 'IsTruncated'
attribute :marker, 'Marker'
attribute :max_keys, 'MaxKeys'
attribute :prefix, 'Prefix'
2009-08-04 13:51:54 -04:00
def initialize(attributes = {})
super
end
2009-08-02 19:37:54 -04:00
def all(options = {})
merge_attributes(options)
bucket.buckets.get(bucket.name, attributes).objects
end
def bucket
@bucket
end
def create(attributes = {})
object = new(attributes)
object.save
object
end
def get(key, options = {})
if self[key] && self[key].body
self[key]
else
self[key] ||= begin
data = connection.get_object(bucket.name, key, options)
object_data = { :body => data.body}
for key, value in data.headers
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
object_data[key] = value
end
end
self[object_data['key']] = Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:objects => self
}.merge!(object_data))
rescue Fog::Errors::NotFound
nil
end
end
end
def head(key, options = {})
self[key] ||= begin
data = connection.head_object(bucket.name, key, options)
object_data = {}
for key, value in data.headers
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
object_data[key] = value
end
end
self[object_data['key']] = Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:objects => self
}.merge!(object_data))
rescue Fog::Errors::NotFound
nil
end
end
def new(attributes = {})
2009-08-17 20:06:25 -04:00
Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:objects => self
2009-08-17 20:06:25 -04:00
}.merge!(attributes))
end
private
def bucket=(new_bucket)
@bucket = new_bucket
end
2009-08-02 19:37:54 -04:00
end
end
end
end