我有一个模型叫 Campaign
和每个 Campaign
有一个附件。
我使用 S3 ActiveStorage 存储,我的 Campaign
需要一个永久 URL图片。
我目前生成的网址如下:
campaign.image.service_url
但是这个链接会在 5 分钟后失效。我需要非过期链接。 (配置设置只让我得到一个 1 周后过期的 URL,它并没有再次解决我的问题)如何获取我的图像的 URL?
编辑
解决方案:
我使用 CloudFront 作为 CDN。这是我找到的解决方案:
https://domainName+/campaign.image.key
这提供了一个指向不会过期的图像文件的链接。
最佳答案
检查文档 https://api.rubyonrails.org/classes/ActiveStorage/Variant.html#method-i-service_url
你不应该暴露 service_url
直接地:
Returns the URL of the variant on the service. This URL is intended to be short-lived for security and not used directly with users. Instead, the service_url should only be exposed as a redirect from a stable, possibly authenticated URL. Hiding the service_url behind a redirect also gives you the power to change services without updating all URLs. And it allows permanent URLs that redirect to the service_url to be cached in the view.
Use url_for(variant) (or the implied form, like +link_to variant+ or +redirect_to variant+) to get the stable URL for a variant that points to the ActiveStorage::RepresentationsController, which in turn will use this service_call method for its redirection.
所以使用
url_for(campaign.image)
(或 url_for(campaign.image.some_variant)
)代替。
关于ruby-on-rails - S3 中 ActiveStorage 对象的永久 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61292786/