Created
June 13, 2012 18:21
-
-
Save bendangelo/2925635 to your computer and use it in GitHub Desktop.
S3 upload rake task
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 'aws/s3' | |
require 'digest/md5' | |
require 'mime/types' | |
#load config yml | |
CONFIG = YAML.load_file("config/config.yml") | |
## These are some constants to keep track of my S3 credentials and | |
## bucket name. Nothing fancy here. | |
AWS_ACCESS_KEY_ID = CONFIG['S3_KEY'] | |
AWS_SECRET_ACCESS_KEY = CONFIG['S3_SECRET'] | |
AWS_BUCKET = CONFIG['S3_BUCKET'] | |
## This defines the rake task `assets:deploy`. | |
namespace :s3 do | |
desc "Deploy all frameworks to S3" | |
task :frameworks, :env, :branch do |t, args| | |
AWS.config( | |
:access_key_id => AWS_ACCESS_KEY_ID, | |
:secret_access_key => AWS_SECRET_ACCESS_KEY) | |
puts "== Uploading assets to S3 ==" | |
s3 = AWS::S3.new | |
bucket = s3.buckets[AWS_BUCKET] | |
## Needed to show progress | |
STDOUT.sync = true | |
## Find all files (recursively) in assets folder | |
Dir["public/assets/**/*"].each do |file| | |
## Only upload files, we're not interested in directories | |
remote_file = file.sub('public/','') | |
if File.file?(file) | |
object = bucket.objects[remote_file] | |
object.write(:file=>file) | |
puts "Uploaded #{remote_file}" | |
else | |
puts "Error #{file} not found!" | |
end | |
end | |
STDOUT.sync = false # Done with progress output. | |
puts "== Done syncing frameworks ==" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment