我正在使用Rails + Carrierwave上传图像,这些图像存储在S3上并通过Cloudfront CDN提供服务。网站上的所有图像都可以毫无问题地提供,但是在实施Ckeditor(gem)之后,我对通过Ckeditor上传的图像一直遇到问题。它们已正确上载到S3,并最初显示,但过了一段时间就会过期。 This is the expiry message when I access the image src after it has expired
我已经搜索了Stack Overflow,但没有找到与该主题相关的答案。我了解这是因为Ckeditor将预签名的URL发送到S3,并且这些预签名的URL指定了到期时间。我尝试将config.fog_public = true设置为无效(路由问题和图像无法上传),并且尝试将S3仪表板中的ckeditor文件夹设为公开,这也是行不通的。
有什么方法可以使config.fog_public = true对我的Ckeditor上传有效,或者是否有其他方法可以确保Ckeditor发送到S3的预签名URL不会过期?
我尝试过的事情:
这是我的上传者> ckeditor_picture_uploader.rb文件
require 'carrierwave'
class CkeditorPictureUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave
include CarrierWave::MiniMagick
if Rails.env.production?
storage :fog
else
storage :file
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
def store_dir
"uploads/ckeditor/pictures/#{model.id}"
end
process :extract_dimensions
version :thumb do
process resize_to_fill: [118, 100]
end
version :content do
process resize_to_limit: [800, 800]
end
def extension_white_list
Ckeditor.image_file_types
end
configure do |c|
c.fog_public = true
end
end
However, this results in there being a routing error and Ckeditor no longer shows the image
This is the routing error in my logs.
这是我的上传者> Carrierwave的avatar_uploader.rb文件
class AvatarUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
def cache_dir
"#{Rails.root}/tmp/uploads"
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
process resize_to_fit: [960, 640]
version :medium do
process resize_to_fit: [500, 500]
end
version :thumb do
process resize_to_fit: [400, 400]
end
version :s_thumb do
process resize_to_fit: [90, 90]
end
version :xs_thumb do
process resize_to_fit: [5, 5]
end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
这是我的初始设置器> carrierwave.rb文件
CarrierWave.configure do |config|
config.fog_provider = 'fog/aws' # required
config.root = Rails.root.join('tmp') # adding these...
config.cache_dir = 'carrierwave' # ...two lines
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
region: ENV["S3_REGION"]
}
config.fog_directory = ENV["S3_BUCKET_NAME"]
config.fog_public = false
config.fog_attributes = { cache_control: "public, max-age=#{365.day.to_i}" }
config.storage = :fog
config.asset_host = "https://d18sx48tl6nre5.cloudfront.net"
end
这是我的初始化程序> ckeditor.rb文件
# Use this hook to configure ckeditor
Ckeditor.setup do |config|
require 'ckeditor/orm/active_record'
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default), :mongo_mapper and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
# available as additional gems.
# Allowed image file types for upload.
# Set to nil or [] (empty array) for all file types
# By default: %w(jpg jpeg png gif tiff)
config.image_file_types = %w(jpg jpeg png gif tiff)
# Allowed flash file types for upload.
# Set to nil or [] (empty array) for all file types
# By default: %w(jpg jpeg png gif tiff)
# config.flash_file_types = %w(swf)
# Allowed attachment file types for upload.
# Set to nil or [] (empty array) for all file types
# By default: %w(doc docx xls odt ods pdf rar zip tar tar.gz swf)
config.attachment_file_types = %w(doc docx xls odt ods pdf rar zip tar tar.gz swf)
# Setup authorization to be run as a before filter
# By default: there is no authorization.
# config.authorize_with :cancan
# Override parent controller CKEditor inherits from
# By default: 'ApplicationController'
# config.parent_controller = 'MyController'
# Asset model classes
# config.picture_model { Ckeditor::Picture }
# config.attachment_file_model { Ckeditor::AttachmentFile }
# Paginate assets
# By default: 24
# config.default_per_page = 24
# Customize ckeditor assets path
# By default: nil
# config.asset_path = 'http://www.example.com/assets/ckeditor/'
# To reduce the asset precompilation time, you can limit plugins and/or languages to those you need:
# By default: nil (no limit)
# config.assets_languages = ['en', 'uk']
# config.assets_plugins = ['image', 'smiley']
# CKEditor CDN
# More info here http://cdn.ckeditor.com/
# By default: nil (CDN disabled)
config.cdn_url = '//cdn.ckeditor.com/4.7.1/standard/ckeditor.js'
# JS config url
# Used when CKEditor CDN enabled
# By default: "ckeditor/config.js"
config.js_config_url = 'ckeditor/config.js'
end
这是我的视图> new.html.erb文件
<%= javascript_include_tag Ckeditor.cdn_url %>
<div class = "story_form">
<div class = "container">
<%= form_for @story, url: stories_path do |f| %>
<%= f.text_field :title, placeholder: "Title", style: "font-size: 30px; font-weight: 700;", class: 'form-control', :required => true %>
<br>
<%= f.text_field :sub_title, placeholder: "Subtitle", style: "font-size: 22px; font-weight: 400;", class: 'form-control', :required => true%>
<br>
<div class = "story_image_sect">
<i class = "fa fa-2x fa-camera"></i>
<%= f.file_field :lead_image, :required => true %>
<%= f.hidden_field :lead_image_cache %>
</div>
<br>
<%= f.cktext_area :body, class: 'form-control'%>
<br>
<%= f.submit "Preview Draft", class: 'btn-block btn-subscribe', id: 'newstoryBtn' %>
<%= link_to "Back to all Stories", stories_admin_path, class: "btn-block btn-subscribe", style: "background-color: #26527F; text-align: center;"%>
<%end%>
</div>
</div>