From 85c0c2fbf1d850f326b5fc5cf32a05cb5f067978 Mon Sep 17 00:00:00 2001 From: geemus Date: Fri, 29 Oct 2010 16:38:17 -0700 Subject: [PATCH] [aws&google|storage] cleanup/fixes for bucket acl --- lib/fog/aws/models/storage/directory.rb | 11 +++++++++++ lib/fog/aws/models/storage/file.rb | 5 ++++- lib/fog/aws/requests/storage/put_bucket.rb | 12 ++++++++---- lib/fog/google/models/storage/directory.rb | 11 +++++++++++ lib/fog/google/models/storage/file.rb | 14 ++++++++++++++ lib/fog/google/requests/storage/put_bucket.rb | 9 +++++---- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/lib/fog/aws/models/storage/directory.rb b/lib/fog/aws/models/storage/directory.rb index 05caf9adc..462cca9eb 100644 --- a/lib/fog/aws/models/storage/directory.rb +++ b/lib/fog/aws/models/storage/directory.rb @@ -14,6 +14,14 @@ module Fog attribute :creation_date, :aliases => 'CreationDate' + def acl=(new_acl) + valid_acls = ['private', 'public-read', 'public-read-write', 'authenticated-read'] + unless valid_acls.include?(new_acl) + raise ArgumentError.new("acl must be one of [#{valid_acls.join(', ')}]") + end + @acl = new_acl + end + def destroy requires :key connection.delete_bucket(key) @@ -56,6 +64,9 @@ module Fog def save requires :key options = {} + if @acl + options['x-amz-acl'] = @acl + end if @location options['LocationConstraint'] = @location end diff --git a/lib/fog/aws/models/storage/file.rb b/lib/fog/aws/models/storage/file.rb index a47f8579f..bc1491ffe 100644 --- a/lib/fog/aws/models/storage/file.rb +++ b/lib/fog/aws/models/storage/file.rb @@ -70,8 +70,11 @@ module Fog def save(options = {}) requires :body, :directory, :key + if options != {} + Formatador.display_line("[yellow][WARN] options param is deprecated, use acl= instead[/] [light_black](#{caller.first})[/]") + end if @acl - options['x-amz-acl'] = @acl + options['x-amz-acl'] ||= @acl end data = connection.put_object(directory.key, @key, @body, options) @etag = data.headers['ETag'] diff --git a/lib/fog/aws/requests/storage/put_bucket.rb b/lib/fog/aws/requests/storage/put_bucket.rb index fbc07d579..d44a078c5 100644 --- a/lib/fog/aws/requests/storage/put_bucket.rb +++ b/lib/fog/aws/requests/storage/put_bucket.rb @@ -8,17 +8,21 @@ module Fog # ==== Parameters # * bucket_name<~String> - name of bucket to create # * options<~Hash> - config arguments for bucket. Defaults to {}. - # * :location_constraint<~Symbol> - sets the location for the bucket + # * 'LocationConstraint'<~Symbol> - sets the location for the bucket + # * 'x-amz-acl'<~String> - Permissions, must be in ['private', 'public-read', 'public-read-write', 'authenticated-read'] # # ==== Returns # * response<~Excon::Response>: # * status<~Integer> - 200 + # + # ==== API Reference + # http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUT.html def put_bucket(bucket_name, options = {}) - if options['LocationConstraint'] + if location_constraint = options.delete('LocationConstraint') data = <<-DATA - #{options['LocationConstraint']} + #{location_constraint} DATA else @@ -27,7 +31,7 @@ DATA request({ :expects => 200, :body => data, - :headers => {}, + :headers => options, :idempotent => true, :host => "#{bucket_name}.#{@host}", :method => 'PUT' diff --git a/lib/fog/google/models/storage/directory.rb b/lib/fog/google/models/storage/directory.rb index 486103482..b057de437 100644 --- a/lib/fog/google/models/storage/directory.rb +++ b/lib/fog/google/models/storage/directory.rb @@ -14,6 +14,14 @@ module Fog attribute :creation_date, :aliases => 'CreationDate' + def acl=(new_acl) + valid_acls = ['private', 'public-read', 'public-read-write', 'authenticated-read'] + unless valid_acls.include?(new_acl) + raise ArgumentError.new("acl must be one of [#{valid_acls.join(', ')}]") + end + @acl = new_acl + end + def destroy requires :key connection.delete_bucket(key) @@ -34,6 +42,9 @@ module Fog def save requires :key options = {} + if @acl + options['x-amz-acl'] = @acl + end if @location options['LocationConstraint'] = @location end diff --git a/lib/fog/google/models/storage/file.rb b/lib/fog/google/models/storage/file.rb index 16504d2df..b7ec5f09e 100644 --- a/lib/fog/google/models/storage/file.rb +++ b/lib/fog/google/models/storage/file.rb @@ -17,6 +17,14 @@ module Fog attribute :size, :aliases => 'Size' attribute :storage_class, :aliases => 'StorageClass' + def acl=(new_acl) + valid_acls = ['private', 'public-read', 'public-read-write', 'authenticated-read'] + unless valid_acls.include?(new_acl) + raise ArgumentError.new("acl must be one of [#{valid_acls.join(', ')}]") + end + @acl = new_acl + end + def body @body ||= if last_modified && (file = collection.get(identity)) file.body @@ -65,6 +73,12 @@ module Fog def save(options = {}) requires :body, :directory, :key + if options != {} + Formatador.display_line("[yellow][WARN] options param is deprecated, use acl= instead[/] [light_black](#{caller.first})[/]") + end + if @acl + options['x-amz-acl'] ||= @acl + end data = connection.put_object(directory.key, @key, @body, options) @etag = data.headers['ETag'] true diff --git a/lib/fog/google/requests/storage/put_bucket.rb b/lib/fog/google/requests/storage/put_bucket.rb index 4ffe6cdc7..4936ab9f7 100644 --- a/lib/fog/google/requests/storage/put_bucket.rb +++ b/lib/fog/google/requests/storage/put_bucket.rb @@ -8,17 +8,18 @@ module Fog # ==== Parameters # * bucket_name<~String> - name of bucket to create # * options<~Hash> - config arguments for bucket. Defaults to {}. - # * :location_constraint<~Symbol> - sets the location for the bucket + # * 'LocationConstraint'<~Symbol> - sets the location for the bucket + # * 'x-amz-acl'<~String> - Permissions, must be in ['private', 'public-read', 'public-read-write', 'authenticated-read'] # # ==== Returns # * response<~Excon::Response>: # * status<~Integer> - 200 def put_bucket(bucket_name, options = {}) - if options['LocationConstraint'] + if location_constraint = options.delete('LocationConstraint') data = <<-DATA - #{options['LocationConstraint']} + #{location_constraint} DATA else @@ -27,7 +28,7 @@ DATA request({ :expects => 200, :body => data, - :headers => {}, + :headers => options, :idempotent => true, :host => "#{bucket_name}.#{@host}", :method => 'PUT'