用capistrano将我的css和javascripts部署到亚马逊S3

时间:2010-07-15 14:49:20

标签: ruby-on-rails amazon-s3 capistrano

我在亚马逊S3中有帐户,我只是用于我的css和javascripts以及像CDN这样的照片。 我想要一个任务capistrano用于发送我的javascripts和css和照片到亚马逊s3我的桶。 我怎么办?

tahnks。

谢谢John 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:推:图像
rake s3:push:js
S3:推:CSS
S3:推:所有

1 个答案:

答案 0 :(得分:1)

不久前,我在博客中介绍了如何使用Rails和AWS-S3 RubyGem将Rails应用程序的MySQL数据库转储备份到Amazon S3。您应该能够轻松地调整说明以将任何文件复制到S3。