[英]Rails ActiveStorage Variants url
I'm trying to create an api to get a model's original image and other variants.我正在尝试创建一个 api 来获取模型的原始图像和其他变体。 Right now I'm not storing attachments on external storage, but I plan to save them on S3 in future.
现在我没有将附件存储在外部存储上,但我计划将来将它们保存在 S3 上。
Now I'm asking which is the best way to get images url?现在我想问哪种是获取图像 url 的最佳方式?
If using url_for
the result is: /rails/active_storage/blobs/redirect/...
or /rails/active_storage/representations/redirect/...
for variants,如果使用
url_for
结果是: /rails/active_storage/blobs/redirect/...
或/rails/active_storage/representations/redirect/...
对于变体,
while processed.url
is returning /rails/active_storage/disk/...
而
processed.url
正在返回/rails/active_storage/disk/...
_model.jbuilder _model.jbuilder
json.image url_for(model.image)
json.image_thumbnail url_for(model.image.variant(:thumbnail).processed)
json.image_icon model.image.variant(:icon).processed.url
I suggest rather than getting the stored URL to use activeStorage's proxy url and does not issue a redirect.我建议不要让存储的 URL 使用 activeStorage 的代理 url 并且不发出重定向。
This allows the storage provider to change and ideally you'd cache this response with a CDN like CloudFront if you're on AWS:这允许存储提供商更改,理想情况下,如果您在 AWS 上,您可以使用像 CloudFront 这样的 CDN 缓存此响应:
json.image_thumbnail rails_storage_proxy_path(model.image.variant(:thumbnail))
More on that here: https://blog.saeloun.com/2021/06/22/rails-active-storage-proxy.html and also in the guides: https://edgeguides.rubyonrails.org/active_storage_overview.html#proxy-mode有关更多信息,请访问: https://blog.saeloun.com/2021/06/22/rails-active-storage-proxy.html以及指南: https://edgeguides.rubyonrails.org/active_storage_overview.html#代理模式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.