在将我们的应用程序从Rails 3.2.22升级到Rails 4.2.11时,在升级之前工作的几个CarrierWave方法现在返回nil,例如.url和.file.我们在AWS S3上托管我们的资产,我们的应用程序托管在Heroku上.奇怪的是,图像在我们的开发环境中按预期工作.
CarrierWave 1.3.1(从0.11.2升级)
Rails 4.2.11,从3.2.22升级
Ruby 2.2.5
fog-aws 3.3.0
到目前为止,我已尝试更改了carrierwave配置,并在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
我希望能够在进入时从S3返回资产路径
art.url
在开发中我得到art.url => “https://qa-marmosetmusic-com.s3.amazonaws.com/uploads/artist/photo/173/af2ac015-1d44-4481-b5d6-5bf5cb8c0e23.jpg”
但是在制作中,art.url =>零.
当我在开发控制台中输入art时,它返回此对象:
=> #<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={}>
当我在生产中输入相同的内容时,它返回:
#<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行为并导致了这个问题.删除该文件允许一切按预期工作.