CarrierWave 未上传(升级后)
CarrierWave not uploading (after upgrade)
一个月前,我将 RoR-app 上的 Ruby 版本从 1.9.3 升级到 2.3.3。在此之前,我已经成功地使用 fog
& carrierwave
将图像上传到 Amazon S3,但在升级后它停止工作了。它不再将我的图像上传到 S3,文件夹和文件也不会在那里创建。造成这个问题的原因是我根本无法解决它,我没有收到任何错误消息或任何东西。
版本:
- 雾 1.38.0
- 载波 1.0.0
- ruby2.3.3
- rails 4.1.13
有用信息:
- 我已经尝试了 fog/aws 和 carrierwave-aws,结果相同(失败)
- 如果我将 AWS 凭据更改为明显错误的内容,则会出现同样的问题。也就是说,CarrierWave 配置文件似乎有问题,或者它没有加载或其他什么
- 对象(礼物)正在正确保存
- 据我所知,我没有在亚马逊上做过任何更改。
- 我尝试切换到另一组 AWS 访问密钥和 ID,但遇到了同样的问题。
- 我在同一个应用程序(使用相同的凭据)上使用 AssetSync 上传到 AWS,这很有效! IE。 AWS/S3.
应该没有问题
initializers/carrierwave.rb
CarrierWave.configure do |config|
config.storage = 'fog'
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'ABC123CODE',
:aws_secret_access_key => '+ABC123CODEKEY',
:region => 'eu-central-1'
}
config.fog_directory = 'myapp-se'
config.cache_dir = "#{Rails.root}/tmp"
config.fog_attributes = {
expires: 1.year.from_now.httpdate,
cache_control: "max-age=#{1.year.to_i}"
}
config.ignore_integrity_errors = false
config.ignore_processing_errors = false
config.ignore_download_errors = false
end
礼物模型(gift.rb)-(相关部分)
mount_uploader :cached_image, GiftImageUploader
#validates_integrity_of :cached_image
#validates_processing_of :cached_image
validates_integrity_of :cached_image
validates_processing_of :cached_image
validates_download_of :cached_image
file = Tempfile.new(["image", ".jpg"])
file.binmode
begin
open(URI.parse(external_image_url), :allow_redirections => :safe) do |data|
file.write data.read
end
rescue Exception => e
logger.info "Error fetching image for product #{id}: " + e.message
return false
end
file.rewind
self.cached_image = file
begin
self.skip_callbacks = true
if self.save!
logger.info "Successfully cached image for product #{id}"
return true
else
logger.info "Soft Error saving product #{id}"
return false
end
self.skip_callbacks = false
rescue Exception => e
logger.info "Rescue Error saving product #{id}" + e.message
end
上传文件
class GiftImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :fog
def store_dir
"cached/gifts/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
version :s300 do
process resize_to_fit: [300, 300]
end
version :s200, :from_version => :s300 do
process resize_to_fit: [200, 200]
end
version :s150, :from_version => :s200 do
process resize_to_fit: [150, 150]
end
version :sq150 do
process resize_to_fill: [150, 150]
end
version :s75, :from_version => :s150 do
process resize_to_fit: [75, 75]
end
version :sq75 do
process resize_to_fill: [75, 75]
end
end
我需要什么帮助:
- 由于我的主要问题是这一切都默默地失败了,我什至无法开始对该功能进行故障排除。在我获得有关错误的一些信息之前,如果有任何关于我如何通过反复试验解决此应用程序故障的建议,我们将不胜感激。
- 如果您能在代码中看到任何可以解决问题的内容,那当然也非常有用!
您需要运行 bundle install
命令才能启动应用程序。如果不起作用,运行 bundle update
后跟 bundle install
。它应该可以工作,因为我过去也遇到过类似的问题。
我使用 carrierwave-aws gem 解决了这个问题。该解决方案对于我的特定应用来说过于复杂且过于具体,未来的读者可能不会感兴趣。坦率地说,我不太确定为什么这解决了这个问题。
一个月前,我将 RoR-app 上的 Ruby 版本从 1.9.3 升级到 2.3.3。在此之前,我已经成功地使用 fog
& carrierwave
将图像上传到 Amazon S3,但在升级后它停止工作了。它不再将我的图像上传到 S3,文件夹和文件也不会在那里创建。造成这个问题的原因是我根本无法解决它,我没有收到任何错误消息或任何东西。
版本:
- 雾 1.38.0
- 载波 1.0.0
- ruby2.3.3
- rails 4.1.13
有用信息:
- 我已经尝试了 fog/aws 和 carrierwave-aws,结果相同(失败)
- 如果我将 AWS 凭据更改为明显错误的内容,则会出现同样的问题。也就是说,CarrierWave 配置文件似乎有问题,或者它没有加载或其他什么
- 对象(礼物)正在正确保存
- 据我所知,我没有在亚马逊上做过任何更改。
- 我尝试切换到另一组 AWS 访问密钥和 ID,但遇到了同样的问题。
- 我在同一个应用程序(使用相同的凭据)上使用 AssetSync 上传到 AWS,这很有效! IE。 AWS/S3. 应该没有问题
initializers/carrierwave.rb
CarrierWave.configure do |config|
config.storage = 'fog'
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'ABC123CODE',
:aws_secret_access_key => '+ABC123CODEKEY',
:region => 'eu-central-1'
}
config.fog_directory = 'myapp-se'
config.cache_dir = "#{Rails.root}/tmp"
config.fog_attributes = {
expires: 1.year.from_now.httpdate,
cache_control: "max-age=#{1.year.to_i}"
}
config.ignore_integrity_errors = false
config.ignore_processing_errors = false
config.ignore_download_errors = false
end
礼物模型(gift.rb)-(相关部分)
mount_uploader :cached_image, GiftImageUploader
#validates_integrity_of :cached_image
#validates_processing_of :cached_image
validates_integrity_of :cached_image
validates_processing_of :cached_image
validates_download_of :cached_image
file = Tempfile.new(["image", ".jpg"])
file.binmode
begin
open(URI.parse(external_image_url), :allow_redirections => :safe) do |data|
file.write data.read
end
rescue Exception => e
logger.info "Error fetching image for product #{id}: " + e.message
return false
end
file.rewind
self.cached_image = file
begin
self.skip_callbacks = true
if self.save!
logger.info "Successfully cached image for product #{id}"
return true
else
logger.info "Soft Error saving product #{id}"
return false
end
self.skip_callbacks = false
rescue Exception => e
logger.info "Rescue Error saving product #{id}" + e.message
end
上传文件
class GiftImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :fog
def store_dir
"cached/gifts/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
version :s300 do
process resize_to_fit: [300, 300]
end
version :s200, :from_version => :s300 do
process resize_to_fit: [200, 200]
end
version :s150, :from_version => :s200 do
process resize_to_fit: [150, 150]
end
version :sq150 do
process resize_to_fill: [150, 150]
end
version :s75, :from_version => :s150 do
process resize_to_fit: [75, 75]
end
version :sq75 do
process resize_to_fill: [75, 75]
end
end
我需要什么帮助:
- 由于我的主要问题是这一切都默默地失败了,我什至无法开始对该功能进行故障排除。在我获得有关错误的一些信息之前,如果有任何关于我如何通过反复试验解决此应用程序故障的建议,我们将不胜感激。
- 如果您能在代码中看到任何可以解决问题的内容,那当然也非常有用!
您需要运行 bundle install
命令才能启动应用程序。如果不起作用,运行 bundle update
后跟 bundle install
。它应该可以工作,因为我过去也遇到过类似的问题。
我使用 carrierwave-aws gem 解决了这个问题。该解决方案对于我的特定应用来说过于复杂且过于具体,未来的读者可能不会感兴趣。坦率地说,我不太确定为什么这解决了这个问题。