我正在使用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来提供这些图像。
我在Google和StackOverflow上的《 Rails指南》中进行了搜索,但到目前为止找不到合适的答案。
目前是否有将Cloudfront与Active Storage结合使用的解决方案?
编辑: 更多内容:每张图片至少在正常流量和来自不同国家/地区的情况下,每分钟至少被加载1000次。我不想让服务器承受这种压力(它还有其他要求要处理)。我希望用户尽快加载这些图像。因此,Cloudfront作为这些图像(公共图像,无需获取签名的URL)的CDN。
答案 0 :(得分:0)
尝试一下...
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 release修复。
答案 1 :(得分:0)
我总是将我的负载均衡器放在一个 cloudfront 分配之后,并且我将 Route53 域指向该分配,而不是负载均衡器。这样您就可以缓存您想要的任何行为。当然,您可以缓存对 rails/active_storage/representations/redirect/* 的任何请求,并且它将保留应用服务器在第一次检索图像时返回的任何缓存标头。