mercredi 21 septembre 2016

Moving CKeditor images from instance local to S3 URL's getting changed and access issues?

In my Rails app I was using ckeditor with Carrierwave with storage set as :file. Because of this setup whenever a user used to upload an image from CKEditor it would save in instance /public/system/ckeditor and then I had an command to sync it to S3 so that other instances could access the same image.

So whenever I would render the images in the view page, the image path for all images would point to that particular instance's location(/public/system/ckeditor), which would access the image from S3 in turn because S3 was mounted, and there was no need to authenticate separately or have an expiry URL. The images were stored in S3 as private.

It was working fine except few times when s3 used to get unmounted, I had to remount it.

Now I want to remove this process of storing the image in local instance and then syncing it. So I have made following changes to s3.rb:

CarrierWave.configure do |config|
  config.fog_credentials = {
      :provider               => 'AWS',
      :aws_access_key_id      => "klajhdfaljkdjklasd",
      :aws_secret_access_key  => "alksdjklajdlkajsxxxxxxxxx",
      :region                 => 'us-east-1'
  }
  config.fog_public     = false  
  config.fog_directory  = "s3-bucket"
end

Now the images are directly uploaded as private, but while reading the images I get 403 Forbidden in browser console.

There is one more problem in CKEditor images migration, the URL is hardcoded in the database, how can I change it as there are over 1 million records.

Similarly I have paperclip which was storing images in local which was being synced to s3, and then the images were accessed via instance through S3. But now I get 403 same as that of CKEditor.

PAPERCLIP_STORAGE_OPTIONS = {
   :styles => { :thumb25 => "25x25>", :thumb50 => "50x50>", :thumb250 => "250x250>"  },
   :url => "/system/:class/:id",
   :storage => :s3,
    :bucket => 's3-bucket',
    :s3_credentials => {:access_key_id => "ajkhdasjhdjkanhsdjknasd", :secret_access_key => "adasdasdasd+aadasd"},
    :s3_protocol => :https,
    :s3_permissions => :private,
}

Now I have too many models having code for displaying images, If I have to add authenticate object code, that would require lot of rework both in paperclip and CKEditor case.

Please let me know If there is any more clarification needed.

Aucun commentaire:

Enregistrer un commentaire