【问题标题】:Are my assets being served from AWS S3我的资产是否由 AWS S3 提供
【发布时间】:2013-03-08 18:23:19
【问题描述】:

也许是一个非常愚蠢的问题,请不要为此标记我,但我终于让 Heroku 使用asset_sync 在我的 S3 存储桶中编译它的静态资产。

现在我怎么知道资产实际上是从那里提供的,我认为没有任何魔法可以将它们从 s3 中拉出来?我必须为每个以

为前缀的资产设置路径
https://s3-eu-west-1.amazonaws.com/pathto/asset

有没有办法在 sinatra 中明确设置,我不必手动更改每个资产,对吗?那太傻了。

asset_sync 文档说要在 rails 中使用它

config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"

但我不确定如何在 sinatra 中设置它

编辑

require 'bundler/setup'
Bundler.require(:default)
require 'active_support/core_ext'
require './config/env' if File.exists?('config/env.rb')
require './config/config'
require "rubygems"
require 'sinatra'


configure :development do
AssetSync.config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
end


get '/' do
 erb :index
end

get '/about' do
 erb :about
end

这会在控制台中出现以下错误

 undefined method `action_controller' for #<AssetSync::Config:0x24d1238> (NoMethodError)

【问题讨论】:

    标签: ruby heroku amazon-s3 sinatra assets


    【解决方案1】:

    尝试通过Async Built-in initializer 将其放入Sinatra's configure block,例如:

    configure :production do
      AssetSync.config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
    end
    

    您可能在某个时候也必须致电AssetSync.sync,我不确定。


    编辑:使用配置块。

    如果您使用的是模块化应用程序(如果不是,则没有什么不同,只需删除 class 位)

    class App < Sinatra::Base
      configure :development do
        set :this, "and that"
        enable :something
        set :this_only, "gets run in development mode"
      end
    
      configure :production do
        set :this, "to something else"
        disable :something
        set :this_only, "gets run in production"
        # put your AssetSync stuff in here
      end
    
      get "/" do
        # …
      end
    
      get "/assets" do
        # …
      end
    
      post "/more-routes" do
        # …
      end
    
      # etc
    end
    

    有关更多信息,请参阅我在上面添加的链接。


    action_controller 是 Rails 的一部分。要为路径添加前缀,您可以做的最好的事情是使用帮助器:

    helpers do
      def aws_asset( path )
        File.join settings.asset_host, path
      end
    end
    
    configure :production do
      set :asset_host, "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
    end
    
    configure :development do
      set :asset_host, "/" # this should serve it from the `public_folder`, add subdirs if you need to.
    end
    

    然后在路线或视图中,您可以执行以下操作:

    aws_asset "sprite_number_1.jpg"
    

    与 ERB 和 sinatra-static-assets's image_tag 一起使用:

    image_tag( aws_asset "sprite_number_1.jpg" )
    

    或将它们组合起来(这可能不起作用,因为image_tag 帮助器可能不会出现在帮助器的范围内,尝试它比考虑它更容易):

    helpers do
      def aws_image( path )
        image_tag( File.join settings.asset_host, path )
      end
    end
    
    # in your view
    aws_image( "sprite_number_1.jpg" )
    

    我确信会有一种更简单的方法来做到这一点,但这会是一个快速而肮脏的解决方案。

    【讨论】:

    • 我要为此创建一个新文件吗?还是在我的应用程序文件中?不知道把这段代码放在哪里抱歉
    • @Richlewis 没关系,这就是 StackOverflow 的用途! :) 我添加了一个示例。
    • 谢谢 iain,所以在这个例子中我不需要设置和启用?配置块?我现在选择了开发,看看是否可以在本地工作。也得到未定义的方法 AssetSync,但我想那是因为我还不需要任何东西?
    • 好的,所以我需要相关的宝石等,但似乎 action_controller 是一个未定义的方法?
    • @Richlewis 是的,忽略setenable 之类的其他内容,它只是作为您将在configure 块中看到的内容的示例。是的,您需要 AssetSync。您遇到的错误,我认为您应该将整个错误发布到您的问题中。
    猜你喜欢
    • 2013-03-06
    • 1970-01-01
    • 2011-12-26
    • 2022-01-23
    • 2018-04-09
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    相关资源
    最近更新 更多