rake command to migrate to AWS

This commit is contained in:
Yuki 2015-05-09 19:48:34 +05:30
parent 0a34cc07b3
commit 20ebb6dbad
3 changed files with 40 additions and 4 deletions

View File

@ -6,6 +6,36 @@ require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks
namespace :justask do
desc "Upload to AWS"
task paperclaws: :environment do
if APP_CONFIG["fog"]["credentials"].nil? or APP_CONFIG["fog"]["credentials"]["provider"] != :AWS
throw "Needs fog (AWS) to be defined in justask.yml"
format = '%t (%c/%C) [%b>%i] %e'
root = "#{Rails.root}/public/system"
files = Dir["#{root}/**/*.*"]
progress = ProgressBar.create title: 'Processing files', format: format, starting_at: 0, total: files.length
# weird voodoo, something is causing just using "APP_CONFIG["fog"]["credentials"]" as Fog::Storage.new to cause an exception
# TODO: Programmatically copy?
credentials = {
provider: :AWS,
aws_access_key_id: APP_CONFIG["fog"]["credentials"]["aws_access_key_id"],
aws_secret_access_key: APP_CONFIG["fog"]["credentials"]["aws_secret_access_key"],
region: APP_CONFIG["fog"]["credentials"]["region"]
}
fog = Fog::Storage.new credentials
bucket = fog.directories.get APP_CONFIG["fog"]["directory"]
files.each do |file|
bucket.files.create key: file[root.length + 1 ... file.length], body: File.open(file), public: true
progress.increment
end
puts "hopefully uploaded #{files.length} files"
end
desc "Recount everything!"
task recount: :environment do
format = '%t (%c/%C) [%b>%i] %e'

View File

@ -2,8 +2,13 @@ Paperclip::Attachment.default_options[:storage] = :fog
Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"}
Paperclip::Attachment.default_options[:fog_directory] = "/system"
Paperclip::Attachment.default_options[:fog_host] = "/system"
unless APP_CONFIG["fog"].nil?
Paperclip::Attachment.default_options[:fog_credentials] = APP_CONFIG["fog"]["credentials"] unless APP_CONFIG["fog"]["credentials"].nil?
Paperclip::Attachment.default_options[:fog_directory] = APP_CONFIG["fog"]["directory"] unless APP_CONFIG["fog"]["directory"].nil?
Paperclip::Attachment.default_options[:fog_host] = APP_CONFIG["fog"]["host"] unless APP_CONFIG["fog"]["host"].nil?
if not APP_CONFIG["fog"]["credentials"].nil? and APP_CONFIG["fog"]["host"].nil?
Paperclip::Attachment.default_options[:fog_host] = nil
end
end

View File

@ -49,7 +49,8 @@ fog:
provider: 'AWS'
aws_access_key_id: 'ACCESS KEY'
aws_secret_access_key: 'SECRET KEY'
# bucket name
directory: 'retrospring-user-content'
# URL host
host: "retrospring-user-content.s3.amazonaws.com"
region: 'space-pluto-1'
# bucket name, required
directory: 'retrospring'
# URL host, comment out to use default, GENERALLY you don't want to define this
# host: ''