Reputation: 31
I've configured Carrierwave+Fog to work with Amazon S3. The problem I have is that everything is created in the public folder of my project and isn't uploaded to S3. The thumb and the dir are OK, is only the placement of the image that is not working.
My uploader:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_fit => [200, 200]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'xxx',
:aws_secret_access_key => 'yyy',
:region => 'eu-west-1'
}
config.fog_directory = 'your_bucket_here'
config.fog_public = true
config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}
end
The class that uses it:
class Image < ActiveRecord::Base
belongs_to :product
mount_uploader :remote_file, ImageUploader
end
And the controller code:
i = Image.new
i.save
i.remote_file = params[:image]
render :json => {:response => i.remote_file.url}
The response is: {"response":"/uploads/tmp/1387464252-27678-6793/logo11w.png"}
And the image is created inside the public dir of the rails project.
Thank you!
Upvotes: 2
Views: 718
Reputation: 24478
CarrierWave performs the upload with ActiveRecord before_save and after_save callbacks. In your controller, set the remote_file
before you call save
:
i = Image.new
i.remote_file = params[:image]
i.save
Upvotes: 2