Created
August 19, 2011 23:37
-
-
Save typeoneerror/1158310 to your computer and use it in GitHub Desktop.
s3 rake uploader
This file contains hidden or 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 'aws/s3' | |
namespace :s3 do | |
desc "Upload assets to AWS s3" | |
task :deploy, [:bucket] => [:environment] do |t, args| | |
config ||= YAML.load(ERB.new(IO.read(File.join(Rails.root, "config/s3.yml"))).result)[Rails.env] rescue nil || {} | |
bucket = args[:bucket].present? ? args[:bucket] : config['bucket'] | |
uploads = [] | |
files = Pathname.glob('public/**/*').each do |pn| | |
uploads.push "#{pn.dirname}/#{pn.basename}" unless pn.directory? | |
end | |
puts | |
puts "---- Uploading s3 bucket: #{bucket} ----" | |
puts | |
if uploads.any? | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => config['access_key_id'], | |
:secret_access_key => config['secret_access_key'] | |
) | |
uploads.each do |file| | |
basename = file.sub(/public\//, '') | |
puts "= Uploading #{file}" | |
AWS::S3::S3Object.store( | |
basename, | |
File.open(file), | |
bucket, | |
:access => :public_read | |
) | |
end | |
else | |
puts "= Nothing to upload to s3" | |
end | |
puts | |
puts "---- Done ----" | |
puts | |
end | |
end |
This file contains hidden or 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
$ rake s3:deploy | |
OR | |
$ rake s3:deploy[bucket-name] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment