2010-03-13 16:37:24 -05:00
module Fog
2011-06-15 17:26:43 -04:00
module Storage
class AWS
2010-03-13 16:37:24 -05:00
class Real
2009-08-08 17:46:13 -04:00
2011-08-24 14:50:42 -04:00
require 'fog/aws/parsers/storage/copy_object'
2010-06-12 18:31:17 -04:00
2009-08-08 17:46:13 -04:00
# Copy an object from one S3 bucket to another
#
# ==== Parameters
# * source_bucket_name<~String> - Name of source bucket
# * source_object_name<~String> - Name of source object
# * target_bucket_name<~String> - Name of bucket to create copy in
# * target_object_name<~String> - Name for new copy of object
# * options<~Hash>:
# * 'x-amz-metadata-directive'<~String> - Specifies whether to copy metadata from source or replace with data in request. Must be in ['COPY', 'REPLACE']
# * 'x-amz-copy_source-if-match'<~String> - Copies object if its etag matches this value
# * 'x-amz-copy_source-if-modified_since'<~Time> - Copies object it it has been modified since this time
# * 'x-amz-copy_source-if-none-match'<~String> - Copies object if its etag does not match this value
# * 'x-amz-copy_source-if-unmodified-since'<~Time> - Copies object it it has not been modified since this time
2011-02-02 02:02:14 -05:00
# * 'x-amz-storage-class'<~String> - Default is 'STANDARD', set to 'REDUCED_REDUNDANCY' for non-critical, reproducable data
2009-08-08 17:46:13 -04:00
#
# ==== Returns
2009-11-02 21:48:49 -05:00
# * response<~Excon::Response>:
2009-08-08 17:46:13 -04:00
# * body<~Hash>:
# * 'ETag'<~String> - etag of new object
# * 'LastModified'<~Time> - date object was last modified
#
2010-10-29 21:05:59 -04:00
# ==== See Also
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectCOPY.html
2009-08-08 17:46:13 -04:00
def copy_object ( source_bucket_name , source_object_name , target_bucket_name , target_object_name , options = { } )
2011-07-28 18:15:51 -04:00
headers = { 'x-amz-copy-source' = > " / #{ source_bucket_name } / #{ CGI . escape ( source_object_name ) } " } . merge! ( options )
2009-08-08 17:46:13 -04:00
request ( {
2009-08-16 14:44:50 -04:00
:expects = > 200 ,
:headers = > headers ,
:host = > " #{ target_bucket_name } . #{ @host } " ,
:method = > 'PUT' ,
2011-06-15 17:26:43 -04:00
:parser = > Fog :: Parsers :: Storage :: AWS :: CopyObject . new ,
2009-10-04 18:29:31 -04:00
:path = > CGI . escape ( target_object_name )
2009-08-08 17:46:13 -04:00
} )
end
2009-07-13 22:14:59 -04:00
end
2009-08-08 17:46:13 -04:00
2010-10-29 21:16:36 -04:00
class Mock # :nodoc:all
2009-07-13 22:14:59 -04:00
2009-08-08 17:46:13 -04:00
def copy_object ( source_bucket_name , source_object_name , target_bucket_name , target_object_name , options = { } )
2009-11-20 14:08:08 -05:00
response = Excon :: Response . new
2011-05-19 18:35:33 -04:00
source_bucket = self . data [ :buckets ] [ source_bucket_name ]
2012-02-06 09:53:27 -05:00
source_object = source_bucket && source_bucket [ :objects ] [ source_object_name ] && source_bucket [ :objects ] [ source_object_name ] . first
2011-05-19 18:35:33 -04:00
target_bucket = self . data [ :buckets ] [ target_bucket_name ]
2009-08-08 17:46:13 -04:00
2011-11-27 18:40:40 -05:00
acl = options [ 'x-amz-acl' ] || 'private'
if ! [ 'private' , 'public-read' , 'public-read-write' , 'authenticated-read' ] . include? ( acl )
raise Excon :: Errors :: BadRequest . new ( 'invalid x-amz-acl' )
else
self . data [ :acls ] [ :object ] [ target_bucket_name ] || = { }
self . data [ :acls ] [ :object ] [ target_bucket_name ] [ target_object_name ] = self . class . acls ( acl )
end
2009-08-16 14:45:52 -04:00
if source_object && target_bucket
2009-08-08 17:46:13 -04:00
response . status = 200
target_object = source_object . dup
2012-02-08 13:19:36 -05:00
target_object . merge! ( { 'Key' = > target_object_name } )
target_bucket [ :objects ] [ target_object_name ] = [ target_object ]
2009-08-08 17:46:13 -04:00
response . body = {
2012-02-08 13:19:36 -05:00
'ETag' = > target_object [ 'ETag' ] ,
'LastModified' = > Time . parse ( target_object [ 'Last-Modified' ] )
2009-08-08 17:46:13 -04:00
}
2009-08-16 14:45:52 -04:00
else
response . status = 404
2009-11-20 14:08:08 -05:00
raise ( Excon :: Errors . status_error ( { :expects = > 200 } , response ) )
2009-08-08 17:46:13 -04:00
end
response
end
end
2009-07-13 22:14:59 -04:00
end
end
2010-03-13 16:37:24 -05:00
end