mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
86 lines
No EOL
3.4 KiB
Ruby
86 lines
No EOL
3.4 KiB
Ruby
unless Fog.mocking?
|
|
|
|
module Fog
|
|
module AWS
|
|
class S3
|
|
|
|
# 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
|
|
#
|
|
# ==== Returns
|
|
# * response<~Fog::AWS::Response>:
|
|
# * body<~Hash>:
|
|
# * 'ETag'<~String> - etag of new object
|
|
# * 'LastModified'<~Time> - date object was last modified
|
|
#
|
|
# TODO: allow specifying new metadata (support all/some of put_object?)
|
|
def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {})
|
|
headers = { 'x-amz-copy-source' => "/#{source_bucket_name}/#{source_object_name}" }.merge!(options)
|
|
request({
|
|
:expects => 200,
|
|
:headers => headers,
|
|
:host => "#{target_bucket_name}.#{@host}",
|
|
:method => 'PUT',
|
|
:parser => Fog::Parsers::AWS::S3::CopyObject.new,
|
|
:path => target_object_name
|
|
})
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|
|
|
|
else
|
|
|
|
module Fog
|
|
module AWS
|
|
class S3
|
|
|
|
def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {})
|
|
response = Fog::Response.new
|
|
source_bucket_status = get_bucket(source_bucket_name).status
|
|
target_bucket_status = get_bucket(target_bucket_name).status
|
|
source_object_status = get_object(source_bucket_name, source_object_name).status
|
|
|
|
if source_bucket_status != 200
|
|
response.status = source_bucket_status
|
|
elsif target_bucket_status != 200
|
|
response.status = target_bucket_status
|
|
elsif source_bucket_status != 200
|
|
response.status = source_bucket_status
|
|
else
|
|
response.status = 200
|
|
end
|
|
|
|
if response.status == 200
|
|
source_bucket = @data['Buckets'].select {|bucket| bucket['Name'] == source_bucket_name}.first
|
|
source_object = source_bucket['Contents'].select {|object| object['Key'] == source_object_name}.first
|
|
target_bucket = @data['Buckets'].select {|bucket| bucket['Name'] == target_bucket_name}.first
|
|
target_object = source_object.dup
|
|
target_object['Name'] = target_object_name
|
|
target_bucket['Contents'] << target_object
|
|
response.body = {
|
|
'ETag' => target_object['ETag'],
|
|
'LastModified' => Time.parse(target_object['LastModified'])
|
|
}
|
|
end
|
|
|
|
response
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|
|
|
|
end |