使用fog / aws gem将Carrierwave上传到S3导致"没有这样的文件或目录@ rb_sysopen"

时间:2017-01-07 21:50:56

标签: amazon-s3 imagemagick carrierwave fog minimagick

我正在使用Carrierwave版本1.0.0rc将文件上传和处理到AWS S3存储桶。这是我的环境:

Rails 4.2.0 Ruby 2.1.1 MiniMagick 4.5.1 ImageMagick 6.9.7-0

我的上传器确定要上传的原始图片是横向还是纵向,并相应地应用处理规则。该文件上传到AWS S3存储桶,但随后出现以下错误:

Errno :: ENOENT in SponsorsController #create 没有这样的文件或目录@ rb_sysopen - uploads / sponsor / logo / 30 / Breen_Electrical_Logo.jpg

并且提取的源代码突出显示了此代码:

      image = MiniMagick::Image.open(picture.path)

以下是我的上传代码:

class LogoUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  storage :fog

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

  version :landscape, if: :is_landscape?

  version :portrait, if: :is_portrait?

  version :landscape do
      process resize_to_fit: [200, 50]
  end

  version :landscape_sm, from_version: :landscape do
      process resize_to_fit: [100, 25]
  end

  version :portrait do
      process resize_to_fit: [50, 200]
  end

  version :portrait_sm, from_version: :portrait do
      process resize_to_fit: [25, 100]
  end

  private 

    def is_landscape? picture
      image = MiniMagick::Image.open(picture.path)
      image[:width] > image[:height]
    end

    def is_portrait? picture
      image = MiniMagick::Image.open(picture.path)
      image[:width] < image[:height]
    end
  end

私有方法似乎打开文件来比较它的宽度和高度值。当我将文件存储在本地公用文件夹中时,这很好用。我在猜测&#34; picture.path&#34; url未指向S3存储桶路径以打开文件。

这是我的/config/initializers/carrierwave.rb文件

require 'fog/aws'

CarrierWave.configure do |config|
  config.fog_provider = 'fog/aws'            
  config.fog_credentials = {
    provider:              'AWS',              
    aws_access_key_id:     <access_key_id>,      
    aws_secret_access_key: <secret_access_key>,     
    region:                'us-west-2',                  
    :path_style            => true
  }
  config.fog_directory  = <bucketname>  
end

我似乎无法找到其他人遇到同样的问题。有任何想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为您需要直接引用该文件,而不仅仅是路径,因为它不是本地的。类似的东西:

ruby image = MiniMagick::Image.open(picture.file)