2009-08-02 19:37:54 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class S3
|
|
|
|
|
|
|
|
class Object < Fog::Model
|
|
|
|
|
2009-08-14 12:19:40 -04:00
|
|
|
attr_accessor :body,
|
2009-08-28 12:03:19 -04:00
|
|
|
:content_length,
|
|
|
|
:content_type,
|
|
|
|
:etag,
|
|
|
|
:key,
|
|
|
|
:last_modified,
|
|
|
|
:owner,
|
|
|
|
:size,
|
|
|
|
:storage_class
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2009-08-04 13:51:54 -04:00
|
|
|
def initialize(attributes = {})
|
|
|
|
remap_attributes(attributes, {
|
2009-08-04 23:09:08 -04:00
|
|
|
'Content-Length' => :content_length,
|
|
|
|
'ETag' => :etag,
|
|
|
|
'Key' => :key,
|
|
|
|
'LastModified' => :last_modified,
|
|
|
|
'Last-Modified' => :last_modified,
|
|
|
|
'Size' => :size,
|
|
|
|
'StorageClass' => :storage_class
|
2009-08-04 13:51:54 -04:00
|
|
|
})
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-08-27 23:47:01 -04:00
|
|
|
def bucket
|
|
|
|
@bucket
|
|
|
|
end
|
|
|
|
|
2009-08-04 23:09:08 -04:00
|
|
|
def copy(target_bucket_name, target_object_key)
|
2009-08-17 20:21:43 -04:00
|
|
|
data = connection.copy_object(bucket.name, key, target_bucket_name, target_object_key).body
|
2009-08-30 17:14:11 -04:00
|
|
|
target_bucket = connection.buckets.new(:name => target_bucket_name)
|
|
|
|
target_object = target_bucket.objects.new(
|
|
|
|
:body => body,
|
|
|
|
:content_length => content_length,
|
|
|
|
:content_type => content_type,
|
|
|
|
:etag => etag,
|
|
|
|
:key => key,
|
|
|
|
:last_modified => last_modified,
|
|
|
|
:owner => owner,
|
|
|
|
:size => size,
|
|
|
|
:storage_class => storage_class
|
|
|
|
)
|
2009-08-04 23:09:08 -04:00
|
|
|
copy_data = {}
|
|
|
|
for key, value in data
|
|
|
|
if ['ETag', 'LastModified'].include?(key)
|
|
|
|
copy_data[key] = value
|
|
|
|
end
|
|
|
|
end
|
2009-08-30 17:14:11 -04:00
|
|
|
target_object.merge_attributes(copy_data)
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2009-08-30 17:14:11 -04:00
|
|
|
def destroy
|
2009-08-04 23:09:08 -04:00
|
|
|
connection.delete_object(bucket, key)
|
2009-08-30 17:14:11 -04:00
|
|
|
objects.delete(key)
|
2009-08-27 23:47:01 -04:00
|
|
|
true
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2009-08-30 17:14:11 -04:00
|
|
|
def new_record?
|
|
|
|
objects.key?(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
objects.get(key)
|
|
|
|
end
|
|
|
|
|
2009-08-04 23:09:08 -04:00
|
|
|
def save(options = {})
|
2009-08-17 20:21:43 -04:00
|
|
|
data = connection.put_object(bucket.name, key, body, options)
|
2009-08-04 23:09:08 -04:00
|
|
|
@etag = data.headers['ETag']
|
2009-08-29 14:20:54 -04:00
|
|
|
objects[key] = self
|
2009-08-27 23:47:01 -04:00
|
|
|
true
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def bucket=(new_bucket)
|
|
|
|
@bucket = new_bucket
|
|
|
|
end
|
|
|
|
|
2009-08-28 12:03:19 -04:00
|
|
|
def objects
|
|
|
|
@objects
|
|
|
|
end
|
|
|
|
|
|
|
|
def objects=(new_objects)
|
|
|
|
@objects = new_objects
|
|
|
|
end
|
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|