2009-08-02 19:37:54 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class S3
|
|
|
|
|
|
|
|
class Object < Fog::Model
|
|
|
|
|
2009-08-30 17:43:01 -04:00
|
|
|
attribute :body
|
|
|
|
attribute :content_length, 'Content-Length'
|
|
|
|
attribute :content_type, 'Content-Type'
|
2009-09-02 00:41:52 -04:00
|
|
|
attribute :etag, ['Etag', 'ETag']
|
2009-08-30 17:43:01 -04:00
|
|
|
attribute :key, 'Key'
|
|
|
|
attribute :last_modified, ['Last-Modified', 'LastModified']
|
2009-09-02 00:41:52 -04:00
|
|
|
attribute :owner
|
2009-08-30 17:43:01 -04:00
|
|
|
attribute :size, 'Size'
|
|
|
|
attribute :storage_class, 'StorageClass'
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2009-08-04 13:51:54 -04:00
|
|
|
def initialize(attributes = {})
|
|
|
|
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-09-02 23:17:53 -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)
|
2009-09-02 23:17:53 -04:00
|
|
|
target_object = target_bucket.objects.new(attributes.merge!(:key => target_object_key))
|
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-09-02 23:17:53 -04:00
|
|
|
target_object
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2009-08-30 17:14:11 -04:00
|
|
|
def destroy
|
2009-09-02 23:17:53 -04:00
|
|
|
connection.delete_object(bucket.name, @key)
|
2009-08-27 23:47:01 -04:00
|
|
|
true
|
2009-09-02 23:17:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def objects
|
|
|
|
@objects
|
2009-08-30 17:14:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
2009-09-02 23:17:53 -04:00
|
|
|
new_attributes = objects.get(@key).attributes
|
2009-09-02 00:41:52 -04:00
|
|
|
merge_attributes(new_attributes)
|
2009-08-30 17:14:11 -04:00
|
|
|
end
|
|
|
|
|
2009-08-04 23:09:08 -04:00
|
|
|
def save(options = {})
|
2009-09-02 23:17:53 -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-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=(new_objects)
|
|
|
|
@objects = new_objects
|
|
|
|
end
|
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|