Skip to content

Instantly share code, notes, and snippets.

@astrauka
Last active December 30, 2015 21:59
Show Gist options
  • Save astrauka/7890928 to your computer and use it in GitHub Desktop.
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…
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