当使用Carrierwave-aws gem,Rails4和Heroku时,为什么我的存储桶在Amazon S3中为空

时间:2014-03-12 12:19:26

标签: ruby-on-rails heroku amazon-web-services amazon-s3 carrierwave

我正在尝试将图片上传到Amazon S3,但它们仅保留在Heroku。使用config.asset_host宝石时,我应该在carrierwave.rb Carrierwave-aws放入什么内容?

这是我的上传者 - > pictureuploader.rb

class PictureUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick

   include CarrierWave::MiniMagick
   version :thumb do
  process :resize_to_fill => [150, 150]
end

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

   def cache_dir
    "#{Rails.root}/tmp/uploads"
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
   def default_url
       ActionController::Base.helpers.asset_path("fallback/#{version_name || :thumb}/default.png")
   end



  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
   def extension_white_list
     %w(jpg jpeg gif png)
   end
end

这是我的配置 - &gt;初始化程序 - &gt; carrierwave.rb

CarrierWave.configure do |config|
  config.storage    = :aws
  config.aws_bucket = ENV['S3_BUCKET_NAME']
  config.aws_acl    = :public_read
  config.asset_host = ''
  config.aws_authenticated_url_expiration = 60 * 60 * 24 * 365


  config.aws_credentials = {
    access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
  }
end

我使用Figaro gem存储Amazon S3凭据并将文件保存在.gitignore中。我做了&gt;&gt; rake figaro:heroku

我的宝石文件:

source 'https://rubygems.org'
ruby '2.0.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'


# Use sqlite3 as the database for Active Record
group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

gem 'carrierwave'

gem 'carrierwave-aws'

gem 'figaro'

gem 'aws-sdk'

gem 'mini_magick', '3.5.0'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

gem 'bootstrap-sass', '2.3.2.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

group :test, :development do
  gem "rspec-rails", "2.13.1"
end

group :test do
  gem "capybara", "2.1.0"
end

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

在我的event.rb模型中,我添加了:

    require 'carrierwave/orm/activerecord'
mount_uploader :image_file_name, PictureUploader

show.html.erb视图中,我有:

<%= image_tag(@event.image_file_name_url, :width => 400) if @event.image_file_name.present? %>

我可以上传图片,但在我的亚马逊AWS控制台上,它说我的存储桶是空的。那么目前他们只留在Heroku?我接下来可以尝试什么?

1 个答案:

答案 0 :(得分:0)

我设法通过将存储选项更改为:aws并更改production.rb

来实现此功能

config.action_controller.asset_host =“https://s3-us-west-1.amazonaws.com/mybucketname

所以现在这个问题已经解决,因为存储桶不再是空的。

但是现在我有一个新问题, CSS doesn't load on Heroku, when I enabled an asset server (AWS) to serve images? Using Rails4, Carrierwave, Heroku, Amazon S3因为我的解决方案打破了CSS。