gpi*_*hot 8 ruby-on-rails amazon-s3 amazon-cloudfront ruby-on-rails-5 rails-activestorage
我正在使用Ruby on Rails构建网站。要上传图像,我正在使用Active Storage和Amazon S3。一切都很好。用户可以上传图像,并且可以在网站上查看图像(图像是公开的)。
现在,在生产中,图像的URL为: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。
尝试这个...
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
Run Code Online (Sandbox Code Playgroud)
然后,当您调用图像时,@model.image.variant(resize: '250x250')
请确保替换您想要的尺寸。目前这是一个黑客攻击。我认为这应该由 Rails 6 版本修复。
归档时间: |
|
查看次数: |
1627 次 |
最近记录: |