2010-07-15 68 views
0

我有帳戶在亞馬遜S3,我用這只是爲我的CSS和JavaScript和像CDN照片。 我想要一個任務capistrano發送我的javascript和css和照片到我的存儲桶在亞馬遜s3。 我該怎麼辦?部署我的css和javascript與capistrano到亞馬遜S3

tahnks。

謝謝你約翰Topley 基於你的代碼我做了如下。

配置你的config/s3.yaml

access_key_id:
secret_access_key:
鬥:

的lib /任務/ 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 

的部署的資產在亞馬遜s3 rake s3:push:images
耙S3:推:JS
S3:推:CSS
S3:推:所有

回答

1

前段時間我的博客上講述瞭如何使用Rails的備份MySQL數據庫轉儲Rails應用程序到Amazon S3和AWS-S3 RubyGem。您應該能夠輕鬆地調整指令以將任何文件複製到S3。