2014-10-02 102 views
1

我在heroku上构建了一个rails应用程序,并且想要将js,css和图像文件部署到Amazon上的存储桶中。我还没有找到这方面的许多资源,但我正在使用此(2012)教程进行指导; https://firmhouse.com/blog/complete-guide-to-serving-your-rails-assets-over-s3-with-asset_syncAsset_Sync没有推送到S3

该网站目前主要是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 

初始化/ 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 

周的Gemfile

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 

任何人谁拥有轨道,Heroku的经验,和S3谁可以指导我在正确的方向,将不胜感激。提前致谢。

+0

shouldnt config.fog_directory = ENV ['localize']是config.fog_directory = ENV ['FOG_DIRECTORY']? – Richlewis 2014-10-02 08:44:52

+0

并阅读heroku文档以获取可能需要做的heroku服务器上的VARS heroku config:set FOG_DIRECTORY =本地化例如..你检查了如果使用config:add实际上已经通过做heroku配置添加VARS到heroku服务器:得到FOG_DIRECTORY或者做heroku config查看你上传的所有VARS – Richlewis 2014-10-02 08:47:45

+0

@Richlewis是的,我将代码改为config.fog_directory = ENV ['FOG_DIRECTORY']。我还运行了heroku config:add和heroku config:设置为FOG_DIRECTORY,FOG_PROVIDER,AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY。我现在得到的错误是'rake中止了! AssetSync :: Config ::无效:Aws访问密钥不能为空,Aws秘密访问密钥不能为空,所以它在FOG上工作,但不是AWS – user3749994 2014-10-03 04:33:59

回答

0

好吧看你的设置似乎有几件事情错了,生病添加什么我通常使用,并希望它会帮助你

Production.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 
+0

不幸运:(但感谢您的帮助,因为这不起作用,你知道我是否要摆脱所有s3相关的东西(宝石,初始化器等),然后像以前一样预先编译资产,它会恢复正常吗? – user3749994 2014-10-03 10:02:36

+0

我真的不能看到为什么这不起作用,这是你用亚马逊设置的第一个桶吗?你有其他人在工作吗?有点显而易见,但我认为我会检查,你有没有设置他们的账单,以便他们可以收取使用费? – Richlewis 2014-10-03 10:06:53

+0

是的,这是第一次使用AWS,我通常只使用heroku,但由于有很多图像,js和css,所以速度很慢,所以我想加快速度(听过s3是如何做到这一点)。我注册了免费套餐,因为这是我使用s3的唯一网站。s3 – user3749994 2014-10-03 10:10:33