Last active
December 30, 2015 21:59
-
-
Save astrauka/7890928 to your computer and use it in GitHub Desktop.
Ruby on Rails - clone Rackspace cloud files from one container to another. Supports different rackspace users. Add the file contents to app/tasks/clone_rackspace_cloud_files.rb and launch CloneRackspaceCloudFiles.new.run via console. Can use as non-rails script, but then need to make sure require "fog" finds the file required. Environment variab…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "fog" | |
require "net/http" | |
class CloneRackspaceCloudFiles | |
def service | |
@service ||= Fog::Storage.new({ | |
:provider => 'Rackspace', | |
:rackspace_username => ENV["RACKSPACE_USER_NAME"], | |
:rackspace_api_key => ENV["RACKSPACE_API"], | |
:rackspace_auth_url => Fog::Rackspace::UK_AUTH_ENDPOINT, | |
:rackspace_region => :lon, | |
}) | |
end | |
def dest_service | |
@dest_service ||= Fog::Storage.new({ | |
:provider => 'Rackspace', | |
:rackspace_username => ENV["DEST_RACKSPACE_USER_NAME"], | |
:rackspace_api_key => ENV["DEST_RACKSPACE_API"], | |
:rackspace_auth_url => Fog::Rackspace::UK_AUTH_ENDPOINT, | |
:rackspace_region => :lon, | |
}) | |
end | |
def run | |
file_headers.each do |header| | |
copy file(header), file_name(header) | |
end | |
end | |
def file_headers | |
Array.wrap service.get_container(container).body | |
end | |
def file_name(header) | |
header["name"] | |
end | |
def file(header) | |
service.get_object(container, file_name(header)) | |
end | |
def copy(file, file_name) | |
pp "Copying #{file_name}" | |
dest_service.put_object dest_container, file_name, file.body | |
end | |
def container | |
ENV["CONTAINER"] | |
end | |
def dest_container | |
ENV["DEST_CONTAINER"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment