简体   繁体   中英

CKEditor gem with Paperclip and Amazon S3

I'm using CKEditor and I've configured it to work with Paperclip but I can't tell it to store files in S3, so it's storing them using Paperclip but on the local filesystem.

So I was wondering if there is some way to tell Paperclip to explicitly use S3 everytime it's used.

I know how I can configure Paperclip with S3 on certain models (pretty easy, described on the paperclip github wiki). I'm deploying on Heroku that's why I can't write to the local filesystem.

One way is to see what the ckeditor install generator is doing. For example, if using ActiveRecord as ORM, take a look at the templates being used for the models that use Paperclip here .

The generator actually copies this templates into your app/models/ckeditor folder. You could edit them and configure as needed for Paperclip to use S3.

For ActiveRecord, the models are:

/app/models/ckeditor/attachment_file.rb
/app/models/ckeditor/picture.rb

Keep in mind that this approach could give you extra work in the future if the ckeditor gem is updated and the update process needs to overwrite this models.

Else, you can use Paperclip default options. In you Paperclip initializer (/config/initializers/paperclip.rb) use:

Paperclip::Attachment.default_options.merge!(
    YOUR OPTIONS FOR S3 HERE
) 

For carrierwave, you can generate the uploader and there you can configure your s3 or whatever you want.

class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
  include Ckeditor::Backend::CarrierWave
  # Choose what kind of storage to use for this uploader:
  if Rails.env.production?
    storage :fog
  else
    storage :file
  end
....
end

It's pretty straight forward. You can use this post to get you started. Alternatively, you can look at this similar question for further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM