Merge pull request #750 from benmanns/add-file-copy
[storage] Add copy method to {Local,Ninefold,Rackspace}::File
This commit is contained in:
commit
a31f820db3
|
@ -84,6 +84,22 @@ Shindo.tests('storage examples', 'storage') do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
tests('@file.copy').succeeds do
|
||||||
|
@file.copy(@directory.key, 'fogstorageobject2')
|
||||||
|
end
|
||||||
|
|
||||||
|
tests('@copy = @directory.files.get(fogstorageobject2)').succeeds do
|
||||||
|
@copy = @directory.files.get('fogstorageobject2')
|
||||||
|
end
|
||||||
|
|
||||||
|
tests('@copy.body == @file.body').succeeds do
|
||||||
|
@copy.body == @file.body
|
||||||
|
end
|
||||||
|
|
||||||
|
tests('@copy.destroy').succeeds do
|
||||||
|
@copy.destroy
|
||||||
|
end
|
||||||
|
|
||||||
# destroy the file
|
# destroy the file
|
||||||
tests('@file.destroy').succeeds do
|
tests('@file.destroy').succeeds do
|
||||||
@file.destroy
|
@file.destroy
|
||||||
|
|
|
@ -36,6 +36,13 @@ module Fog
|
||||||
@directory
|
@directory
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def copy(target_directory_key, target_file_key, options={})
|
||||||
|
requires :directory, :key
|
||||||
|
connection.copy_object(directory.key, key, target_directory_key, target_file_key)
|
||||||
|
target_directory = connection.directories.new(:key => target_directory_key)
|
||||||
|
target_directory.files.get(target_file_key)
|
||||||
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
requires :directory, :key
|
requires :directory, :key
|
||||||
::File.delete(path) if ::File.exists?(path)
|
::File.delete(path) if ::File.exists?(path)
|
||||||
|
|
|
@ -64,6 +64,14 @@ module Fog
|
||||||
def path_to(partial)
|
def path_to(partial)
|
||||||
::File.join(@local_root, partial)
|
::File.join(@local_root, partial)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def copy_object(source_directory_name, source_object_name, target_directory_name, target_object_name, options={})
|
||||||
|
require 'fileutils'
|
||||||
|
source_path = path_to(::File.join(source_directory_name, source_object_name))
|
||||||
|
target_path = path_to(::File.join(target_directory_name, target_object_name))
|
||||||
|
::FileUtils.mkdir_p(::File.dirname(source_path))
|
||||||
|
::FileUtils.copy_file(source_path, target_path)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,6 +28,14 @@ module Fog
|
||||||
@directory
|
@directory
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def copy(target_directory_key, target_file_key, options={})
|
||||||
|
target_directory = connection.directories.new(:key => target_directory_key)
|
||||||
|
target_directory.files.create(
|
||||||
|
:key => target_file_key,
|
||||||
|
:body => body
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
requires :directory, :key
|
requires :directory, :key
|
||||||
connection.delete_namespace([directory.key, key].join('/'))
|
connection.delete_namespace([directory.key, key].join('/'))
|
||||||
|
|
|
@ -29,6 +29,13 @@ module Fog
|
||||||
@directory
|
@directory
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def copy(target_directory_key, target_file_key, options={})
|
||||||
|
requires :directory, :key
|
||||||
|
connection.copy_object(directory.key, key, target_directory_key, target_file_key)
|
||||||
|
target_directory = connection.directories.new(:key => target_directory_key)
|
||||||
|
target_directory.files.get(target_file_key)
|
||||||
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
requires :directory, :key
|
requires :directory, :key
|
||||||
connection.delete_object(directory.key, key)
|
connection.delete_object(directory.key, key)
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
module Fog
|
||||||
|
module Storage
|
||||||
|
class Rackspace
|
||||||
|
class Real
|
||||||
|
|
||||||
|
# Copy object
|
||||||
|
#
|
||||||
|
# ==== Parameters
|
||||||
|
# * source_container_name<~String> - Name of source bucket
|
||||||
|
# * source_object_name<~String> - Name of source object
|
||||||
|
# * target_container_name<~String> - Name of bucket to create copy in
|
||||||
|
# * target_object_name<~String> - Name for new copy of object
|
||||||
|
# * options<~Hash> - Additional headers
|
||||||
|
def copy_object(source_container_name, source_object_name, target_container_name, target_object_name, options={})
|
||||||
|
headers = { 'X-Copy-From' => "/#{source_container_name}/#{source_object_name}" }.merge(options)
|
||||||
|
request({
|
||||||
|
:expects => 201,
|
||||||
|
:headers => headers,
|
||||||
|
:method => 'PUT',
|
||||||
|
:path => "#{Fog::Rackspace.escape(target_container_name)}/#{Fog::Rackspace.escape(target_object_name)}"
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -15,6 +15,7 @@ module Fog
|
||||||
collection :files
|
collection :files
|
||||||
|
|
||||||
request_path 'fog/rackspace/requests/storage'
|
request_path 'fog/rackspace/requests/storage'
|
||||||
|
request :copy_object
|
||||||
request :delete_container
|
request :delete_container
|
||||||
request :delete_object
|
request :delete_object
|
||||||
request :get_container
|
request :get_container
|
||||||
|
|
Loading…
Reference in New Issue