Search code examples
ruby-on-railsamazon-s3ckeditorcarrierwavefog

Rails + Carrierwave + Ckeditor + Fog + S3 - files are not pointing to amazon


I am trying to run my static files on S3 and everything works great. Files are available, rendered and uploaded without any problem. But everything that is pushed to blogs via ckeditor is not pointing to amazon host, but application url instead.

this is the configuration

config/initializers/fog.rb

CarrierWave.configure do |config|
  config.cache_dir = "#{Rails.root}/tmp/uploads" 

  config.storage = :fog

  config.fog_credentials = {
    provider:              'AWS',
    aws_access_key_id:     Rails.application.secrets.app_s3_username,
    aws_secret_access_key: Rails.application.secrets.app_s3_password,
    region:                Rails.application.secrets.app_s3_region
  }
  config.fog_directory  = Rails.application.secrets.app_s3_bucket
  config.fog_public     = false
  config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" }
end

models/ckeditor/picture.rb

class Ckeditor::Picture < Ckeditor::Asset
  mount_uploader :data, CkeditorPictureUploader, :mount_on => :data_file_name

  def url_content
    url(:content)
  end

end

uploaders/ckeditor_picture_uploader.rb

# encoding: utf-8
class CkeditorPictureUploader < CarrierWave::Uploader::Base
  include Ckeditor::Backend::CarrierWave

  # Include RMagick or ImageScience support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick
  # include CarrierWave::ImageScience

  # Choose what kind of storage to use for this uploader:
  storage :fog
.
.
.

installed gems

  * carrierwave (0.11.2)
  * ckeditor (4.1.6)
  * fog (1.37.0)

anyway the url of other uploaders images is https://bucket.s3-eu-west-1.amazonaws.com/uploads/... the url of ckeditor images http://example.com/uploads/...

any suggestion and help would be appreciated as i tried and searched the internet and wasn't able to find anyone having similar issue. Some mentioned a problem with public / private images, but that is not my issue as the URL is incorrect


Solution

  • The problem can be resolved in few simple steps:

    First you have to know, that ckeditor hardcodes the src of images into the database, so when ckeditor carrierwave was modified to point to s3 from old source, old links were broken and had to be fixed by dumping the table

    mysqldump -hhost -uuser -ppassword database table > table.dump
    

    and then editing it by sed or some text editor that can do find and replace

    nano table.dump + ctrl + w + r
    

    then search for "/uploads/ and replace with "https://bucket.amazonaws.com/uploads/

    of course only in case you save the folder structure what I did.

    The second issue with the upload wasn't an issue at all. I just haven't tested it as I was looking into existing pictures first.

    P.S.: Just to make sure this would work even in the future. I made the configuration public so the src links were not stored with all the details that might expire and edited bucket policy to

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AddPerm",
                "Effect": "Allow",
                "Principal": "*",
                "Action": "s3:GetObject",
                "Resource": "arn:aws:s3:::bucket/*"
            }
        ]
    }