1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

more fleshing out s3 models

This commit is contained in:
Wesley Beary 2009-08-30 14:14:11 -07:00
parent ae2298ee11
commit 19f3408ec7
5 changed files with 64 additions and 23 deletions

View file

@ -21,14 +21,17 @@ module Fog
@buckets
end
def delete
def destroy
connection.delete_bucket(name)
buckets.delete(name)
true
end
def location
data = s3.get_bucket_location(name)
data.body['LocationConstraint']
@location ||= begin
data = s3.get_bucket_location(name)
data.body['LocationConstraint']
end
end
def objects
@ -39,8 +42,10 @@ module Fog
end
def payer
data = connection.get_request_payment(name)
data.body['Payer']
@payer ||= begin
data = connection.get_request_payment(name)
data.body['Payer']
end
end
def payer=(new_payer)
@ -48,6 +53,14 @@ module Fog
@payer = new_payer
end
def new_record?
buckets.key?(name)
end
def reload
buckets.get(name)
end
def save
options = {}
if @location

View file

@ -17,15 +17,14 @@ module Fog
def all
data = connection.get_service.body
owner = Fog::AWS::S3::Owner.new(data.delete('Owner').merge!(:connection => connection))
buckets = Fog::AWS::S3::Buckets.new
data['Buckets'].each do |bucket|
buckets << Fog::AWS::S3::Bucket.new({
self[bucket['Name']] = Fog::AWS::S3::Bucket.new({
:buckets => buckets,
:connection => connection,
:owner => owner
}.merge!(bucket))
end
buckets
self
end
def create(attributes = {})
@ -43,9 +42,11 @@ module Fog
})
data = connection.get_bucket(name, options).body
bucket = Fog::AWS::S3::Bucket.new({
:buckets => self,
:connection => connection,
:name => data['Name']
})
self[bucket.name] = bucket
objects_data = {}
for key, value in data
if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
@ -58,7 +59,7 @@ module Fog
}.merge!(objects_data))
data['Contents'].each do |object|
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
objects << Fog::AWS::S3::Object.new({
objects[object['key']] = Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:objects => self,

View file

@ -33,22 +33,41 @@ module Fog
def copy(target_bucket_name, target_object_key)
data = connection.copy_object(bucket.name, key, target_bucket_name, target_object_key).body
copy = self.dup
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
)
copy_data = {}
for key, value in data
if ['ETag', 'LastModified'].include?(key)
copy_data[key] = value
end
end
copy.update_attributes(copy_data)
copy
target_object.merge_attributes(copy_data)
end
def delete
def destroy
connection.delete_object(bucket, key)
objects.delete(key)
true
end
def new_record?
objects.key?(key)
end
def reload
objects.get(key)
end
def save(options = {})
data = connection.put_object(bucket.name, key, body, options)
@etag = data.headers['ETag']

View file

@ -26,6 +26,18 @@ module Fog
end
def all(options = {})
options = {
:is_truncated => is_trucated,
:marker => marker,
:max_keys => max_keys,
:prefix => prefix
}.merge!(options)
remap_attributes(options, {
:is_truncated => 'IsTruncated',
:marker => 'Marker',
:max_keys => 'MaxKeys',
:prefix => 'Prefix'
})
bucket.buckets.get(bucket.name, options).objects
end
@ -47,7 +59,7 @@ module Fog
object_data[key] = value
end
end
Fog::AWS::S3::Object.new({
self[object_data['key']] = Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:objects => self
@ -62,7 +74,7 @@ module Fog
object_data[key] = value
end
end
Fog::AWS::S3::Object.new({
self[object_data['key']] = Fog::AWS::S3::Object.new({
:bucket => bucket,
:connection => connection,
:objects => self

View file

@ -1,8 +1,8 @@
module Fog
class Model
def initialize(attributes = {})
update_attributes(attributes)
def initialize(new_attributes = {})
merge_attributes(new_attributes)
end
def inspect
@ -13,8 +13,8 @@ module Fog
data << ">"
end
def update_attributes(attributes = {})
for key, value in attributes
def merge_attributes(new_attributes = {})
for key, value in new_attributes
send(:"#{key}=", value)
end
self
@ -30,10 +30,6 @@ module Fog
@connection
end
def new_record?
!defined?(@new_record) || @new_record
end
def remap_attributes(attributes, mapping)
for key, value in mapping
if attributes.key?(key)