Carrierwave uploads to S3 using fog/aws gem result in "No such file or directory @ rb_sysopen"

1k views Asked by At

I am using Carrierwave version 1.0.0rc to upload and process files to AWS S3 bucket. Here is my environment:

Rails 4.2.0 Ruby 2.1.1 MiniMagick 4.5.1 ImageMagick 6.9.7-0

My uploader determines if the original image to be uploaded is landscape or portrait and will apply processing rules accordingly. The file uploads to the AWS S3 bucket, but then I get the following error:

Errno::ENOENT in SponsorsController#create No such file or directory @ rb_sysopen - uploads/sponsor/logo/30/Breen_Electrical_Logo.jpg

and the extracted source shows this code highlighted:

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

Here is my uploader code:

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

The private methods seem to opening up the file to compare its width and height values. This worked just fine when I was storing the files in the local public folder. I am guessing that the "picture.path" url is not pointing up to the S3 bucket path to open the file.

Here is my /config/initializers/carrierwave.rb file

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

I can't seem to find others having the same issue. Any ideas? Thanks in advance.

1

There are 1 answers

2
geemus On

I think you'll need to refer to the file directly, instead of just the path, since it wouldn't be local. Something like:

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