我正在使用Ruby on Rails构建一个网站。要上传图像,我使用的是Active Storage和Amazon S3。一切都很好。用户可以在网站上上传图像和图像(图像是公开的)。
现在,在制作中,图像的网址是:https://example.com/rails/active_storage/representations/1ej21h ...
哪个返回302到S3桶:https://my-bucket.amazonaws.com/variants/9jdh2 ...
我不是一个忠实的粉丝:
我宁愿使用Cloudfront来提供这些图像。
我在Rails指南,Google和StackOverflow上搜索过,但到目前为止找不到合适的答案。
目前是否有任何解决方案将Cloudfront与Active Storage配合使用?
编辑:更多上下文:每个图像将至少在正常流量和来自不同国家/地区的1000分钟内加载。我不想让服务器承受这种压力(它还有其他处理请求)。我希望用户尽快加载这些图像。因此,Cloudfront作为这些图像的CDN(公共图像,无需获取签名的URL)。
试试这个...
在controllers/active_storage/representations_controller.rb
module ActiveStorage
class RepresentationsController < BaseController
include ActiveStorage::SetBlob
def show
expires_in 1.year, public: true
variant = @blob.representation(params[:variation_key]).processed
send_data @blob.service.download(variant.key),
type: @blob.content_type || DEFAULT_SEND_FILE_TYPE,
disposition: 'inline'
end
end
end
然后,当您使用@model.image.variant(resize: '250x250')
调用图像时,请确保替换所需的尺寸。这是现在的黑客攻击。这应该由rails 6释放我认为。