Michael
Michael

Reputation: 37

Rails Engine not uploading to s3 with Carrierwave

I am creating a rails plugin with rails new plugin my_plugin --mountable

This was quite some work to figure out but it is supposed to upload files to S3 with carrierwave, but it says ok but nothing is uploaded

Carrierwave is used to generate an uploader with rails g uploader photo the file looks like this

# my_engine/app/uploaders/my_engine/photo_uploader.rb

# encoding: utf-8
module my_engine
 class PhotoUploader < CarrierWave::Uploader::Base
  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # 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
 end
end

the model had an mount :photo, PhotoUploader

module PdfGeneratorEngine
  class Assemble < ActiveRecord::Base
    attr_accessible :color, :photo, :qr_code_url, :text

    mount_uploader :photo, PhotoUploader
  end
end

my CarrierWave config file is this

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => 'MY_ACCES_KEY',
    :aws_secret_access_key  => 'MY_SECRET_KEY',
    :provider => 'AWS',
    :region => 'eu-west-1'
  }
  config.fog_directory  =  'my.bucket.com'
  config.fog_host       =  'https://s3-eu-west-1.amazonaws.com/my.bucket.com'
  config.storage = :fog
  config.s3_use_ssl = true
  config.fog_public = true
end

So first of all it starts screaming at fog_host, it is okay if it is asset_host

Next it's problem lies within s3_use_ssl, while it is an merged issue on CarrierWave's github. But the host is already defined as https:// so I don't see why that line is necessary.

After that it says 'Okay it's done' and when I try to check (with a deamon) for the file, there's nothing there.

What did I miss? Or is there something of an issue with CarrierWave and Rails mountable engines?

Upvotes: 2

Views: 2290

Answers (2)

Michael
Michael

Reputation: 37

Okay So there is a bit of a problem with CarrierWave.

I have quickly setup RightAws and now it uploads to S3 and I can find it from my deamon.

in my uploader I added

   @s3 = RightAws::S3Interface.new('MY KEY', 'MY SECRET KEY')
   @s3.put('my.bucket.com', assemble.photo.identifier ,params[:assemble][:photo])

Thanks for your help Nishant, CarrierWave would be a lot slicker and nicer but it currently does not work. There has been an issue for this in their github regarding use in Rails engines.

Upvotes: 0

Nishant
Nishant

Reputation: 3005

In your photo_uploader.rb

comment storage:file and uncomment storage:fog

  # storage :file
  storage :fog   

-- Look at your fog.rb, Its inconsistent with what is given here

carrierwave#using-amazon-s3

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => 'xxx',                        # required
    :aws_secret_access_key  => 'yyy',                        # required
    :region                 => 'eu-west-1'                   # optional, defaults to 'us-east-1'
    :hosts                  => 's3.example.com'              # optional, defaults to nil
    :endpoint               => 'https://s3.example.com:8080' # optional, defaults to nil
  }
  config.fog_directory  = 'name_of_directory'                     # required
  config.fog_public     = false                                   # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end

Upvotes: 1

Related Questions