ruby-on-rails - S3 中 ActiveStorage 对象的永久 URL

标签 ruby-on-rails amazon-s3

我有一个模型叫 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/

相关文章:

ruby-on-rails - 使用纯 Ruby 代码编写服务器应用程序是否明智?

ruby-on-rails - 莺 - 没有这样的文件来加载 zip/zip

ruby-on-rails - 如何在 Rails 5 中的现有表中添加新列?

ios - 如何使用 Swift 从 iOS 上传图像到 Ruby/Rails 服务器

jquery - 使用回形针中的单选按钮将图像设置为头像

google-chrome - Cross Origin amazon S3 无法使用 chrome

amazon-web-services - 无法将 RDS 快照导出到 S3 : "This operation is not currently supported"

python - 在 AWS Lambda 中重用 scikit 学习模型 .pkl

ruby-on-rails - 从 Rails 应用程序上的 s3 存储桶中获取 1000 多个对象

mysql - crontab 命令未运行