ruby-on-railsamazon-s3carrierwavesidekiq

Carrierwave processed images not uploading to AWS S3


Similar problem to this question but the solution provided did not fix my problem. Carrierwave processed images not uploading to S3

Using Railscast #383 as basis for code: http://railscasts.com/episodes/383-uploading-to-amazon-s3

The image is successfully uploaded to S3 using carrierwave_direct. I want to process the images in the background with sidekiq and then upload to S3.

The sidekiq worker completes the image processing without error but the processed images (:thumb and :large) are stored locally in the public/uploads folder.

Any idea why the processed images are not being uploaded to S3?

Uploader:

class ImageUploader < CarrierWave::Uploader::Base

include CarrierWave::MiniMagick

storage :fog

include CarrierWave::MimeTypes
process :set_content_type

include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper

version :thumb do
  process :resize_to_limit => [200, 200]
end

version :large do
  process :resize_to_limit => [600, 400]
end

def extension_white_list
   %w(jpg jpeg gif png)
end
end

Model:

class Photo < ActiveRecord::Base
attr_accessible  :name, :image, :remote_image_url, :user_id
mount_uploader :image, ImageUploader

belongs_to :user

after_commit :enqueue_image, :on => :create

has_many :comments, as: :commentable

def image_name
        File.basename(image.path || image.filename) if image
end

def enqueue_image
    ImageWorker.perform_async(id, key) if key.present?
end

class ImageWorker
    include Sidekiq::Worker
    sidekiq_options retry: false

        def perform(id, key)
          photo = Photo.find(id)
          photo.key = key
          photo.remote_image_url = photo.image.direct_fog_url(with_path: true)
          photo.save!
        end
    end

end

PhotoController

class PhotosController < ApplicationController

 def index
  @photos = Photo.all
  @uploader = Photo.new.image
  @uploader.success_action_redirect = new_photo_url
 end

View

.
. 
.
<%= direct_upload_form_for @uploader do |f| %>
  <p><%= f.file_field :image %></p>
  <p><%= f.submit "Upload Photo" %></p>
<% end %>

initializers/carrierwave.rb

 CarrierWave.configure do |config|
   config.fog_credentials = {
    provider: "AWS",
    aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
    aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"]
  }
  config.fog_directory = ENV["AWS_S3_BUCKET"]
end

initializers/fog.rb *added this based on the response to the question linked above

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => ENV["AWS_ACCESS_KEY_ID"],
    :aws_secret_access_key  => ENV["AWS_SECRET_ACCESS_KEY"],
   :region                 => 'us-east-1'
  }

 config.fog_directory = ENV["AWS_S3_BUCKET"]
 config.fog_public = true
 config.fog_attributes = {'Cache-Control' => 'max-age=315576000'} 
end

Solution

  • In my uploaders folder I had copied my original uploader and renamed it 'image_uploader_file.rb'. I then modified the origial version 'image_uploader.rb' to use fog and S3. I assumed that carrierwave would only use 'image_uploader.rb' but it appears that it loaded both files. The old version used local storage so that is where carrierwave stored the images. Once I deleted 'image_uploader_file.rb' the processed images were properly uploaded to S3.