【发布时间】:2010-07-15 14:49:19
【问题描述】:
我在 amazon S3 中有帐户,我只将它用于我的 css 和 javascripts 以及 CDN 之类的照片。 我想要一个任务 capistrano 将我的 javascripts 和 css 以及照片发送到我在 amazon s3 中的存储桶。 我该怎么做?
tahnks.
谢谢约翰·托普利 根据你的代码,我做了如下。
配置你的 config/s3.yaml
access_key_id:
秘密访问密钥:
桶:
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
结束
用于在 amazon s3 中部署资产
rake s3:push:图像
耙 s3:push:js
s3:push:css
s3:推送:全部
【问题讨论】:
标签: ruby-on-rails amazon-s3 capistrano