从 Rails 3 升级到 Rails 4 后 CarrierWave url nil

CarrierWave url nil after upgrading from Rails 3 to Rails 4

在将我们的应用程序从 Rails 3.2.22 升级到 Rails 4.2.11 时,升级前可用的几种 CarrierWave 方法现在 return 为零,例如 .url.file。我们在 AWS S3 上托管我们的资产,我们的应用程序托管在 Heroku 上。奇怪的是,图像在我们的开发环境中按预期工作。

CarrierWave 1.3.1 (upgraded from 0.11.2)

Rails 4.2.11, upgraded from 3.2.22

Ruby 2.2.5

fog-aws 3.3.0

到目前为止,我已尝试更改载波配置,并在 rails 控制台中尝试不同的方法,并将开发结果与生产结果进行比较。

Gemfile

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

gem 'rails', '4.2.11'

gem 'puma'
gem 'pg', '0.15.1'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'rack-cors'
gem 'sprockets_uglifier_with_source_maps'
gem 'carrierwave'
gem 'mini_magick', '3.6.0'
gem 'paper_trail', '>= 4.0'
gem 'fog-aws'
gem 'aws-sdk-s3', '~> 1'
gem 'identity_cache', '>= 0.2'
gem 'memcachier'
gem 'dalli'
gem 'connection_pool'
gem 'active_model_serializers', '>= 0.10'
gem 'formtastic', '~> 3.1.0'
gem 'sprockets-rails', require: 'sprockets/railtie'
gem 'sprockets-image_compressor'
gem 'actionpack-action_caching'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '2.5.0'
gem 'compass-rails'
gem 'sprite-factory', '1.5.3'
gem 'responders', '~> 2.0'

group :development do
  gem 'rack-mini-profiler'
  gem 'rmagick', '2.13.2'
end

carrierwave.rb


CarrierWave.configure do |config|
  config.fog_provider = 'fog/aws'
  config.fog_credentials = {
      provider: ENV['FOG_PROVIDER'],
      aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      region: ENV['FOG_REGION'],
      endpoint: ENV['FOG_HOST']
    }
  if Rails.env.production?
    config.storage = :fog
    config.fog_directory = ENV['FOG_DIRECTORY']
    config.fog_public = false
    config.fog_authenticated_url_expiration = 1800
  else
    config.asset_host = "#{ENV['FOG_HOST']}"
    config.storage = :file
    config.enable_processing = false if Rails.env.test?
  end
end
art_uploader.rb

# encoding: utf-8

class ArtUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  version :thumbnail do
    process resize_to_fill: [100, 100]
  end

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Add a white list of extensions which are allowed to be uploaded.
  def extension_white_list
    %w(jpg jpeg png)
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  def default_url
    if model.has_fallback_art?
      version_name == :thumbnail  
      model.song.artist.photo.small_thumb.url : 
      model.song.artist.photo.album_art.url
    end
  end

end

我希望在进入时能够 return 来自 S3 的资产路径 art.url

在开发中我得到 art.url => "https://qa-marmosetmusic-com.s3.amazonaws.com/uploads/artist/photo/173/af2ac015-1d44-4481-b5d6-5bf5cb8c0e23.jpg"

但是在生产中 art.url => nil

当我在开发控制台中输入 art 时,它 return 是这个对象:

=> #<ArtUploader::Uploader70128841541160:0x007f904c7c88f8
 @cache_id=nil,
 @file=
  #<CarrierWave::SanitizedFile:0x007f904c1ffd68
   @content=nil,
   @content_type=nil,
   @file=
    "/Users/ekingan/dev/marmoset/public/uploads/album/art/3540/thumbnail_Evan_Andree_Your_Heart_Album_Artwork_1_3000x3000.jpg",
   @original_filename=nil>,
 @filename=nil,
 @format=nil,
 @model=
  #<Album:0x007f904a0417a8
   id: 3540,
   title: "Your Heart",
   art: "Evan_Andree_Your_Heart_Album_Artwork_1_3000x3000.jpg",
   state: "active",
   description: "",
   created_at: Tue, 27 Nov 2018 12:50:14 PST -08:00,
   updated_at: Tue, 27 Nov 2018 12:50:14 PST -08:00>,
 @mounted_as=:art,
 @parent_version=
  #<ArtUploader:0x007f904c7c9618
   @cache_id=nil,
   @file=
    #<CarrierWave::SanitizedFile:0x007f904c7c8948
     @content=nil,
     @content_type=nil,
     @file="/Users/ekingan/dev/marmoset/public/uploads/album/art/3540/Evan_Andree_Your_Heart_Album_Artwork_1_3000x3000.jpg",
     @original_filename=nil>,
   @filename=nil,
   @format=nil,
   @model=
    #<Album:0x007f904a0417a8
     id: 3540,
     title: "Your Heart",
     art: "Evan_Andree_Your_Heart_Album_Artwork_1_3000x3000.jpg",
     state: "active",
     description: "",
     created_at: Tue, 27 Nov 2018 12:50:14 PST -08:00,
     updated_at: Tue, 27 Nov 2018 12:50:14 PST -08:00>,
   @mounted_as=:art,
   @storage=#<CarrierWave::Storage::File:0x007f904c7c9118 @cache_called=nil, @uploader=#<ArtUploader:0x007f904c7c9618 ...>>,
   @versions={:thumbnail=>#<ArtUploader::Uploader70128841541160:0x007f904c7c88f8 ...>}>,
 @storage=
  #<CarrierWave::Storage::File:0x007f904c7c86f0
   @cache_called=nil,
   @uploader=#<ArtUploader::Uploader70128841541160:0x007f904c7c88f8 ...>>,
 @versions={}>

当我在生产中输入相同的内容时 returns:

#<ArtUploader::Uploader6168740:0x000000072781c8 @model=#<Album id: 2008, title: "Vektlaus", art: "Daniel+Kvammen+-+Vektlaus.jpg", state: "active", description: nil, created_at: "2017-05-19 18:40:43", updated_at: "2017-07-26 16:51:17">, @mounted_as=:art, @file=nil, @filename=nil, @cache_id=nil, @versions={}, @format=nil, @parent_version=#<ArtUploader:0x00000007278d30 @model=#<Album id: 2008, title: "Vektlaus", art: "Daniel+Kvammen+-+Vektlaus.jpg", state: "active", description: nil, created_at: "2017-05-19 18:40:43", updated_at: "2017-07-26 16:51:17">, @mounted_as=:art, @file=nil, @filename=nil, @cache_id=nil, @versions={:thumbnail=>#<ArtUploader::Uploader6168740:0x000000072781c8 ...>}, @format=nil, @storage=#<CarrierWave::Storage::Fog:0x000000072786f0 @uploader=#<ArtUploader:0x00000007278d30 ...>>>, @storage=#<CarrierWave::Storage::Fog:0x00000007267300 @uploader=#<ArtUploader::Uploader6168740:0x000000072781c8 ...>>>

我很想听听任何建议。 谢谢!

我们解决了这个问题!

我们在 carrierwave/storage 中有一个名为 fog.rb 的文件,它覆盖了 gem 行为并导致了这个问题。删除该文件允许一切按预期工作。