mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
first pass at stubbing out the rest of s3 object api
This commit is contained in:
parent
2ef7546567
commit
72c9e6e888
5 changed files with 114 additions and 15 deletions
|
@ -32,18 +32,20 @@ module Fog
|
|||
bucket = Fog::AWS::S3::Bucket.new({
|
||||
:connection => connection
|
||||
})
|
||||
objects = {}
|
||||
objects_data = {}
|
||||
for key, value in data
|
||||
if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
|
||||
objects[key] = value
|
||||
objects_data[key] = value
|
||||
end
|
||||
end
|
||||
bucket.objects = Fog::AWS::S3::Objects.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection
|
||||
}.merge!(objects))
|
||||
}.merge!(objects_data))
|
||||
data['Contents'].each do |object|
|
||||
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
|
||||
bucket.objects << Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:owner => owner
|
||||
}.merge!(object))
|
||||
|
|
|
@ -4,19 +4,58 @@ module Fog
|
|||
|
||||
class Object < Fog::Model
|
||||
|
||||
attr_accessor :etag, :key, :last_modified, :owner, :size, :storage_class
|
||||
attr_accessor :body, :content_length, :etag, :key, :last_modified, :owner, :size, :storage_class
|
||||
|
||||
def initialize(attributes = {})
|
||||
remap_attributes(attributes, {
|
||||
'ETag' => :etag,
|
||||
'Key' => :key,
|
||||
'LastModified' => :last_modified,
|
||||
'Size' => :size,
|
||||
'StorageClass' => :storage_class
|
||||
'Content-Length' => :content_length,
|
||||
'ETag' => :etag,
|
||||
'Key' => :key,
|
||||
'LastModified' => :last_modified,
|
||||
'Last-Modified' => :last_modified,
|
||||
'Size' => :size,
|
||||
'StorageClass' => :storage_class
|
||||
})
|
||||
super
|
||||
end
|
||||
|
||||
def copy(target_bucket_name, target_object_key)
|
||||
data = connection.copy_object(bucket, key, target_bucket_name, target_object_key).body
|
||||
copy = self.dup
|
||||
copy_data = {}
|
||||
for key, value in data
|
||||
if ['ETag', 'LastModified'].include?(key)
|
||||
copy_data[key] = value
|
||||
end
|
||||
end
|
||||
copy.update_attributes(copy_data)
|
||||
copy
|
||||
end
|
||||
|
||||
def delete
|
||||
return false if new_record?
|
||||
connection.delete_object(bucket, key)
|
||||
@new_record = true
|
||||
true
|
||||
end
|
||||
|
||||
def save(options = {})
|
||||
data = connection.put_object(bucket, key, body, options)
|
||||
@etag = data.headers['ETag']
|
||||
@new_record = false
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def bucket=(new_bucket)
|
||||
@bucket = new_bucket
|
||||
end
|
||||
|
||||
def bucket
|
||||
@bucket
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -16,6 +16,56 @@ module Fog
|
|||
super
|
||||
end
|
||||
|
||||
def create(attributes = {})
|
||||
object = new(attributes)
|
||||
object.save
|
||||
object
|
||||
end
|
||||
|
||||
def get(key, options = {})
|
||||
data = connection.get_object(bucket, key, options)
|
||||
object_data = {}
|
||||
for key, value in data.headers
|
||||
if ['Content-Length', 'ETag', 'Last-Modified'].include?(key)
|
||||
object_data[key] = value
|
||||
end
|
||||
end
|
||||
object = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:body => data.body,
|
||||
:connection => connection
|
||||
}.merge!(object_data))
|
||||
end
|
||||
|
||||
def head(key, options = {})
|
||||
data = connection.head_object(bucket, key, options)
|
||||
object_data = {}
|
||||
for key, value in data.headers
|
||||
if ['Content-Length', 'ETag', 'Last-Modified'].include?(key)
|
||||
object_data[key] = value
|
||||
end
|
||||
end
|
||||
object = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection
|
||||
}.merge!(object_data))
|
||||
end
|
||||
|
||||
def new(attributes = {})
|
||||
Fog::AWS::S3::Object.new(attributes.merge!(:connection => connection))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def bucket=(new_bucket)
|
||||
@bucket = new_bucket
|
||||
end
|
||||
|
||||
def bucket
|
||||
@bucket
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -2,9 +2,7 @@ module Fog
|
|||
class Collection < Array
|
||||
|
||||
def initialize(attributes = {})
|
||||
for key, value in attributes
|
||||
send(:"#{key}=", value)
|
||||
end
|
||||
update_attributes(attributes)
|
||||
end
|
||||
|
||||
def inspect
|
||||
|
@ -20,6 +18,12 @@ module Fog
|
|||
data << "]>"
|
||||
end
|
||||
|
||||
def update_attributes(attributes = {})
|
||||
for key, value in attributes
|
||||
send(:"#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection=(new_connection)
|
||||
|
|
|
@ -2,9 +2,7 @@ module Fog
|
|||
class Model
|
||||
|
||||
def initialize(attributes = {})
|
||||
for key, value in attributes
|
||||
send(:"#{key}=", value)
|
||||
end
|
||||
update_attributes(attributes)
|
||||
end
|
||||
|
||||
def inspect
|
||||
|
@ -15,6 +13,12 @@ module Fog
|
|||
data << ">"
|
||||
end
|
||||
|
||||
def update_attributes(attributes = {})
|
||||
for key, value in attributes
|
||||
send(:"#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def connection=(new_connection)
|
||||
|
|
Loading…
Reference in a new issue