尝试使用Heroku安装S3时,我的应用程序崩溃了。
我想要做的是让Carrierwave上传文件到S3存储,并让rails从S3存储加载资产。我打开了我的S3帐户,在我的应用程序桶中,我上传了整个Assets文件夹,其中包含目录树屁股:
以下是我遵循的步骤,阅读指南Heroku: Using AWS S3 to Store Static Assets and File Uploads和Example of setting up S3 with Carrierwave:
在我的Gemfile中,我添加了
gem 'fog'
我运行了命令:
heroku config:add AWS_ACCESS_KEY_ID=XXXXXX AWS_SECRET_ACCESS_KEY=XXXXXX
heroku config:add S3_BUCKET_NAME=myapp
heroku config:add S3_REGION=ap-southeast-1 # I created my bucket in Singapore
heroku config:add S3_ASSET_URL=https://s3-ap-southeast-1.amazonaws.com/myapp/assets_%24folder%24
然后运行bundle install
然后我创建了config / initializers / carrierwave.rb
# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3 should be made available through an Environment variable.
# For local installations, export the env variable through the shell OR
# if using Passenger, set an Apache environment variable.
#
# In Heroku, follow http://devcenter.heroku.com/articles/config-vars
#
# $ heroku config:add S3_KEY=your_s3_access_key S3_SECRET=your_s3_secret S3_REGION=eu-west-1 S3_ASSET_URL=http://assets.example.com/ S3_BUCKET_NAME=s3_bucket/folder
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:region => ENV['S3_REGION']
}
# For testing, upload files to local `tmp` folder.
if Rails.env.test? || Rails.env.cucumber?
config.storage = :file
config.enable_processing = false
config.root = "#{Rails.root}/tmp"
else
config.storage = :fog
end
config.cache_dir = "#{Rails.root}/tmp/uploads" # To let CarrierWave work on heroku
config.fog_directory = ENV['S3_BUCKET_NAME']
config.s3_access_policy = :public_read # Generate http:// urls. Defaults to :authenticated_read (https://)
config.fog_host = "#{ENV['S3_ASSET_URL']}/#{ENV['S3_BUCKET_NAME']}"
end
之后我更新了我的git并推送了heroku:
git add .
git commit -m "added S3 configs with fog"
git push heroku master
当我使用Heroku应用程序时,我意识到出现错误,我检查了我的日志,出现以下错误:
...
2013-01-20T11:00:17+00:00 heroku[web.1]: Process exited with status 1
2013-01-20T11:00:17+00:00 heroku[web.1]: State changed from starting to crashed
2013-01-20T11:00:17+00:00 heroku[web.1]: State changed from crashed to starting
...
2013-01-20T11:00:52+00:00 app[web.1]: /app/config/initializers/carrierwave.rb:29:in `block in <top (required)>': undefined method `s3_access_policy=' for CarrierWave::Uploader::Base:Class (NoMethodError)
...
2013-01-20T11:00:53+00:00 heroku[web.1]: Process exited with status 1
2013-01-20T11:00:53+00:00 heroku[web.1]: State changed from starting to crashed
2013-01-20T11:04:52+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=myapp.herokuapp.com fwd=xxx.xxx.xx.x dyno= queue= wait= connect= service= status=503 bytes=
2013-01-20T11:04:54+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=myapp.herokuapp.com fwd=xxx.xxx.xx.x dyno= queue= wait= connect= service= status=503 bytes=
2013-01-20T11:04:55+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=myapp.herokuapp.com fwd=xxx.xxx.xx.x dyno= queue= wait= connect= service= status=503 bytes=
我还试图运行heroku run rake db:migrate
并收到错误:
rake aborted!
undefined method `s3_access_policy=' for CarrierWave::Uploader::Base:Class
另外,在我的观点中,我应该为静态资产添加什么URL?
感谢您提出任何明智的建议
的Aurelien
答案 0 :(得分:2)
以下是我解决问题的方法:
我评论了方法:config.s3_access_policy
和config.fog_host
并且它有效。这是我的最终初始化程序,它具有额外的条件检查,可以在基于Rails环境上传时使用雾化rails文件系统。谢谢FrontierPsycho,你是对的,凭证是最低的预请求。
# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3 should be made available through an Environment variable.
# For local installations, export the env variable through the shell OR
# if using Passenger, set an Apache environment variable.
#
# In Heroku, follow http://devcenter.heroku.com/articles/config-vars
#
# $ heroku config:add S3_KEY=your_s3_access_key S3_SECRET=your_s3_secret S3_REGION=eu-west-1 S3_ASSET_URL=http://assets.example.com/ S3_BUCKET_NAME=s3_bucket/folder
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:region => ENV['S3_REGION']
}
# For testing, upload files to local `tmp` folder.
if Rails.env.test? || Rails.env.cucumber?
config.storage = :file
config.enable_processing = false
config.root = "#{Rails.root}/tmp"
elsif Rails.env.development?
config.storage = :file
else
config.storage = :fog
end
config.cache_dir = "#{Rails.root}/tmp/uploads" # To let CarrierWave work on heroku
config.fog_directory = ENV['S3_BUCKET_NAME']
# config.s3_access_policy = :public_read # Generate http:// urls. Defaults to :authenticated_read (https://)
# config.fog_host = "#{ENV['S3_ASSET_URL']}/#{ENV['S3_BUCKET_NAME']}"
end
答案 1 :(得分:1)