views:

39

answers:

1

I have account in amazon S3 and i use this just for my css and javascripts and photos like a CDN. I want a task capistrano for send my javascripts and css and photos to my bucket in amazon s3. How I do ?

tahnks.

thanks you John Topley Based in your code i did as below.

configure you config/s3.yaml

access_key_id:
secret_access_key:
bucket:

lib/tasks/s3.rake

namespace :s3 do
  namespace :push do
    require 'aws/s3'
    #TIMESTAMP  = '%Y%m%d-%H%M'
    db = YAML::load(open("#{RAILS_ROOT}/config/database.yml"))
    s3 = YAML::load(open("#{RAILS_ROOT}/config/s3.yml"))
    AWS::S3::Base.establish_connection!(
          :access_key_id => "#{s3['access_key_id']}",
          :secret_access_key => "#{s3['secret_access_key']}"
      )

    desc 'Send images of current brach to S3'
    task :images => :environment do
      path = "images"
      files = Dir.glob(File.join("public/#{path}", "*"))
      bucket = "#{s3['bucket']}/#{path}"
      files.each do |file|
          AWS::S3::S3Object.store(File.basename(file), open(file), "#{bucket}", :content_type => 'application/x-gzip')
          puts("Sending file #{file}") 
      end
    end

    desc 'Send css of current brach to S3'
    task :css => :environment do
      path = "stylesheets"
      files = Dir.glob(File.join("public/#{path}", "*.css"))
      bucket = "#{s3['bucket']}/#{path}"
      files.each do |file|
          AWS::S3::S3Object.store(File.basename(file), open(file), "#{bucket}", :content_type => 'application/x-gzip')
          puts("Sending file #{file}") 
      end
    end

     desc 'Send js of current brach to S3'
     task :js => :environment do
       path = "javascripts"
       files = Dir.glob(File.join("public/#{path}", "*.js"))
       bucket = "#{s3['bucket']}/#{path}"
       files.each do |file|
           AWS::S3::S3Object.store(File.basename(file), open(file), "#{bucket}", :content_type => 'application/x-gzip')
           puts("Sending file #{file}") 
       end
     end

     desc 'Send all files'
      task :all => :environment do
          system("rake s3:push:images RAILS_ENV=#{RAILS_ENV}")
          system("rake s3:push:css RAILS_ENV=#{RAILS_ENV}")
          system("rake s3:push:js RAILS_ENV=#{RAILS_ENV} ")
      end
  end

end

for deploy of assets in amazon s3 rake s3:push:images
rake s3:push:js
s3:push:css
s3:push:all

+1  A: 

A while ago I blogged about how to back up a MySQL database dump of a Rails application to Amazon S3 using Rails and the AWS-S3 RubyGem. You should be able to easily adapt the instructions to copy any file to S3.

John Topley