Rails如何将预编译资产上载到Cloudfront

时间:2022-11-10 20:37:04
  • I'm using a Rails 3.2 app
  • 我正在使用Rails 3.2应用程序
  • I created a Cloudfront distribution with a S3 origin
  • 我创建了一个具有S3源的Cloudfront发行版
  • I already change config.action_controller.asset_host to my cloudfront asset
  • 我已经将config.action_controller.asset_host更改为我的cloudfront资产
  • I'm using capistrano
  • 我正在使用capistrano

I want to know, how to upload my precompiled assets to Cloudfront every time I deploy

我想知道,每次部署时如何将预编译资产上传到Cloudfront

3 个解决方案

#1


18  

There is no need for the s3 bucket as Amazon Cloudfront now supports "custom origins". It used to be that you had to specify a s3 bucket as the origin, but now you can point your cloudfront distribution at your rails app.

由于Amazon Cloudfront现在支持“自定义源”,因此不需要s3存储桶。过去,您必须指定一个s3存储桶作为源,但现在您可以将您的cloudfront分发指向您的rails应用程序。

http://aws.typepad.com/aws/2010/11/amazon-cloudfront-support-for-custom-origins.html

http://aws.typepad.com/aws/2010/11/amazon-cloudfront-support-for-custom-origins.html

So, after you create a distribution that is pointed at your publicly accessible app, you set your asset host in the appropriate config file e.g. production.rb and you are off to the races.

因此,在创建指向可公开访问的应用程序的分发后,您可以在适当的配置文件中设置资产主机,例如production.rb和你一起参加比赛。

config.action_controller.asset_host = "url of your cloudfront distribution"

Here is a half decent article on how to configure things:

这是关于如何配置事物的一篇不错的文章:

http://ryantownsend.co.uk/post/13126016608/cloudfront-cdn-on-rails

http://ryantownsend.co.uk/post/13126016608/cloudfront-cdn-on-rails

Don't be confused with the bit about setting up CNAME recprds for an origin subdomain. This is only relevant if you want to use your own subdomain instead of amazons.

不要混淆有关为源子域设置CNAME recprds的问题。仅当您想要使用自己的子域而不是亚马逊时,这才有意义。

#2


14  

After installing the aws-s3 gem, you can add this at this end of your capistrano recipe :

安装aws-s3 gem后,您可以在capistrano配方的这一端添加:

set :cdn_user, "KEY ID" # This is called "CDN KEY API" for AWS
set :cdn_api_key, "YOUR KEY SECRET"
set :cdn_container, "bucket name"

namespace :assets do
    task :to_cdn do
      require 'aws/s3'
      AWS::S3::Base.establish_connection!(:access_key_id => cdn_user, :secret_access_key => cdn_api_key )
      assets_dir = "#{shared_path}/assets"
      Dir.glob(assets_dir + "/**/*").each do |file|
        if !File.directory?(file)
          cdn_filename = file.gsub(assets_dir,"assets")
          AWS::S3::S3Object.store(cdn_filename, open(file) , cdn_container)
        end
      end
    end
end

Using a trigger like this :

使用这样的触发器:

after "deploy:assets:precompile", "assets:to_cdn"

You will also need to launch the compilation at some point in your recipe with :

您还需要在配方中的某个位置启动编译:

load 'deploy/assets'

Result : at the end of your asset compilation, all your assets are going to be pushed on S3, and you will be able to access them from cloudfront.

结果:在资产编译结束时,所有资产都将在S3上推送,您将能够从cloudfront访问它们。

You then have to update your config/environment/production.rb to point to your CDN URL.

然后,您必须更新config / environment / production.rb以指向您的CDN URL。

config.action_controller.asset_host = "http://assets.example.com"

#3


3  

There's a great Ruby gem that handles this called AssetSync. If you combine it with turbo-sprockets, you can make sure that only new or updated assets are copied on deploy.

有一个很好的Ruby gem来处理这个叫做AssetSync的东西。如果将其与tu​​rbo-sprockets结合使用,则可以确保在部署时仅复制新资产或更新资产。

#1


18  

There is no need for the s3 bucket as Amazon Cloudfront now supports "custom origins". It used to be that you had to specify a s3 bucket as the origin, but now you can point your cloudfront distribution at your rails app.

由于Amazon Cloudfront现在支持“自定义源”,因此不需要s3存储桶。过去,您必须指定一个s3存储桶作为源,但现在您可以将您的cloudfront分发指向您的rails应用程序。

http://aws.typepad.com/aws/2010/11/amazon-cloudfront-support-for-custom-origins.html

http://aws.typepad.com/aws/2010/11/amazon-cloudfront-support-for-custom-origins.html

So, after you create a distribution that is pointed at your publicly accessible app, you set your asset host in the appropriate config file e.g. production.rb and you are off to the races.

因此,在创建指向可公开访问的应用程序的分发后,您可以在适当的配置文件中设置资产主机,例如production.rb和你一起参加比赛。

config.action_controller.asset_host = "url of your cloudfront distribution"

Here is a half decent article on how to configure things:

这是关于如何配置事物的一篇不错的文章:

http://ryantownsend.co.uk/post/13126016608/cloudfront-cdn-on-rails

http://ryantownsend.co.uk/post/13126016608/cloudfront-cdn-on-rails

Don't be confused with the bit about setting up CNAME recprds for an origin subdomain. This is only relevant if you want to use your own subdomain instead of amazons.

不要混淆有关为源子域设置CNAME recprds的问题。仅当您想要使用自己的子域而不是亚马逊时,这才有意义。

#2


14  

After installing the aws-s3 gem, you can add this at this end of your capistrano recipe :

安装aws-s3 gem后,您可以在capistrano配方的这一端添加:

set :cdn_user, "KEY ID" # This is called "CDN KEY API" for AWS
set :cdn_api_key, "YOUR KEY SECRET"
set :cdn_container, "bucket name"

namespace :assets do
    task :to_cdn do
      require 'aws/s3'
      AWS::S3::Base.establish_connection!(:access_key_id => cdn_user, :secret_access_key => cdn_api_key )
      assets_dir = "#{shared_path}/assets"
      Dir.glob(assets_dir + "/**/*").each do |file|
        if !File.directory?(file)
          cdn_filename = file.gsub(assets_dir,"assets")
          AWS::S3::S3Object.store(cdn_filename, open(file) , cdn_container)
        end
      end
    end
end

Using a trigger like this :

使用这样的触发器:

after "deploy:assets:precompile", "assets:to_cdn"

You will also need to launch the compilation at some point in your recipe with :

您还需要在配方中的某个位置启动编译:

load 'deploy/assets'

Result : at the end of your asset compilation, all your assets are going to be pushed on S3, and you will be able to access them from cloudfront.

结果:在资产编译结束时,所有资产都将在S3上推送,您将能够从cloudfront访问它们。

You then have to update your config/environment/production.rb to point to your CDN URL.

然后,您必须更新config / environment / production.rb以指向您的CDN URL。

config.action_controller.asset_host = "http://assets.example.com"

#3


3  

There's a great Ruby gem that handles this called AssetSync. If you combine it with turbo-sprockets, you can make sure that only new or updated assets are copied on deploy.

有一个很好的Ruby gem来处理这个叫做AssetSync的东西。如果将其与tu​​rbo-sprockets结合使用,则可以确保在部署时仅复制新资产或更新资产。