【问题标题】:Asset_Sync not pushing to S3Asset_Sync 未推送到 S3
【发布时间】:2014-11-26 23:32:54
【问题描述】:

我正在 Heroku 上构建一个 Rails 应用程序,并希望将 js、css 和图像文件部署到 Amazon 上的存储桶中。我没有找到很多资源,但我正在使用这个(2012)教程作为指导; https://firmhouse.com/blog/complete-guide-to-serving-your-rails-assets-over-s3-with-asset_sync

目前网站主要是css和js。到目前为止,这是我的代码;

production.rb

Rails.application.configure do

config.action_controller.asset_host = "http://localize.s3.amazonaws.com"

config.cache_classes = true

config.consider_all_requests_local       = false
config.action_controller.perform_caching = true

config.serve_static_assets = false

config.assets.compress = true

config.assets.compile = false

config.assets.digest = true

end

initializers/asset_sync.rb

 if defined?(AssetSync)
 AssetSync.configure do |config|
  config.fog_provider = ENV['FOG_PROVIDER']
  config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID'] 
  config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
  config.fog_directory = ENV['FOG_DIRECTORY']
  config.fog_region = ENV['FOG_REGION']

 # Don't delete files from the store
 config.existing_remote_files = "delete"

 # Automatically replace files with their equivalent gzip compressed version
 config.gzip_compression = true

# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
config.manifest = true

config.custom_headers = { '.*' => { cache_control: 'max-age=31536000', expires: 1.year.from_now.httpdate } }
 end
end

Heroku 变量

AWS_ACCESS_KEY_ID:            *****************
AWS_SECRET_ACCESS_KEY:        *****************************
FOG_DIRECTORY:                localize
FOG_PROVIDER:                 AWS
FOG_REGION:                   us-west-2

宝石文件

gem 'rails', '4.1.1'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails'
gem 'sdoc', '~> 0.4.0', group: :doc

#aws
gem "fog", "~>1.20"
gem 'asset_sync'

group :development do 
    gem 'thin'
end

group :production do 
  gem 'newrelic_rpm'
  gem 'rails_12factor'
  gem 'pg'
end 

我也跑了:

heroku config:add FOG_PROVIDER=AWS AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=yyy

随着

heroku config:add FOG_DIRECTORY=localize

然后当我运行时

bundle exec rake assets:precompile

或者

RAILS_ENV=production bundle exec rake assets:precompile

我得到了这个输出;

rake aborted!
AssetSync::Config::Invalid: Fog directory can't be blank, Aws access key can't be blank, Aws secret access key can't be blank

任何对 Rails、heroku 和 S3 有经验的人都可以指导我朝着正确的方向前进,我们将不胜感激。提前致谢。

【问题讨论】:

  • 不应该 config.fog_directory = ENV['localize'] 是 config.fog_directory = ENV['FOG_DIRECTORY'] 吗?
  • 并阅读 heroku 文档以在 heroku 服务器上获取 VARS,您可能需要执行 heroku config:set FOG_DIRECTORY=localize 例如..您是否检查过使用 config:add 是否实际上已将 VARS 添加到heroku 服务器通过执行 heroku config:get FOG_DIRECTORY 或执行 heroku config 以查看您上传的所有 VARS
  • @Richlewis 是的,我将代码更改为 config.fog_directory = ENV['FOG_DIRECTORY'] 。我还为 FOG_DIRECTORY、FOG_PROVIDER、AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 运行了 heroku config:add 和 heroku config:set。我现在得到的错误是rake aborted! AssetSync::Config::Invalid: Aws access key can't be blank, Aws secret access key can't be blank,所以它适用于 FOG,但不适用于 AWS
  • 您是否将 config.aws_access_key_id = ENV['MY_ACCESS_KEY'] 和 config.aws_secret_access_key = ENV['MY_SECRET_KEY'] 放入您的asset_sync 配置文件中?
  • @Richlewis 是的,我有。我跑了bundle exec rake assets:precompile,并收到一个新错误,看起来它与权限有关; rake aborted! Excon::Errors::Forbidden: Expected(200) <=> Actual(403 Forbidden) excon.error.response

标签: ruby-on-rails-4 heroku amazon-s3


【解决方案1】:

好的,看看你的设置似乎有一些问题,我会添加我通常使用的,希望它会帮助你

生产.rb

ExampleApp::Application.configure do

config.action_controller.asset_host = "http://exampleapp.s3.amazonaws.com"

config.cache_classes = true

config.consider_all_requests_local       = false
config.action_controller.perform_caching = true

config.serve_static_assets = true

config.assets.compress = true

config.assets.compile = true

config.assets.digest = true

end

asset_sync.rb

if defined?(AssetSync)
 AssetSync.configure do |config|
  config.fog_provider = ENV['FOG_PROVIDER']
  config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
  config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
  config.fog_directory = ENV['FOG_DIRECTORY']
  config.fog_region = ENV['FOG_REGION']

 # Don't delete files from the store
 config.existing_remote_files = "delete"

 # Automatically replace files with their equivalent gzip compressed version
 config.gzip_compression = true

# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
config.manifest = true

config.custom_headers = { '.*' => { cache_control: 'max-age=31536000', expires: 1.year.from_now.httpdate } }
 end
end

【讨论】:

  • 不走运 :( 但感谢您的帮助。因为这不起作用,您知道我是否要摆脱与 s3 相关的所有内容(宝石、初始化程序等),然后预编译资产,例如之前会恢复正常吗?
  • 我真的不明白为什么这不起作用,这是您在亚马逊上设置的第一个存储桶吗?你还有其他人在工作吗?有点明显,但我想我会检查一下,您是否与他们设置了帐单以便他们可以收取使用费?
  • 是的,这是第一次使用 AWS。我通常只使用heroku,但因为有很多图像、js 和css,它有点慢所以我想加快速度(听说s3 是怎么做的)。我注册了免费套餐,因为这是我唯一使用 s3 的网站
  • 是的,S3 存储桶会有很大帮助。您在 S3 中将存储桶命名为什么?确定是本地化的吗?
  • 我问的原因是如果您尝试转到localize.s3.amazonaws.com 访问被拒绝,这告诉我存储桶名称不正确?
猜你喜欢
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2021-05-03
  • 2020-07-19
  • 2012-02-29
  • 2013-03-22
  • 2015-02-09
相关资源
最近更新 更多