2

在尝试使用 Heroku 安装 S3 时,我的应用程序崩溃了。

我想要做的是让 Carrierwave 将文件上传到 S3 存储,并让 rails 从 S3 存储加载资产。我打开了我的 S3 帐户,在我的应用程序存储桶中,我上传了整个 Assets 文件夹,目录树如下:

  • 我的应用
    • 资产
      • 图片
      • javascripts
      • 样式表

以下是我遵循的步骤,阅读指南Heroku:使用 AWS S3 存储静态资产和文件上传以及使用 Carrierwave 设置 S3 的示例

在我的 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?

谢谢你的任何明智的建议

奥雷连

4

2 回答 2

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
于 2013-02-03T05:20:55.433 回答
1

我不确定这一点,但我似乎遵循了与您相同的指南,并且它一定已经过时了。CarrierWave Uploader API 似乎发生了变化。现在,上传的图片默认是公开的,您可以通过 config.fog_public 配置选项进行更改。

在这里这里查看更多信息。

我最终只得到:

...
:provider              => 'AWS',
:aws_access_key_id     => ENV['S3_KEY'],
:aws_secret_access_key => ENV['S3_SECRET'],
:region                => ENV['S3_REGION']
...

在我的雾初始化器中。什么都不需要了。

于 2013-02-02T11:42:20.807 回答