是我的资产正在从AWS S3服务我的、资产、AWS

2023-09-11 23:46:05 作者:22.洞房不败.

也许真正愚蠢的问题,请不要标志着我失望了,但我终于得到了Heroku的在我的S3桶asset_sync编译它的静态资产。

现在我怎么知道资产实际上正在从那里服,我把它世界上没有神奇的事情,在从S3拉他们吗?我要为每个资产prefixed路径与

  https://s3-eu-west-1.amazonaws.com/pathto/asset
 

有没有办法明确设置这西纳特拉,我不必手动更改所有资产是吧?这将是愚蠢的。

在asset_sync文档说使用这种在轨

  config.action_controller.asset_host =//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com
 
AWS s3 V4签名算法

但林不知道该如何设置在西纳特拉

修改

 要求打捆/设置
Bundler.require(:默认)
要求active_support / core_ext
需要'./config/env如果File.exists?(配置/ env.rb)
需要'./config/config
要求RubyGems的
要求辛纳特拉


配置:开发办
AssetSync.config.action_controller.asset_host =//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com
结束


得到'/'做
 雇员再培训局:指数
结束

得到'/关于做
 雇员再培训局:有关
结束
 

这提供了以下错误控制台

 未定义的方法`action_controller'的#< AssetSync ::配置:0x24d1238> (NoMethodError)
 

解决方案

尝试把它 Sinatra的配置块通过异步内置初始化,例如:

 配置:生产做
  AssetSync.config.action_controller.asset_host =//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com
结束
 

这是可能的,你得叫 AssetSync.sync ,以及在某些时候,我不知道。

编辑:用配置块

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

 类应用<西纳特拉::基地
  配置:开发办
    设置:此,并且
    启用:东西
    设置:this_only,在开发模式下获取运行
  结束

  配置:生产做
    设置:这个别的东西
    禁用:东西
    设置:this_only,在生产中获取运行
    #把你的AssetSync东西在这里
  结束

  得到/做
    #...
  结束

  得到/资产吗
    #...
  结束

  后/更路线吗
    #...
  结束

  # 等等
结束
 

请参阅我在上面添加更多的链接。

action_controller 是的Rails的一部分。为preFIX的路径,你可以做的最好的事情是使用一个辅助:

 助手做
  高清aws_asset(路径)
    File.join settings.asset_host,路径
  结束
结束

配置:生产做
  设置:asset_host,//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com
结束

配置:开发办
  设置:asset_host,/#这应该从'public_folder`为它服务,如果您需要添加子目录。
结束
 

然后在路线或视图中,可以做这样的事情:

  aws_assetsprite_number_1.jpg
 

要与雇员再培训局和西纳特拉静电-资产的image_tag :

  image_tag(aws_assetsprite_number_1.jpg)
 

或将它们组合(如 image_tag 帮手可能在助手的范围看不出来,这可能无法正常工作,更容易去尝试,而不是去想它)

 助手做
  高清aws_image(路径)
    image_tag(File.join settings.asset_host,路径)
  结束
结束

#在你看来
aws_image(sprite_number_1.jpg)
 

我敢肯定就会有一个更简单的方法来做到这一点,但这会做一个快速和肮脏的解决方案。

Maybe a really daft question and please dont mark me down for this, but I finally got Heroku to compile its static assets in my S3 bucket with asset_sync.

Now how do i know that the assets are in fact being served from there, I take it theres no magic going on that pulls them in from s3? I have to set the path for each asset prefixed with

https://s3-eu-west-1.amazonaws.com/pathto/asset

Is there a way to set this in sinatra explicitly, I don't have to manually change every asset do I? that would be silly.

The asset_sync docs say to use this in rails

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

but im not sure how to set this in sinatra

EDIT

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

This gives the following error in the console

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

解决方案

Try putting it in Sinatra's configure block via the Async Built-in initializer, e.g:

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

It's possible you'll have to call AssetSync.sync as well at some point, I'm not sure.

Edit: using a configure block.

If you were using a modular app (if not, it's not any different, just remove the class bits)

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

See the link I added above for more.

action_controller is part of Rails. To prefix the path, the best thing you could do is use a helper:

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

Then in a route or a view you can do something like this:

aws_asset "sprite_number_1.jpg"

To use with ERB and sinatra-static-assets's image_tag:

image_tag( aws_asset "sprite_number_1.jpg" )

or combine them (this may not work as the image_tag helper might not be seen in the scope of the helper, it's easier to try it than to think about it):

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" )

I'm sure there'll be an easier way to do this but this will do for a quick and dirty solution.