more fleshing out of the models, getting close to feature complete for s3
This commit is contained in:
parent
d4d8e8684c
commit
0a80fbf01c
|
@ -33,10 +33,12 @@ module Fog
|
|||
end
|
||||
|
||||
def objects
|
||||
Fog::AWS::S3::Objects.new(
|
||||
:bucket => self,
|
||||
:connection => connection
|
||||
)
|
||||
@objects ||= begin
|
||||
Fog::AWS::S3::Objects.new(
|
||||
:bucket => self,
|
||||
:connection => connection
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def payer
|
||||
|
@ -52,7 +54,8 @@ module Fog
|
|||
end
|
||||
|
||||
def reload
|
||||
buckets.get(name)
|
||||
new_attributes = buckets.get(name).attributes
|
||||
merge_attributes(new_attributes)
|
||||
end
|
||||
|
||||
def save
|
||||
|
|
|
@ -11,15 +11,15 @@ 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(:connection => connection)
|
||||
self.delete_if {true}
|
||||
data['Buckets'].each do |bucket|
|
||||
buckets[bucket['Name']] = Fog::AWS::S3::Bucket.new({
|
||||
:buckets => buckets,
|
||||
self[bucket['Name']] = Fog::AWS::S3::Bucket.new({
|
||||
:buckets => self,
|
||||
:connection => connection,
|
||||
:owner => owner
|
||||
}.merge!(bucket))
|
||||
end
|
||||
buckets
|
||||
self
|
||||
end
|
||||
|
||||
def create(attributes = {})
|
||||
|
@ -29,43 +29,38 @@ module Fog
|
|||
end
|
||||
|
||||
def get(name, options = {})
|
||||
self[name] ||= begin
|
||||
remap_attributes(options, {
|
||||
:is_truncated => 'IsTruncated',
|
||||
:marker => 'Marker',
|
||||
:max_keys => 'MaxKeys',
|
||||
:prefix => 'Prefix'
|
||||
})
|
||||
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)
|
||||
objects_data[key] = value
|
||||
end
|
||||
remap_attributes(options, {
|
||||
:is_truncated => 'IsTruncated',
|
||||
:marker => 'Marker',
|
||||
:max_keys => 'MaxKeys',
|
||||
:prefix => 'Prefix'
|
||||
})
|
||||
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)
|
||||
objects_data[key] = value
|
||||
end
|
||||
objects = Fog::AWS::S3::Objects.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection
|
||||
}.merge!(objects_data))
|
||||
data['Contents'].each do |object|
|
||||
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
|
||||
objects[object['key']] = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:objects => self,
|
||||
:owner => owner
|
||||
}.merge!(object))
|
||||
end
|
||||
bucket
|
||||
rescue Fog::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
bucket.objects.merge_attributes(objects_data)
|
||||
data['Contents'].each do |object|
|
||||
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
|
||||
bucket.objects[object['key']] = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:objects => self,
|
||||
:owner => owner
|
||||
}.merge!(object))
|
||||
end
|
||||
bucket
|
||||
rescue Fog::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
def new(attributes = {})
|
||||
|
@ -77,6 +72,10 @@ module Fog
|
|||
)
|
||||
end
|
||||
|
||||
def reload
|
||||
all
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -7,9 +7,10 @@ module Fog
|
|||
attribute :body
|
||||
attribute :content_length, 'Content-Length'
|
||||
attribute :content_type, 'Content-Type'
|
||||
attribute :etag, 'Etag'
|
||||
attribute :etag, ['Etag', 'ETag']
|
||||
attribute :key, 'Key'
|
||||
attribute :last_modified, ['Last-Modified', 'LastModified']
|
||||
attribute :owner
|
||||
attribute :size, 'Size'
|
||||
attribute :storage_class, 'StorageClass'
|
||||
|
||||
|
@ -35,7 +36,7 @@ module Fog
|
|||
end
|
||||
|
||||
def destroy
|
||||
connection.delete_object(bucket, key)
|
||||
connection.delete_object(bucket.name, key)
|
||||
objects.delete(key)
|
||||
true
|
||||
rescue Fog::Errors::NotFound
|
||||
|
@ -43,7 +44,8 @@ module Fog
|
|||
end
|
||||
|
||||
def reload
|
||||
objects.get(key)
|
||||
new_attributes = objects.get(key).attributes
|
||||
merge_attributes(new_attributes)
|
||||
end
|
||||
|
||||
def save(options = {})
|
||||
|
|
|
@ -15,7 +15,12 @@ module Fog
|
|||
|
||||
def all(options = {})
|
||||
merge_attributes(options)
|
||||
bucket.buckets.get(bucket.name, attributes).objects
|
||||
self.delete_if {true}
|
||||
objects = bucket.buckets.get(bucket.name, attributes).objects
|
||||
objects.keys.each do |key|
|
||||
self[key] = objects[key]
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def bucket
|
||||
|
@ -29,26 +34,20 @@ module Fog
|
|||
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
|
||||
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
|
||||
|
||||
def head(key, options = {})
|
||||
|
@ -78,6 +77,10 @@ module Fog
|
|||
}.merge!(attributes))
|
||||
end
|
||||
|
||||
def reload
|
||||
all
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def bucket=(new_bucket)
|
||||
|
|
|
@ -23,14 +23,6 @@ module Fog
|
|||
merge_attributes(new_attributes)
|
||||
end
|
||||
|
||||
def attributes
|
||||
attributes = {}
|
||||
for attribute in self.class.attributes
|
||||
attributes[attribute] = send(:"#{attribute}")
|
||||
end
|
||||
attributes
|
||||
end
|
||||
|
||||
def inspect
|
||||
data = "#<#{self.class.name}"
|
||||
for attribute in self.class.attributes
|
||||
|
@ -39,6 +31,14 @@ module Fog
|
|||
data << ">"
|
||||
end
|
||||
|
||||
def attributes
|
||||
attributes = {}
|
||||
for attribute in self.class.attributes
|
||||
attributes[attribute] = send(:"#{attribute}")
|
||||
end
|
||||
attributes
|
||||
end
|
||||
|
||||
def merge_attributes(new_attributes = {})
|
||||
for key, value in new_attributes
|
||||
if aliased_key = self.class.aliases[key]
|
||||
|
|
|
@ -4,13 +4,9 @@ describe 'Fog::AWS::S3::Bucket' do
|
|||
|
||||
describe "#initialize" do
|
||||
|
||||
it "should return a Fog:AWS::S3::Bucket" do
|
||||
s3.buckets.new.should be_an(Fog::AWS::S3::Bucket)
|
||||
end
|
||||
|
||||
it "should remap attributes from parser" do
|
||||
now = Time.now
|
||||
bucket = s3.buckets.new(
|
||||
bucket = Fog::AWS::S3::Bucket.new(
|
||||
'CreationDate' => now,
|
||||
'Name' => 'bucketname'
|
||||
)
|
||||
|
@ -26,6 +22,11 @@ describe 'Fog::AWS::S3::Bucket' do
|
|||
s3.buckets.new.buckets.should be_a(Fog::AWS::S3::Buckets)
|
||||
end
|
||||
|
||||
it "should be the buckets the bucket is related to" do
|
||||
buckets = s3.buckets
|
||||
buckets.new.buckets.should == buckets
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#destroy" do
|
||||
|
|
|
@ -8,6 +8,12 @@ describe 'Fog::AWS::S3::Buckets' do
|
|||
s3.buckets.all.should be_a(Fog::AWS::S3::Buckets)
|
||||
end
|
||||
|
||||
it "should include persisted buckets" do
|
||||
bucket = s3.buckets.create(:name => 'fogbucketname')
|
||||
s3.buckets.all.keys.should include('fogbucketname')
|
||||
bucket.destroy
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#create" do
|
||||
|
@ -62,4 +68,12 @@ describe 'Fog::AWS::S3::Buckets' do
|
|||
|
||||
end
|
||||
|
||||
describe "#reload" do
|
||||
|
||||
it "should return a Fog::AWS::S3::Buckets" do
|
||||
s3.buckets.all.should be_a(Fog::AWS::S3::Buckets)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ describe 'S3::Object' do
|
|||
|
||||
describe "#initialize" do
|
||||
|
||||
it "should return an S3::Object"
|
||||
it "should remap attributes from parser"
|
||||
|
||||
end
|
||||
|
||||
|
@ -32,6 +32,12 @@ describe 'S3::Object' do
|
|||
|
||||
end
|
||||
|
||||
describe "#reload" do
|
||||
|
||||
it "should reload from s3"
|
||||
|
||||
end
|
||||
|
||||
describe "#save" do
|
||||
|
||||
it "should return the success value"
|
||||
|
|
|
@ -1,46 +1,99 @@
|
|||
require File.dirname(__FILE__) + '/../../../spec_helper'
|
||||
|
||||
describe 'S3::Objects' do
|
||||
describe 'Fog::AWS::S3::Objects' do
|
||||
|
||||
before(:each) do
|
||||
@bucket = s3.buckets.create(:name => 'fogbucketname')
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
@bucket.destroy
|
||||
end
|
||||
|
||||
describe "#initialize" do
|
||||
|
||||
it "should return an S3::Objects"
|
||||
it "should remap attributes from parser" do
|
||||
objects = Fog::AWS::S3::Objects.new(
|
||||
'IsTruncated' => true,
|
||||
'Marker' => 'marker',
|
||||
'MaxKeys' => 1,
|
||||
'Prefix' => 'prefix'
|
||||
)
|
||||
objects.is_truncated.should == true
|
||||
objects.marker.should == 'marker'
|
||||
objects.max_keys.should == 1
|
||||
objects.prefix.should == 'prefix'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#all" do
|
||||
|
||||
it "should return an S3::Objects"
|
||||
it "should return a Fog::AWS::S3::Objects" do
|
||||
@bucket.objects.all.should be_a(Fog::AWS::S3::Objects)
|
||||
end
|
||||
|
||||
it "should include persisted objects" do
|
||||
file = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
|
||||
object = @bucket.objects.create(:key => 'fogobjectname', :body => file)
|
||||
@bucket.objects.keys.should include('fogobjectname')
|
||||
object.destroy
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#bucket" do
|
||||
|
||||
it "should return an S3::Bucket"
|
||||
it "should return a Fog::AWS::S3::Bucket" do
|
||||
@bucket.objects.bucket.should be_a(Fog::AWS::S3::Bucket)
|
||||
end
|
||||
|
||||
it "should be the bucket the object is related to" do
|
||||
@bucket.objects.bucket.should == @bucket
|
||||
end
|
||||
end
|
||||
|
||||
describe "#create" do
|
||||
|
||||
it "should return an S3::Object that has been persisted to s3"
|
||||
it "should return a Fog::AWS::S3::Object" do
|
||||
file = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
|
||||
object = @bucket.objects.create(:key => 'fogobjectname', :body => file)
|
||||
object.should be_a(Fog::AWS::S3::Object)
|
||||
object.destroy
|
||||
end
|
||||
|
||||
it "should exist on s3" do
|
||||
file = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
|
||||
object = @bucket.objects.create(:key => 'fogobjectname', :body => file)
|
||||
@bucket.objects.get('fogobjectname').should_not be_nil
|
||||
object.destroy
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#get" do
|
||||
|
||||
it "should return an S3::Object with metadata and data"
|
||||
it "should return a Fog::AWS::S3::Object with metadata and data"
|
||||
|
||||
end
|
||||
|
||||
describe "#head" do
|
||||
|
||||
it "should return an S3::Object with metadata"
|
||||
it "should return a Fog::AWS::S3::Object with metadata"
|
||||
|
||||
end
|
||||
|
||||
describe "#new" do
|
||||
|
||||
it "should return an S3::Object"
|
||||
it "should return a Fog::AWS::S3::Object" do
|
||||
@bucket.objects.new.should be_a(Fog::AWS::S3::Object)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#reload" do
|
||||
|
||||
it "should reload from s3"
|
||||
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue