ruby-on-rails - 将 Cloudfront 与 Active Storage 结合使用
问题描述
我正在使用 Ruby on Rails 构建一个网站。要上传图片,我使用 Active Storage 和 Amazon S3。这里一切都很好。用户可以上传图片,图片可以在网站上查看(图片是公开的)。
现在,在生产中,图片的网址是:https ://example.com/rails/active_storage/representations/1ej21h ...
它将 302 返回到 S3 存储桶: https ://my-bucket.amazonaws.com/variants/9jdh2 ...
我不是以下产品的忠实粉丝:
- 两次往返以获取图像;
- 向 Rails 服务器发送图像请求;
- 这些图像上的呆滞感。
而且我更愿意使用 Cloudfront 来提供这些图像。
我在 Rails Guides、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 release 修复。
推荐阅读
- php - 按数据库中的指定字段对所有结果进行排序
- mysql - MySQL中的阿拉伯语显示为?
- javascript - 使用 Node.js 的 google-translate-api 库时出现问题
- python - 关于 fit_generator() / fit() 和线程安全
- javascript - Vue多选元素在错误元素上触发事件
- java - 具有不同布局的多个回收站视图
- docker - docker-compose 设备通配符
- c++ - 如何在 MFC 中分隔 CString
- vba - ApplyFilter 一直有效,直到我点击清除过滤器的清除按钮
- bash - 无法打开 tar 文件