简体   繁体   English

Rails + Carrierwave + Ckeditor + Fog + S3 - 文件没有指向亚马逊

[英]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. 我试图在S3上运行我的静态文件,一切都很好。 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. 但是通过ckeditor推送到博客的所有内容都不是指向亚马逊主机,而是指向应用程序网址。

this is the configuration 这是配置

config/initializers/fog.rb 配置/初始化/ 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 车型/ 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 上传/ 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/ ... 无论如何,其他上传者图片的网址是https://bucket.s3-eu-west-1.amazonaws.com/uploads/ ... ckeditor图片的网址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 有些人提到了公共/私有图片的问题,但这不是我的问题,因为URL不正确

Did you try restarting the Rails server? 您是否尝试重新启动Rails服务器?

Try to create config/initializers/carrierwave.rb instead of lib/carrierwave/storage/fog.rb https://github.com/carrierwaveuploader/carrierwave/tree/0.11-stable#configuring-carrierwave 尝试创建config/initializers/carrierwave.rb而不是lib/carrierwave/storage/fog.rb https://github.com/carrierwaveuploader/carrierwave/tree/0.11-stable#configuring-carrierwave

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 首先你要知道,ckeditor将图像的src硬编码到数据库中,所以当ckeditor carrierwave被修改为从旧源指向s3时,旧链接被破坏并且必须通过转储表来修复

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

and then editing it by sed or some text editor that can do find and replace 然后通过sed或一些可以查找和替换的文本编辑器进行编辑

nano table.dump + ctrl + w + r

then search for "/uploads/ and replace with " https://bucket.amazonaws.com/uploads/ 然后搜索“/ uploads /并替换为” 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. 我刚刚没有测试它,因为我先查看现有图片。

PS: Just to make sure this would work even in the future. PS:只是为了确保即使将来也可以使用。 I made the configuration public so the src links were not stored with all the details that might expire and edited bucket policy to 我将配置公开,因此src链接未存储可能过期的所有详细信息并将桶策略编辑到

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

Try reseting your database. 尝试重置您的数据库。 Or dropping the table where your ckeditor photo urls were stored previous to pointing them to fog/aws. 或者将表格放在你的ckeditor照片网址存储之前,然后将它们指向雾/ aws。 Worked for me. 为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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